/// <summary>
        /// Add the VMSubnet that corresponds the newly created vBridge on ODL.
        /// </summary>
        /// <param name="vmnetworkInfo">VM Network info.</param>
        /// <param name="vbr">Newly created vBridge.</param>
        /// <param name="vmnetwork">VM network that correspond the VTN in which vBridge is created.</param>
        /// <param name="logicalNetworkDefinitionId">Logical network definition Id.</param>
        private void AddVMSubnet(VMNetworkInfo vmnetworkInfo, Vbridge vbr, VMNetwork vmnetwork, Guid? logicalNetworkDefinitionId)
        {
            List<string> ipsubnets = new List<string>();

            vmnetwork.VMSubnets.ToList().Where(item => item.Description.Contains("/")).ToList().ForEach(sub
                    => sub.IPSubnets.ToList().ForEach(ipsub
                        => ipsubnets.Add(ipsub.Subnet)));
            string ipsubnet = this.CreateUniqueIPSubnet(ipsubnets);
            VMSubnet vmsubnet = VSEMVMSubnetManagement.CreateVbrVmNetworkDefinition(vmnetworkInfo.VMNetworkID,
                null,
                new IPSubnet[] { new IPSubnet{
                                    AddressFamily = AddressFamily.IPv4,
                                    Subnet = ipsubnet,
                                    Id = Guid.NewGuid(),
                                    LastModifiedTimeStamp = DateTime.Now,
                                    SupportsDHCP = true
                                }},
                vbr.Name,
                logicalNetworkDefinitionId,
                vbr.Name,
                vbr.VlanId);
            vmsubnet.Description = vmsubnet.Description + " (" + ipsubnet + ")";
            Array.Resize(ref vmnetwork.VMSubnets, vmnetwork.VMSubnets.Length + 1);
            vmnetwork.VMSubnets[vmnetwork.VMSubnets.Length - 1] = vmsubnet;
            vmnetworkInfo.VMSubnetInfo.Add(new VMSubnetInfo {
                VBridgeName = vbr.Name,
                VMSubnetVlanId = string.IsNullOrEmpty(vmnetworkInfo.VMNetworkName) ? 0 : vbr.VlanId,
                VMSubnetID = vmsubnet.Id,
                VMSubnetName = string.IsNullOrEmpty(vmnetworkInfo.VMNetworkName) ? string.Empty : vbr.Name,
                CreatedFrom = "ODL",
                VBridgeVlanId = vbr.VlanId
            });
        }
        /// <summary>
        /// This function is responsible to synchronize the network configuration between ODL and SCVMM.
        /// </summary>
        /// <param name="vtns">List of VTNs on ODL.</param>
        /// <param name="vMNetworkConfig">VMNetwork.config file instance.</param>
        /// <param name="logicalNetworkId">Logical network ID.</param>
        /// <param name="logicalNetworkDefinitionId">Logical network definition ID.</param>
        /// <param name="txnMng">Transaction manager instance.</param>
        /// <param name="VtnHostName">Host name of the VTNCoordinator.</param>
        private void CompareVTNObjects(List<Vtn> vtns, VMNetworkConfig vMNetworkConfig, Guid logicalNetworkId, Guid? logicalNetworkDefinitionId, TransactionManager txnMng, string VtnHostName)
        {
            var vmnetworks = vMNetworkConfig.VMNetwork;
            foreach (Vtn vtn in vtns) {
                var vmnetworkInfo = vmnetworks.VMNetworkMappingInformation.VMNetworkInfo.FirstOrDefault(vmnet => vmnet.VTNName.Equals(vtn.Name));
                if (vmnetworkInfo != null) {
                    VMNetwork vmnetwork = vmnetworks.VmNetworks.FirstOrDefault(vmn => vmn.Id == vmnetworkInfo.VMNetworkID);
                    this.SyncVTN(logicalNetworkDefinitionId, vmnetwork, vtn, vmnetworkInfo, txnMng, VtnHostName);
                } else {
                    VMNetwork vMNetwork = VSEMVMNetworkManagement.CreateVtnVmNetwork(vtn.Name);
                    vMNetwork.LogicalNetwork = logicalNetworkId;

                    vMNetworkConfig.VMNetwork.VmNetworks.Add(vMNetwork);
                    if (vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo == null) {
                        vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo =
                            new List<VMNetworkInfo>();
                    }
                    var vmnetInfo = new VMNetworkInfo {
                        VMNetworkID = vMNetwork.Id,
                        VMNetworkName = string.Empty,
                        VTNName = vtn.Name,
                        VMSubnetInfo = new List<VMSubnetInfo>(),
                        CreatedFrom = "ODL",
                        Description = "VM Network corresponding to this VTN is not created on SCVMM",
                        VMNetworkOriginalName = vMNetwork.Name
                    };
                    vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Add(
                        vmnetInfo);
                    foreach (Vbridge vbr in vtn.Vbridges) {
                        this.AddVMSubnet(vmnetInfo, vbr, vMNetwork, logicalNetworkDefinitionId);
                    }
                    ////--------VTN is added on ODL
                }
            }
        }
 /// <summary>
 /// Checks whether the change is VLAN ID is consistent or not.
 /// </summary>
 /// <param name="vmnetworkInfo">Corresponding VM network info.</param>
 /// <param name="txnMng">Transaction manager instance.</param>
 /// <param name="VtnHostName">Host name of the VTNCoordinator.</param>
 /// <param name="vmsubnetInfo">Corresponding VM subnet info.</param>
 /// <returns>Empty id the modification is consistent.</returns>
 private static string CheckVlanRange(VMNetworkInfo vmnetworkInfo, TransactionManager txnMng, string VtnHostName, VMSubnetInfo vmsubnetInfo)
 {
     string vlanIdRange = string.Empty;
     if (vmsubnetInfo.CreatedFrom.CompareTo("ODL") == 0) {
         return vlanIdRange;
     }
     vmsubnetInfo.CreatedFrom = "ODLModified";
     VLANIDMap vLANIDMap = new VLANIDMap(txnMng,
            VtnHostName,
            TransactionManager.OpenMode.ReadMode);
     vlanIdRange = vLANIDMap.GetVlanId(vmnetworkInfo.VMNetworkOriginalName, vmsubnetInfo.VMSubnetName);
     if (string.IsNullOrEmpty(vlanIdRange)) {
         return vlanIdRange;
     }
     bool isinRange = CheckVlanId(vlanIdRange, vmsubnetInfo.VBridgeVlanId);
     if (isinRange) {
         return string.Empty;
     } else {
         return vlanIdRange;
     }
 }
 /// <summary>
 /// Compare the VTN.
 /// </summary>
 /// <param name="logicalNetworkDefinitionId">Logfical network definition ID.</param>
 /// <param name="vmnetwork">Corresponding VM network.</param>
 /// <param name="vtn">VTN to compare.</param>
 /// <param name="vmnetworkInfo">Corresponding VM network info.</param>
 /// <param name="txnMng">Transaction manager instance.</param>
 /// <param name="VtnHostName">Host name of the VTNCoordinator.</param>
 /// <returns>Indicateds the need to refresh the network service.</returns>
 public bool SyncVTN(Guid? logicalNetworkDefinitionId, VMNetwork vmnetwork, Vtn vtn, VMNetworkInfo vmnetworkInfo, TransactionManager txnMng, string VtnHostName)
 {
     bool needRefresh = false;
     foreach (Vbridge vbr in vtn.Vbridges) {
         var vmsubnetInfo = vmnetworkInfo.VMSubnetInfo.FirstOrDefault(vmsubnet => vmsubnet.VBridgeName.Equals(vbr.Name));
         if (vmsubnetInfo != null) {
             if (vmsubnetInfo.VBridgeVlanId != vbr.VlanId && !string.IsNullOrEmpty(vmsubnetInfo.VMSubnetName)) {
                 string vlanRange = CheckVlanRange(vmnetworkInfo, txnMng, VtnHostName, vmsubnetInfo);
                 if (vlanRange.CompareTo(string.Empty) == 0) {
                     vmsubnetInfo.Description = string.Format("VLAN ID of this VM Subnet is changed from {0} to {1} on ODL. VM attached to this VM Subnet prior to this change (i.e, with VLAN ID {0}), will not be operable now.", vmsubnetInfo.VMSubnetVlanId, vbr.VlanId);
                     vmsubnetInfo.VMSubnetVlanId = vbr.VlanId;
                     var vmsubnet = vmnetwork.VMSubnets.FirstOrDefault(subnet => subnet.Id.Equals(vmsubnetInfo.VMSubnetID));
                     vmsubnet.SegmentId.PrimarySegmentIdentifier = Convert.ToUInt32(vbr.VlanId);
                     needRefresh = true;
                 } else {
                     vmsubnetInfo.Description = string.Format("VLAN ID of this VM Subnet is changed from {0} to {1} on ODL. But {1} does not lie in the specified range '{2}'. VM attached to this VM Subnet will not be operable now.", vmsubnetInfo.VMSubnetVlanId, vbr.VlanId, vlanRange);
                 }
             }
             vmsubnetInfo.VBridgeVlanId = vbr.VlanId;
         } else {
             this.AddVMSubnet(vmnetworkInfo, vbr, vmnetwork, logicalNetworkDefinitionId);
             needRefresh = true;
         }
     }
     foreach (var subnetInfo in vmnetworkInfo.VMSubnetInfo) {
         Vbridge vbr = vtn.Vbridges.FirstOrDefault(odlItem => odlItem.Name.CompareTo(subnetInfo.VBridgeName) == 0);
         if (vbr == null) {
             RemoveVMSubnetSync(null, subnetInfo.VMSubnetID, vmnetwork, vmnetworkInfo);
             needRefresh = true;
         }
     }
     return needRefresh;
 }
 /// <summary>
 /// Compare the VTN.
 /// </summary>
 /// <param name="vtn">VTN to compare.</param>
 /// <param name="vmnetworkInfo">Corresponding VM network info.</param>
 /// <returns>Indicates whether VTN is synchronized or not.</returns>
 public bool CompareVTN(Vtn vtn, VMNetworkInfo vmnetworkInfo)
 {
     bool needRefresh = false;
     List<Vbridge> vbridges = new List<Vbridge>();
     foreach (Vbridge vbr in vtn.Vbridges) {
         var vmsubnetInfo = vmnetworkInfo.VMSubnetInfo.FirstOrDefault(vmsubnet => vmsubnet.VBridgeName.Equals(vbr.Name));
         if (vmsubnetInfo != null) {
             if (vmsubnetInfo.VBridgeVlanId != vbr.VlanId) {
                 needRefresh = true;
             }
         } else {
             needRefresh = true;
         }
     }
     foreach (var subnetInfo in vmnetworkInfo.VMSubnetInfo) {
         Vbridge vbr = vtn.Vbridges.FirstOrDefault(odlItem => odlItem.Name.CompareTo(subnetInfo.VBridgeName) == 0);
         if (vbr == null) {
             needRefresh = true;
         }
     }
     return needRefresh;
 }
 /// <summary>
 /// Remove the VMSubnet corresponding to the removed vBridge from ODL.
 /// </summary>
 /// <param name="vMNetworkConfig">VMNetwork.config file instance.</param>
 /// <param name="vmSubnetId">Id of the VMSubnet.</param>
 /// <param name="vmnetwork">Parent VM Network.</param>
 /// <param name="vmnetworkInfo">Corresponding VM netrok info.</param>
 public static void RemoveVMSubnetSync(VMNetworkConfig vMNetworkConfig, Guid vmSubnetId, VMNetwork vmnetwork, VMNetworkInfo vmnetworkInfo)
 {
     if (vMNetworkConfig == null && (vmnetwork == null || vmnetworkInfo == null)) {
         ODLVSEMETW.EventWriteArgumentError(
             MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
             MethodBase.GetCurrentMethod().Name,
             "The parameter 'vMNetworkConfig' is null or invalid.");
         throw new ArgumentException("The parameter 'vMNetworkConfig' is null or invalid.");
     }
     if (vmnetwork == null) {
         vmnetwork = vMNetworkConfig.VMNetwork.VmNetworks.FirstOrDefault(net => net.VMSubnets.Any(vms => vms.Id.CompareTo(vmSubnetId) == 0));
     }
     if (vmnetwork != null) {
         var temp = vmnetwork.VMSubnets.ToList();
         temp.RemoveAll(net => net.Id.CompareTo(vmSubnetId) == 0);
         vmnetwork.VMSubnets = temp.ToArray();
         if (vmnetworkInfo == null) {
             vmnetworkInfo = vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.FirstOrDefault(vmn => vmn.VMNetworkID.CompareTo(vmnetwork.Id) == 0);
         }
         vmnetworkInfo.VMSubnetInfo.ForEach(vms => {
             if (vms.VMSubnetID.CompareTo(vmSubnetId) == 0) {
                 vms.Description = "Corresponding vBridge is deleted on ODL.";
                 vms.VBridgeName = string.Empty;
                 vms.VBridgeVlanId = 0;
             }
         });
     }
 }
 /// <summary>
 /// Remove the VMSubnet corresponding to the removed vBridge from ODL.
 /// </summary>
 /// <param name="vMNetworkConfig">VMNetwork.config file instance.</param>
 /// <param name="networkInfo">Corresponding VM netrok info.</param>
 public static void RemoveVMNetworkSync(VMNetworkConfig vMNetworkConfig, VMNetworkInfo networkInfo)
 {
     vMNetworkConfig.VMNetwork.VmNetworks.RemoveAll(net => net.Id.CompareTo(networkInfo.VMNetworkID) == 0);
     networkInfo.VTNName = string.Empty;
     networkInfo.Description = "Corresponding VTN is deleted on ODL.";
     networkInfo.VMSubnetInfo.ForEach(net => {
         net.VBridgeName = string.Empty; net.VBridgeVlanId = 0;
         net.Description = "Corresponding vBridge is deleted on ODL.";
     });
 }