/// <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;
 }
Exemplo n.º 3
0
        /// <summary>
        /// This method is responsible for making the connection with ODL through
        /// http request and retrieve the list of VTNs.
        /// </summary>
        /// <param name="readVTN">Vtn name.</param>
        /// <returns>Extracts list of vtns from the response and return.</returns>
        public List<Vtn> ReadVTNObjects(string readVTN)
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            ODLVSEMETW.EventWriteStartODLLibrary(
                MethodBase.GetCurrentMethod().Name,
                string.Empty);
            List<Vtn> vtns = new List<Vtn>();
            List<string> vtnNames = this.GetVtnList();
            if (!string.IsNullOrEmpty(readVTN)) {
                vtnNames.RemoveAll(vtn => vtn.CompareTo(readVTN) != 0);
            }
            foreach (string vtnName in vtnNames) {
                var vtn = new Vtn(this.Base_uri, this.Credential) {
                    Name = vtnName
                };
                List<string> vbrNames = this.GetVbridgesListforVtn(vtnName);
                foreach (string vbrName in vbrNames) {
                    long vlanID = this.GetVLANIDforVbridge(vtnName, vbrName);
                    if (vlanID != 0) {
                        var vbr = new Vbridge(this.Base_uri, this.Credential) {
                            Name = vbrName,
                            VlanId = vlanID
                        };
                        vtn.Vbridges.Add(vbr);
                    }
                }
                if (vtn.Vbridges.Count() > 0) {
                    vtns.Add(vtn);
                }
            }

            string output = "\"vtns\":" + JavaScriptSerializer.Serialize(vtns.Select(vtn => vtn.Name));

            ODLVSEMETW.EventWriteEndODLLibrary(MethodBase.GetCurrentMethod().Name, output);
            return vtns;
        }
        /// <summary>
        /// Remove the specified vtn.
        /// </summary>
        /// <param name="vtnName">VTN name.</param>
        public void RemoveVmNetwork(string vtnName)
        {
            StringBuilder json = new StringBuilder("\"vtnName\":\"" + vtnName + "\"");
            ODLVSEMETW.EventWriteStartLibrary(MethodBase.GetCurrentMethod().Name,
                json.ToString());
            ODLVSEMETW.EventWriteRemoveVMNetwork(MethodBase.GetCurrentMethod().Name,
                    "Removing VTN.");
            if (string.IsNullOrWhiteSpace(vtnName)) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'vtnName' is null or invalid.");
                throw new ArgumentException("The parameter 'vtnName' is null or invalid.");
            }

            Vtn vtn = new Vtn(this.ConnectionString, this.Credential);
            vtn.RemoveVtn(vtnName);
            ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.",
                string.Empty);
        }
        /// <summary>
        /// Remove the specified VM network.
        /// </summary>
        /// <param name="vMNetworkConfig">Vm network config.</param>
        /// <param name="vmNetId">ID of VM network to remove.</param>
        public void RemoveVmNetwork(VMNetworkConfig vMNetworkConfig, Guid vmNetId)
        {
            var JavaScriptSerializer = new JavaScriptSerializer();
            JavaScriptSerializer.MaxJsonLength = int.MaxValue;
            StringBuilder json = new StringBuilder(" \"vMNetworkConfig\":" + JavaScriptSerializer.Serialize(vMNetworkConfig));
            json.Append("\"vmNetId\":\"" + vmNetId + "\"");
            ODLVSEMETW.EventWriteStartLibrary(MethodBase.GetCurrentMethod().Name,
                json.ToString());
            ODLVSEMETW.EventWriteRemoveVMNetwork(MethodBase.GetCurrentMethod().Name,
                    "Removing VMNetwork.");
            if (vMNetworkConfig == 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 (vmNetId == Guid.Empty) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'vmNetId' is null or invalid.");
                throw new ArgumentException("The parameter 'vmNetId' is null or invalid.");
            }
            string vtnName = string.Empty;
            foreach (var vmNet in
                vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo) {
                if (vmNet.VMNetworkID == vmNetId) {
                    vtnName = vmNet.VTNName;
                    break;
                }
            }

            Vtn vtn = new Vtn(this.ConnectionString, this.Credential);
            try {
                vtn.RemoveVtn(vtnName);
            } catch (System.Net.WebException) {
                //// If VSEM succeeded in vBridge deletion,
                //// there will be no problem in further operations.
                //// Garbage VTN will be abandoned.
            }
            ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty);
            var vmNwkInfo =
                vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo;

            if (vmNwkInfo.First(vmNet => vmNet.VMNetworkID == vmNetId).VMSubnetInfo.Count == 0) {
                vmNwkInfo.RemoveAll(
                    vmNet => vmNet.VMNetworkID == vmNetId);
            } else {
                vmNwkInfo.First(vmNet => vmNet.VMNetworkID == vmNetId).Description =
                    "Corresponding VTN is deleted on ODL";
            }

            vMNetworkConfig.VMNetwork.VmNetworks.RemoveAll(
                vmNet => vmNet.Id == vmNetId);
            ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty);
        }
        /// <summary>
        /// Create a new VM network with specified parameters using VTN on demand.
        /// </summary>
        /// <param name="txnMng">Transaction manager.</param>
        /// <param name="vMSubnetName">Name of VM Subnet to create.</param>
        /// <param name="vMNetworkName">Name 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>
        /// <returns>VM network if successful, else null.</returns>
        public VMNetwork CreateVMNetwork(TransactionManager txnMng,
            string vMSubnetName,
            string vMNetworkName,
            long? maxNumberOfPorts,
            IPSubnet[] ipSubnets,
            Guid logicalNetworkDefinitionId,
            VSEMConnection connection,
            out string vtnName)
        {
            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("\"vMNetworkName\":\"" + vMNetworkName + "\"");
            json.Append("\"vMSubnetName\":\"" + vMSubnetName + "\"");
            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 'transaction' is null or invalid.");
                throw new ArgumentException("The parameter 'transaction' is null or invalid.");
            }

            if (string.IsNullOrEmpty(vMNetworkName)) {
                ODLVSEMETW.EventWriteArgumentError(
                    MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(),
                    MethodBase.GetCurrentMethod().Name,
                    "The parameter 'vMNetworkName' is null or invalid.");
                throw new ArgumentException("The parameter 'vMNetworkName' 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);

            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 logicalNetName = VSEMODLConstants.LOGICAL_NETWORK_NAME;
            logicalNetName += VtnHostName;
            var fabricNwDef = LogicalNetworkConfig.LogicalNetworks.First(
                log => log.Name.Equals(logicalNetName)).LogicalNetworkDefinitions.FirstOrDefault(
                fabnw => fabnw.Id == logicalNetworkDefinitionId);
            if (fabricNwDef == 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")));
            }

            ODLVSEMETW.EventWriteReturnLibrary(string.Format(CultureInfo.CurrentCulture,
                "VSEMLogicalNetworkDefinition is retrieved by ID: {0}",
                logicalNetworkDefinitionId.ToString("B")),
                string.Empty);

            if (!fabricNwDef.SupportsVMNetworkProvisioning) {
                ODLVSEMETW.EventWriteSupportsVMNetworkProvisioningError(
                    "Logical network does not support VM network creation.",
                    string.Empty);
                throw new InvalidOperationException(
                    "Logical network does not support VM network creation.");
            }

            vtnName = this.CreateUniqueNameForVTN(vMNetworkName);

            Vtn vtn = new Vtn(this.ConnectionString, this.Credential);
            vtn.AddVtn(vtnName);

            ODLVSEMETW.EventWriteReturnODLLibrary(string.Format(CultureInfo.CurrentCulture,
                "VTN '{0}' is created",
                vtnName),
                string.Empty);

            VMNetwork vmNet = CreateVtnVmNetwork(vMNetworkName);
            vmNet.LogicalNetwork = fabricNwDef.LogicalNetworkId;
            if (vmNet == null) {
                ODLVSEMETW.EventWriteProcessFailedVMNetworkError(
                    MethodBase.GetCurrentMethod().Name,
                    "Failed to create VM Network.");
                throw new InvalidOperationException("Failed to create VM Network.");
            }
            ODLVSEMETW.EventWriteSuccessVmNetwork(MethodBase.GetCurrentMethod().Name,
                    "VM Network Successfully Created.");

            // Create the VM Subnet
            vMNetworkConfig.VMNetwork.VmNetworks.Add(vmNet);
            if (vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo == null) {
                vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo =
                    new List<VMNetworkInfo>();
            }

            vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Add(
                new VMNetworkInfo {
                    VMNetworkID = vmNet.Id,
                    VMNetworkName = vmNet.Name,
                    VTNName = vtnName,
                    VMSubnetInfo = new List<VMSubnetInfo>(),
                    CreatedFrom = "SCVMM",
                    VMNetworkOriginalName = vmNet.Name
                });

            var vsemvmnetworkDefinition = new VSEMVMSubnetManagement(
                this.ConnectionString, this.Credential);
            ODLVSEMETW.EventWriteCreateVMsubNetwork(MethodBase.GetCurrentMethod().Name,
                    "VM Sub Network creation process started.");
            string vbrName = string.Empty;
            VMSubnet vmNetworkDef =
                vsemvmnetworkDefinition.CreateVMNetworkDefinitionforVtn(
                txnMng,
                LogicalNetworkConfig,
                vMNetworkConfig,
                vMSubnetName,
                vmNet.Id,
                maxNumberOfPorts,
                ipSubnets,
                logicalNetworkDefinitionId,
                connection,
                out vtnName,
                out vbrName);

            Controller odl = new Controller(this.ConnectionString, this.Credential);
            odl.UpdateStartupConfiguration();
            string output = "\"vmNet\":" + JavaScriptSerializer.Serialize(vmNet);
            ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", output);

            ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name,
                output);
            return vmNet;
        }