Ejemplo n.º 1
0
        /// <summary>
        /// This function is responsible for creating a VM Network
        /// with one VM Subnet on the VSEM.
        /// </summary>
        protected override void DoODLVSEMCmdlet()
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder(" \"IPSubnets\":" + JavaScriptSerializer.Serialize(this.IPSubnets));
            json.Append("\"MaxNumberOfPorts\":\"" + this.MaxNumberOfPorts + "\"");
            json.Append("\"VMSubnetName\":\"" + this.VMSubnetName + "\"");
            json.Append("\"VMNetworkName\":\"" + this.VMNetworkName + "\"");
            json.Append("\"LogicalNetworkDefinitionId\":\"" + this.LogicalNetworkDefinitionId + "\"");
            ODLVSEMETW.EventWriteDoCmdlet(this.CmdletName,
                   "VM Network creation process started.",
                   json.ToString());
            TransactionManager txnMng = new TransactionManager();
            txnMng.StartTransaction();
            var ope = TransactionManager.Operation.None;
            VMNetwork nw = null;
            string vtnName = string.Empty;
            string connectionString =
                this.conn.ConnectionString.Split(',').FirstOrDefault();
            VSEMVMNetworkManagement vSEMVmNetworkManagement =
                new VSEMVMNetworkManagement(connectionString, this.conn.Credential);
            try {
                nw = vSEMVmNetworkManagement.CreateVMNetwork(txnMng,
                    this.VMSubnetName,
                    this.VMNetworkName,
                    this.MaxNumberOfPorts,
                    this.IPSubnets,
                    this.LogicalNetworkDefinitionId,
                    this.conn,
                    out vtnName);
                ODLVSEMETW.EventWriteReturnLibrary("VM network is created.",
                    string.Empty);

                ope = TransactionManager.Operation.Commit;
            } catch (Exception ex) {
                Exception userException = ex;
                ODLVSEMETW.EventWriteFailedCmdlet(
                    "VM network creation is failed. ODL changes rollback is started.",
                    string.Empty);
                ope = TransactionManager.Operation.Rollback;
                try {
                    if (!string.IsNullOrEmpty(vtnName)) {
                        vSEMVmNetworkManagement.RemoveVmNetwork(vtnName);
                        ODLVSEMETW.EventWriteFailedCmdlet(
                            "VM network creation is failed. ODL changes are successfully rolled back.",
                            string.Empty);
                    }
                } catch (Exception except) {
                    ODLVSEMETW.EventWriteFailedCmdlet(
                        string.Format(CultureInfo.CurrentCulture,
                        "Due to some problem in connection with ODL, VSEM VM Network creation process is terminated in the middle of the request. VTN '{0}' may have been created on ODL, Please check. Please delete the VTN from ODL to maintain the consistency between ODL and SCVMM.\n{1}",
                        vtnName,
                        except.Message),
                        string.Empty);
                    userException =
                        new System.Net.WebException(string.Format(CultureInfo.CurrentCulture,
                            "Due to some problem in connection with ODL, VSEM VM Network creation process is terminated in the middle of the request. VTN '{0}' may have been created on ODL, Please check. Please delete the VTN from ODL to maintain the consistency between ODL and SCVMM.",
                            vtnName),
                            ex);
                }

                Exception exception = VSEMODLExceptionUtil.ConvertExceptionToVSEMException(userException);
                ODLVSEMETW.EventWriteFailedCmdlet(this.CmdletName, exception.GetType() + " : " + ex.Message);
                ope = TransactionManager.Operation.Rollback;
                throw exception;
            } finally {
                txnMng.EndTransaction(ope);

                string output = "\"VM Network\":" + JavaScriptSerializer.Serialize(nw);
                ODLVSEMETW.EventWriteEndCmdlet(this.CmdletName, output);
                this.WriteObject(nw);
            }
        }