/// <summary>
        /// This method is responsible to add specified vlan entry into the local variable.
        /// </summary>
        protected override void DoODLVSEMCmdlet()
        {
            ODLVSEMETW.EventWriteDoCmdlet(this.CmdletName,
                "Synchronizing SCVMM and ODL.",
                string.Empty);
            VSEMSynchronization vSEMSynchronization = null;
            if (string.IsNullOrEmpty(this.SCVMSubnets)) {
                TransactionManager txnMng = new TransactionManager();
                txnMng.StartTransaction();
                var operation = TransactionManager.Operation.None;
                try {
                    vSEMSynchronization = new VSEMSynchronization(txnMng);

                    vSEMSynchronization.SynchronizeVTNObjects(txnMng);
                    operation = TransactionManager.Operation.Commit;
                } catch (Exception ex) {
                    Exception exception = VSEMODLExceptionUtil.ConvertExceptionToVSEMException(ex);
                    ODLVSEMETW.EventWriteFailedCmdlet(this.CmdletName, exception.GetType() + " : synchronization is failed. \n" + ex.Message);
                    operation = TransactionManager.Operation.Rollback;
                    ODLVSEMETW.EventWriteEndCmdlet(this.CmdletName, string.Empty);
                    throw exception;
                } finally {
                    txnMng.EndTransaction(operation);
                }
            } else {
                TransactionManager txnMng = new TransactionManager();
                txnMng.StartTransaction();
                var operation = TransactionManager.Operation.Commit;
                try {
                    vSEMSynchronization = new VSEMSynchronization(txnMng);
                    vSEMSynchronization.StatusAfterRefreshNetworkService(txnMng, this.SCVMSubnets);
                } catch (Exception ex) {
                    operation = TransactionManager.Operation.Rollback;
                    if (ex.GetType() != typeof(NSPluginSynchronizationException)) {
                        ODLVSEMETW.EventWriteFailedCmdlet("NetworkService could not be refreshed in SCVMM. " + ex.Message,
                           string.Empty);

                        throw new NSPluginSynchronizationException("NetworkService could not be refreshed in SCVMM. " + ex.Message);
                    } else {
                        throw;
                    }
                } finally {
                    txnMng.EndTransaction(operation);
                    ODLVSEMETW.EventWriteEndCmdlet(this.CmdletName, string.Empty);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Synchronize the VMSubnet.
        /// </summary>
        /// <param name="vmNetworkDefinitionId"> ID of the VMSubnet.</param>
        /// <param name="vMNetworkConfig">Instance of VMNetwor.config file.</param>
        private void SyncVMSubnet(Guid vmNetworkDefinitionId, VMNetworkConfig vMNetworkConfig)
        {
            string        vtnName = string.Empty;
            string        vbrName = string.Empty;
            VMNetworkInfo vmnetworkMappigInfoFound = null;

            foreach (var vmNet in
                     vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo)
            {
                foreach (var vmSubNet in vmNet.VMSubnetInfo)
                {
                    if (vmSubNet.VMSubnetID == vmNetworkDefinitionId)
                    {
                        vtnName = vmNet.VTNName;
                        vbrName = vmSubNet.VBridgeName;
                        vmnetworkMappigInfoFound = vmNet;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(vtnName))
                {
                    break;
                }
            }
            VSEMSynchronization vSEMSynchronization =
                new VSEMSynchronization(this.ConnectionString, this.Credential);
            Controller odl       = new Controller(this.ConnectionString, this.Credential);
            int        odlStatus = odl.CheckVbridgeStatus(vtnName, vbrName);

            if (odlStatus == -2)
            {
                VSEMSynchronization.RemoveVMNetworkSync(vMNetworkConfig, vmnetworkMappigInfoFound);
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                    "Corresponding VTN is not found on ODL.");
                throw new DataMisalignedException("Corresponding VTN is not found on ODL.\nRefresh the Odl configuration and then retry.");
            }
            if (odlStatus == -1)
            {
                VSEMSynchronization.RemoveVMSubnetSync(vMNetworkConfig, vmNetworkDefinitionId, null, null);
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                    "Corresponding vBridge is not found on ODL.");
                throw new DataMisalignedException("Corresponding vBridge is not found on ODL.\nRefresh the Odl configuration and then retry.");
            }
        }
        /// <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(" \"VMNetwork\":" + JavaScriptSerializer.Serialize(this.VMNetwork));
            json.Append(" \"OperationType\":" + JavaScriptSerializer.Serialize(this.OperationType));
            ODLVSEMETW.EventWriteDoCmdlet(this.CmdletName,
                   "VM Network creation process started.",
                   json.ToString());

            // If VMNetwork is HNV resource, return immediately.
            var mgmt = new HNVVMNetworkManagement(this.conn.GetVtnHostName());
            if (mgmt.IsHNVVMNetwork(this.VMNetwork) == true) {
                return;
            }

            TransactionManager txnMng = new TransactionManager();
            txnMng.StartTransaction();
            var ope = TransactionManager.Operation.None;
            VMNetwork nw = null;
            string connectionString =
                this.conn.ConnectionString.Split(',').FirstOrDefault();
            VSEMSynchronization vSEMSynchronization =
                new VSEMSynchronization(connectionString, this.conn.Credential);
            try {
                bool needRefresh = vSEMSynchronization.PublishVMNetwork(txnMng,
                    this.VMNetwork,
                    this.OperationType,
                    this.conn);
                ODLVSEMETW.EventWriteReturnLibrary("VM network is updated.",
                    string.Empty);
                if (needRefresh) {
                    this.conn.RefreshConnection();
                }
                ope = TransactionManager.Operation.Commit;
            } catch (Exception ex) {
                Exception exception = VSEMODLExceptionUtil.ConvertExceptionToVSEMException(ex);
                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(this.VMNetwork);
            }
        }
        /// <summary>
        /// Synchronize the VMSubnet.
        /// </summary>
        /// <param name="vmNetworkDefinitionId"> ID of the VMSubnet.</param>
        /// <param name="vMNetworkConfig">Instance of VMNetwor.config file.</param>
        private void SyncVMSubnet(Guid vmNetworkDefinitionId, VMNetworkConfig vMNetworkConfig)
        {
            string vtnName = string.Empty;
            string vbrName = string.Empty;
            VMNetworkInfo vmnetworkMappigInfoFound = null;
            foreach (var vmNet in
                vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo) {
                foreach (var vmSubNet in vmNet.VMSubnetInfo) {
                    if (vmSubNet.VMSubnetID == vmNetworkDefinitionId) {
                        vtnName = vmNet.VTNName;
                        vbrName = vmSubNet.VBridgeName;
                        vmnetworkMappigInfoFound = vmNet;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(vtnName)) {
                    break;
                }
            }
            VSEMSynchronization vSEMSynchronization =
                new VSEMSynchronization(this.ConnectionString, this.Credential);
            Controller odl = new Controller(this.ConnectionString, this.Credential);
            int odlStatus = odl.CheckVbridgeStatus(vtnName, vbrName);

            if (odlStatus == -2) {
                VSEMSynchronization.RemoveVMNetworkSync(vMNetworkConfig, vmnetworkMappigInfoFound);
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                        "Corresponding VTN is not found on ODL.");
                throw new DataMisalignedException("Corresponding VTN is not found on ODL.\nRefresh the Odl configuration and then retry.");
            }
            if (odlStatus == -1) {
                VSEMSynchronization.RemoveVMSubnetSync(vMNetworkConfig, vmNetworkDefinitionId, null, null);
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                        "Corresponding vBridge is not found on ODL.");
                throw new DataMisalignedException("Corresponding vBridge is not found on ODL.\nRefresh the Odl configuration and then retry.");
            }
        }
        /// <summary>
        /// Remove the specified VM Subnet.
        /// </summary>
        /// <param name="txnMng">Transaction manager.</param>
        /// <param name="id">ID of VM Subnet to remove.</param>
        /// <param name="connection">VSEM connection.</param>
        /// <param name="vbrName">Name of the vBridge.</param>
        /// <returns>Indicated the need to refresh the network service..</returns>
        public bool RemoveVmNetworkDefinition(TransactionManager txnMng,
            Guid id,
            VSEMConnection connection,
            out string vbrName)
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder("\"txnMng\":" + JavaScriptSerializer.Serialize(txnMng));
            json.Append("\"id\":\"" + id + "\"");
            ODLVSEMETW.EventWriteStartLibrary(MethodBase.GetCurrentMethod().Name,
                json.ToString());
            if (txnMng == null) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'txnMng' is null or invalid.");
                throw new ArgumentException("The parameter 'txnMng' is null or invalid.");
            }
            if (id == Guid.Empty) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'id' is null or invalid.");
                throw new ArgumentException("The parameter 'id' is null or invalid.");
            }
            if (connection == null) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'connection' is null or invalid.");
                throw new ArgumentException("The parameter 'connection' is null or invalid.");
            }
            string VtnHostName = this.ConnectionString;

            if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) {
                VtnHostName = VtnHostName.Substring(8);
            }

            VtnHostName = VtnHostName.Split('.', ':').First();
            var vMNetworkConfig = new VMNetworkConfig(VtnHostName);
            txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.WriteMode);
            if (vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Count == 0) {
                ODLVSEMETW.EventWriteConfigManagerFileIOError(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "VMNetwork.config {0}",
                    configFileIOErrorValidationMessage));
                throw new InvalidOperationException(
                    string.Format(CultureInfo.CurrentCulture,
                    "VMNetwork.config {0}",
                    configFileIOErrorValidationMessage));
            }

            string vtnName = string.Empty;
            vbrName = string.Empty;
            Guid vmNetId = Guid.Empty;
            VMNetworkInfo vmnetworkMappigInfoFound = null;
            long vlanId = 0;
            foreach (var vmNet in
                vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo) {
                foreach (var vmSubNet in vmNet.VMSubnetInfo) {
                    if (vmSubNet.VMSubnetID == id) {
                        vtnName = vmNet.VTNName;
                        vbrName = vmSubNet.VBridgeName;
                        vmNetId = vmNet.VMNetworkID;
                        vlanId = vmSubNet.VBridgeVlanId;
                        vmnetworkMappigInfoFound = vmNet;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(vtnName)) {
                    break;
                }
            }
            if (string.IsNullOrEmpty(vbrName)) {
                ODLVSEMETW.EventWriteGetVSEMVMNetworkError(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "VMSubnet '{0}' not found.",
                    id.ToString("B")));
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                    "VMSubnet '{0}' not found.",
                    id.ToString("B")));
            }
            VMNetwork network = null;
            foreach (var vmNet in
                 vMNetworkConfig.VMNetwork.VmNetworks) {
                if (vmNet.Id == vmNetId) {
                    network = vmNet;
                    break;
                }
            }
            if (network == null || !network.VMSubnets.Any(vms => vms.Id.CompareTo(id) == 0)) {
                ODLVSEMETW.EventWriteGetVSEMVMNetworkError(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "VMSubnet '{0}' not found.",
                    id.ToString("B")));
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                    "VMSubnet '{0}' not found.",
                    id.ToString("B")));
            }
            var logicalNetworkDefinitionId = vMNetworkConfig.VMNetwork.VmNetworks.FirstOrDefault(
                vmn => vmn.Id == vmNetId).VMSubnets.FirstOrDefault(
                subNetwork => subNetwork.Id == id).LogicalNetworkDefinitionId;

            VSEMSynchronization vSEMSynchronization =
                new VSEMSynchronization(this.ConnectionString, this.Credential);
            Controller odl = new Controller(this.ConnectionString, this.Credential);
            List<Vtn> vtns = odl.ReadVTNObjects(vtnName);
            if (vtns.Count == 0 && network.VMSubnets.Count() > 1) {
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                    "Corresponding VTN is not found on ODL.");
                throw new DataMisalignedException(
                    "Corresponding VTN is not found on ODL.\nRefresh the Odl configuration and then retry.");
            }
            string vbridgeName = vbrName;
            if (vtns[0].Vbridges.Any(vbr => vbr.Name.CompareTo(vbridgeName) != 0) && network.VMSubnets.Count() == 1) {
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                    "On synchronization, additional vBridge(s) are found in the VTN corresponding to this VM Network on ODL.\nSo, this VM Network cannot be removed and the operation has been abandoned.\nRefresh the Odl configuration and then retry.");
                throw new DataMisalignedException(
                    "On synchronization, additional vBridge(s) are found in the VTN corresponding to this VM Network on ODL.\nSo, this VM Network cannot be removed and the operation has been abandoned.\nRefresh the Odl configuration and then retry.");
            }
            Vbridge vbridge = vtns[0].Vbridges.FirstOrDefault(vbr => vbr.Name.CompareTo(vbridgeName) == 0);
            if (vbridge != null && vlanId != vbridge.VlanId) {
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                        "VLAN ID of corresponding vBridge is modified on ODL.");
                throw new DataMisalignedException(
                    "VLAN ID of corresponding vBridge is modified on ODL.\nRefresh the Odl configuration and then retry.");
            }

            var vmsubnet = vmnetworkMappigInfoFound.VMSubnetInfo.Where(vms =>
                vms.VMSubnetID.CompareTo(id) == 0).First();
            if (vmsubnet.CreatedFrom.Contains("ODL") &&
                vbridge != null) {
                ODLVSEMETW.EventWriteGetVSEMVMNetworkError(
                   MethodBase.GetCurrentMethod().Name,
                   "Corresponding vBridge is created or modified on ODL");
                throw new DataMisalignedException("Corresponding vBridge is created or modified on ODL.\nRefresh the Odl configuration and then retry.");
            }
            if (vtns.Count != 0) {
                Vbridge vBridge = new Vbridge(this.ConnectionString, this.Credential);
                vBridge.RemoveVbridge(vtnName, vbrName);
                ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty);
            }

            // Checking if VM Network removal is required.
            network.VMSubnets = network.VMSubnets.Where(
                netDef => netDef.Id != id).ToArray();

            vmnetworkMappigInfoFound.VMSubnetInfo.RemoveAll(subNet => subNet.VMSubnetID == id);
            if (network.VMSubnets.Count() == 0) {
                VSEMVMNetworkManagement vmNetworkManagement =
                    new VSEMVMNetworkManagement(this.ConnectionString,
                        this.Credential);
                vmNetworkManagement.RemoveVmNetwork(vMNetworkConfig,
                    vmNetId);
            }

            odl.UpdateStartupConfiguration();
            ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty);

            ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty);
            return false;
        }
        /// <summary>
        /// Create a new VM subnet with specified parameters using VTN on demand.
        /// </summary>
        /// <param name="txnMng">Transaction manager.</param>
        /// <param name="name">Name of VM Subnet to create.</param>
        /// <param name="vMNetworkId">Id of VM network to create.</param>
        /// <param name="maxNumberOfPorts">Maximum number of ports.</param>
        /// <param name="ipSubnets">IP pools to use.</param>
        /// <param name="logicalNetworkDefinitionId">ID of logical network definition to associate with.</param>
        /// <param name="connection">VSEM connection.</param>
        /// <param name="vtnName">VTN name.</param>
        /// <param name="vbrName">Vbridge name.</param>
        /// <returns>VM network if successful, else null.</returns>
        public VMSubnet CreateVMNetworkDefinition(TransactionManager txnMng,
            string name,
            Guid vMNetworkId,
            long? maxNumberOfPorts,
            IPSubnet[] ipSubnets,
            Guid logicalNetworkDefinitionId,
            VSEMConnection connection,
            out string vtnName,
            out string vbrName)
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder("\"txnMng\":" + JavaScriptSerializer.Serialize(txnMng));
            json.Append(" \"ipSubnets\":" + JavaScriptSerializer.Serialize(ipSubnets));
            json.Append("\"maxNumberOfPorts\":\"" + maxNumberOfPorts + "\"");
            json.Append("\"name\":\"" + name + "\"");
            json.Append("\"logicalNetworkDefinitionId\":\"" + logicalNetworkDefinitionId + "\"");
            ODLVSEMETW.EventWriteStartLibrary(MethodBase.GetCurrentMethod().Name,
                json.ToString());
            if (txnMng == null) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'txnMng' is null or invalid.");

                throw new ArgumentException("The parameter 'txnMng' is null or invalid.");
            }

            if (string.IsNullOrEmpty(name)) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'name' is null or invalid.");

                throw new ArgumentException("The parameter 'name' is null or invalid.");
            }

            if (vMNetworkId == Guid.Empty) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'vMNetworkId' is null or invalid.");

                throw new ArgumentException("The parameter 'vMNetworkId' is null or invalid.");
            }
            if (logicalNetworkDefinitionId == Guid.Empty) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'logicalNetworkDefinitionId' is null or invalid.");

                throw new ArgumentException(
                    "The parameter 'logicalNetworkDefinitionId' is null or invalid.");
            }

            if (connection == null) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'connection' is null or invalid.");

                throw new ArgumentException("The parameter 'connection' is null or invalid.");
            }

            string VtnHostName = this.ConnectionString;

            if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) {
                VtnHostName = VtnHostName.Substring(8);
            }

            VtnHostName = VtnHostName.Split('.', ':').First();

            var vMNetworkConfig = new VMNetworkConfig(VtnHostName);
            txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.WriteMode);
            if (vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Count == 0) {
                ODLVSEMETW.EventWriteConfigManagerFileIOError(MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "VMNetwork.config {0}",
                    configFileIOErrorValidationMessage));
                throw new InvalidOperationException(
                    string.Format(CultureInfo.CurrentCulture,
                    "VMNetwork.config {0}",
                    configFileIOErrorValidationMessage));
            }

            var vmNw = vMNetworkConfig.VMNetwork.VmNetworks.FirstOrDefault(
                nwk => nwk.Id == vMNetworkId);

            if (vmNw == null) {
                ODLVSEMETW.EventWriteGetFabricNetworkDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "VM network '{0}' not found.",
                    vMNetworkId.ToString("B")));
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                    "VM network '{0}' not found. VTN corresponding to this VM network may have been deleted from ODL.\nRefer Network Object Mapping screen.",
                    vMNetworkId.ToString("B")));
            }

            var LogicalNetworkConfig = new LogicalNetworkConfig(VtnHostName);
            txnMng.SetConfigManager(LogicalNetworkConfig, TransactionManager.OpenMode.ReadMode);
            if (LogicalNetworkConfig.LogicalNetworks.Count == 0) {
                ODLVSEMETW.EventWriteConfigManagerFileIOError(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "LogicalNetwork.config {0}",
                    configFileIOErrorValidationMessage));
                throw new InvalidOperationException(
                    string.Format(CultureInfo.CurrentCulture,
                    "LogicalNetwork.config {0}",
                    configFileIOErrorValidationMessage));
            }

            var logicalNwDef = LogicalNetworkConfig.LogicalNetworks.First().LogicalNetworkDefinitions.FirstOrDefault(
                fabnw => fabnw.Id == logicalNetworkDefinitionId);

            if (logicalNwDef == null) {
                ODLVSEMETW.EventWriteGetFabricNetworkDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format(CultureInfo.CurrentCulture,
                    "Logical network definition '{0}' not found.",
                    logicalNetworkDefinitionId.ToString("B")));
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                     "Logical network definition '{0}' not found.",
                logicalNetworkDefinitionId.ToString("B")));
            }

            string output = "\"logicalNwDef\":" + JavaScriptSerializer.Serialize(logicalNwDef);
            if (!logicalNwDef.SupportsVMNetworkProvisioning) {
                ODLVSEMETW.EventWriteSupportsVMNetworkProvisioningError(
                    "Logical does not support VM network creation.",
                    output);
                throw new InvalidOperationException(
                    "Logical does not support VM network creation.");
            }
            VMNetworkInfo vmnetworkInfo = vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.First(vmn => vmn.VMNetworkID.CompareTo(vmNw.Id) == 0);
            VSEMSynchronization vSEMSynchronization =
                new VSEMSynchronization(this.ConnectionString, this.Credential);
            Controller odl = new Controller(this.ConnectionString, this.Credential);
            List<Vtn> vtns = odl.ReadVTNObjects(vmnetworkInfo.VTNName);
            if (vtns.Count == 0) {
                ODLVSEMETW.EventWriteValidateVMNetDefinitionError(
                    MethodBase.GetCurrentMethod().Name,
                        "Corresponding VTN is not found on ODL.");
                throw new DataMisalignedException("Corresponding VTN is not found on ODL.\nRefresh the Odl configuration and then retry.");
            }

            VMSubnet nw = this.CreateVMNetworkDefinitionforVtn(txnMng,
                LogicalNetworkConfig,
                vMNetworkConfig,
                name,
                vMNetworkId,
                maxNumberOfPorts,
                ipSubnets,
                logicalNetworkDefinitionId,
                connection,
                out vtnName,
                out vbrName);

            if (nw == null) {
                ODLVSEMETW.EventWriteProcessFailedVMSubNetworkError(
                    MethodBase.GetCurrentMethod().Name,
                    "Failed to create VM Subnet.");
                throw new InvalidOperationException("Failed to create VM Subnet.");
            }

            odl = new Controller(this.ConnectionString, this.Credential);
            odl.UpdateStartupConfiguration();
            ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty);

            string outputLib = "\"nw\":" + JavaScriptSerializer.Serialize(nw);
            ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, outputLib);
            return nw;
        }