/// <summary>
        /// Verify whether the specified VMNetwork is HNV resource or not.
        /// </summary>
        /// <param name="vmnetwork">VMNetwork instance.</param>
        /// <returns>True if HNV resource, else false.</returns>
        public bool IsHNVVMNetwork(VMNetwork vmnetwork)
        {
            if (vmnetwork == null)
            {
                return(false);
            }

            // Create txn and read configuration.
            var txnMng = new TransactionManager();

            txnMng.StartTransaction();
            var logicalNetworkConfig = new LogicalNetworkConfig(this.VtnHostName);

            txnMng.SetConfigManager(logicalNetworkConfig, TransactionManager.OpenMode.ReadMode);

            bool ret;

            // Get LogicalNetowork that Guid equals vmnetwork.LogicalNetwork.
            LogicalNetwork logicalnetwork = logicalNetworkConfig.GetLogicalNetworkById(vmnetwork.LogicalNetwork);

            if (logicalnetwork == null)
            {
                ret = false;
                goto txnEnd;
            }

            var hnvLogicalNetworkManagement = new HNVLogicalNetworkManagement(this.VtnHostName);

            ret = hnvLogicalNetworkManagement.IsHNVLogicalNetwork(logicalnetwork);

            if (ret == false)
            {
                ODLVSEMETW.EventWriteNotHNVVMNetwork(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format("VMNetwork name is {0}", vmnetwork.Name));
            }
txnEnd:
            txnMng.EndTransaction(TransactionManager.Operation.None);
            return(ret);
        }
        /// <summary>
        /// Update the VSEM repository.
        /// </summary>
        /// <param name="txnMng">Transaction manager.</param>
        public void UpdateConnection(TransactionManager txnMng)
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder("\"TransactionManager\":" + JavaScriptSerializer.Serialize(txnMng));
            ODLVSEMETW.EventWriteStartLibrary(MethodBase.GetCurrentMethod().Name, json.ToString());
            if (txnMng == null) {
                throw new ArgumentException(
                    "txnMng' is null or invalid.");
            }

            string VtnHostName = this.ConnectionString;

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

            VtnHostName = VtnHostName.Split('.', ':').First();
            var vseminformation = this.CreateVSEMInfo();

            // Make sure we can connect to the ODL
            if (!this.VerifyConnection(vseminformation.MinVTNCoVersion)) {
                ODLVSEMETW.EventWriteProcesOdlLibraryError(MethodBase.GetCurrentMethod().Name,
                    "WebAPI version is not supported.");
                throw new ArgumentException("WebAPI version is not supported.");
            }
            Controller odl_ctr = new Controller(this.ConnectionString, this.controllers, this.Credential);
            odl_ctr.Create_ctr(Constants.CTR_NAME);

            ODLVSEMETW.EventWriteCreateODLData(
                MethodBase.GetCurrentMethod().Name, "Creating VSEM repository.");

            VSEMConfig vsemConfig = new VSEMConfig();
            txnMng.SetConfigManager(vsemConfig, TransactionManager.OpenMode.WriteMode);

            // Update Configuration Data;
            vsemConfig.Info = vseminformation;
            vsemConfig.Controller = new VSEMController();
            vsemConfig.Controller.ControllerInfo = this.controllers;
            if (vsemConfig.NetworkServiceSystemInformation.Id.CompareTo(Guid.Empty) == 0) {
                vsemConfig.NetworkServiceSystemInformation = VSEMConnection.CreateSystemInfo();
            } else {
                VSEMODLConstants.SYSTEM_INFO_ID = vsemConfig.NetworkServiceSystemInformation.Id;
            }
            if (vsemConfig.SwitchExtensionInfo.Count == 0) {
                vsemConfig.SwitchExtensionInfo = VSEMConnection.CreateSwitchExtensionInfos();
            } else {
                VSEMODLConstants.SWITCH_EXTENSION_INFO_ID = vsemConfig.SwitchExtensionInfo[0].Id;
            }
            vsemConfig.SwitchExtensionInfo.ForEach((i) =>
                    VSEMConnection.AddSwitchFeatureToSwitchExtInfos(i, this.controllers));

            LogicalNetworkConfig logicalNetworkConfig = new LogicalNetworkConfig(VtnHostName);
            txnMng.SetConfigManager(logicalNetworkConfig, TransactionManager.OpenMode.WriteMode);
            if (logicalNetworkConfig.LogicalNetworks.Count == 0) {
                logicalNetworkConfig.LogicalNetworks =
                    this.CreateLogicalNetworks();
            }

            PortProfileConfig portProfileConfig = new PortProfileConfig();
            txnMng.SetConfigManager(portProfileConfig, TransactionManager.OpenMode.WriteMode);

            if (portProfileConfig.UplinkPortProfiles.Count == 0) {
                portProfileConfig.UplinkPortProfiles =
                    VSEMConnection.CreateUplinkPortProfiles();
            } else {
                VSEMODLConstants.UPLINK_PORT_PROFILE_ID = portProfileConfig.UplinkPortProfiles[0].Id;
            }

            if (portProfileConfig.VirtualPortProfiles.Count == 0) {
                portProfileConfig.VirtualPortProfiles =
                    VSEMConnection.CreateVirtualPortProfiles();
            }

            // Create HNV default resource.
            HNVLogicalNetworkManagement hnvMgmt = new HNVLogicalNetworkManagement(this.VtnHostName);
            hnvMgmt.CreateHNVLogicalNetwork(logicalNetworkConfig);
            hnvMgmt.AssociatePortProfileWithHNV(portProfileConfig);

            this.SaveVtncoInfo(txnMng, VtnHostName);

            ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty);
        }
        /// <summary>
        /// Process the updated logical network.
        /// </summary>
        /// <remarks>
        /// Ignore logical network that does not belong to us.
        /// </remarks>
        protected override void DoODLVSEMCmdlet()
        {
            var hnvLogicalNetworkManagement = new HNVLogicalNetworkManagement(this.conn.GetVtnHostName());

            // Make sure we are managing this logical network.
            if (hnvLogicalNetworkManagement.IsHNVLogicalNetwork(this.LogicalNetwork) == true) {
                var txnMng = new TransactionManager();
                txnMng.StartTransaction();
                var ope = TransactionManager.Operation.None;
                bool writeResult = false;

                try {
                    switch (this.OperationType) {
                        case NetworkEntityPublishType.Create:
                            {
                                // Create is not supported through VMM.
                                writeResult = true;
                            }
                            break;

                        case NetworkEntityPublishType.Update:
                            {
                                hnvLogicalNetworkManagement.UpdateLogicalNetwork(txnMng, this.LogicalNetwork);
                                writeResult = hnvLogicalNetworkManagement.IsLogicalNetworkValid(this.LogicalNetwork);
                            }
                            break;

                        case NetworkEntityPublishType.Delete:
                            {
                                // Delete is not supported through VMM.
                                writeResult = true;
                            }
                            break;
                    }

                    ope = TransactionManager.Operation.Commit;
                } catch (Exception ex) {
                    ope = TransactionManager.Operation.Rollback;
                    throw VSEMODLExceptionUtil.ConvertExceptionToVSEMException(ex);
                } finally {
                    txnMng.EndTransaction(ope);
                }

                string output = "\"Logical Network\":" + new JavaScriptSerializer().Serialize(this.LogicalNetwork);
                ODLVSEMETW.EventWriteEndCmdlet(this.CmdletName, output);

                // Indicate errors.
                if (!writeResult) {
                    this.WriteObject(this.LogicalNetwork);
                }
            }
        }
        /// <summary>
        /// This function is responsible to retrieve the VSEMFabricNetworkDefinition information
        /// for the connection.
        /// </summary>
        protected override void DoODLVSEMCmdlet()
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder(" \"ID\":" + JavaScriptSerializer.Serialize(this.ID));
            ODLVSEMETW.EventWriteDoCmdlet(this.CmdletName,
                "Retrieving logical network definition.",
                json.ToString());
            List<LogicalNetwork> vmNw = null;
            TransactionManager txnMng = new TransactionManager();
            txnMng.StartTransaction();
            var operation = TransactionManager.Operation.None;
            try {
                if (this.ID != Guid.Empty) {
                    vmNw = new List<LogicalNetwork>();
                    vmNw.Add(this.conn.GetVSEMLogicalNetworkById(txnMng, this.ID));
                    ODLVSEMETW.EventWriteReturnLibrary(string.Format(CultureInfo.CurrentCulture,
                        "Logical network definition is retrieved by ID: {0}.",
                        this.ID.ToString("B")),
                        string.Empty);

                    if (vmNw[0] == null) {
                        ODLVSEMETW.EventWriteProcessCmdletWarning(this.CmdletName,
                        string.Format(CultureInfo.CurrentCulture,
                        "ID '{0}' not found.",
                        this.ID.ToString("B")));
                        this.WriteWarning(string.Format(CultureInfo.CurrentCulture,
                            "ID '{0}' not found.",
                            this.ID.ToString("B")));
                    } else {
                        var hnvmgmt = new HNVLogicalNetworkManagement(this.conn.GetVtnHostName());

                        // Validate HNV LogicalNetwork. Need not check return value.
                        hnvmgmt.IsLogicalNetworkValid(vmNw[0]);
                        this.WriteObject(vmNw, true);
                    }
                } else {
                    vmNw = this.conn.GetVSEMLogicalNetwork(txnMng);
                    ODLVSEMETW.EventWriteReturnLibrary(
                        "Logical networks are retrieved.", string.Empty);
                    if (vmNw == null) {
                        ODLVSEMETW.EventWriteProcessCmdletWarning(this.CmdletName,
                            "Logical network not found.");
                        this.WriteWarning("Logical network not found.");
                    } else {
                        var hnvmgmt = new HNVLogicalNetworkManagement(this.conn.GetVtnHostName());
                        for (int i = 0; i < vmNw.Count; i++) {
                            // Validate HNV LogicalNetworks. Need not check return value.
                            hnvmgmt.IsLogicalNetworkValid(vmNw[i]);
                        }
                        this.WriteObject(vmNw, true);
                    }
                }
            } catch (Exception ex) {
                Exception exception = VSEMODLExceptionUtil.ConvertExceptionToVSEMException(ex);
                ODLVSEMETW.EventWriteFailedCmdlet(this.CmdletName, exception.GetType() + " : " + ex.Message);
                operation = TransactionManager.Operation.Rollback;
                throw exception;
            } finally {
                txnMng.EndTransaction(operation);

                string output = "\"LogicalNetwork\":" + JavaScriptSerializer.Serialize(vmNw);
                ODLVSEMETW.EventWriteEndCmdlet(this.CmdletName, output);
            }
        }
        /// <summary>
        /// Verify whether the specified VMNetwork is HNV resource or not.
        /// </summary>
        /// <param name="vmnetwork">VMNetwork instance.</param>
        /// <returns>True if HNV resource, else false.</returns>
        public bool IsHNVVMNetwork(VMNetwork vmnetwork)
        {
            if (vmnetwork == null) {
                return false;
            }

            // Create txn and read configuration.
            var txnMng = new TransactionManager();
            txnMng.StartTransaction();
            var logicalNetworkConfig = new LogicalNetworkConfig(this.VtnHostName);
            txnMng.SetConfigManager(logicalNetworkConfig, TransactionManager.OpenMode.ReadMode);

            bool ret;

            // Get LogicalNetowork that Guid equals vmnetwork.LogicalNetwork.
            LogicalNetwork logicalnetwork = logicalNetworkConfig.GetLogicalNetworkById(vmnetwork.LogicalNetwork);
            if (logicalnetwork == null) {
                ret = false;
                goto txnEnd;
            }

            var hnvLogicalNetworkManagement = new HNVLogicalNetworkManagement(this.VtnHostName);
            ret = hnvLogicalNetworkManagement.IsHNVLogicalNetwork(logicalnetwork);

            if (ret == false) {
                ODLVSEMETW.EventWriteNotHNVVMNetwork(
                    MethodBase.GetCurrentMethod().Name,
                    string.Format("VMNetwork name is {0}", vmnetwork.Name));
            }
            txnEnd:
            txnMng.EndTransaction(TransactionManager.Operation.None);
            return ret;
        }