/// <summary> /// This method is responsible to synchronize the network configuration between ODL and SCVMM. /// </summary> /// <param name="txnMng">TransactionManager instance.</param> public void SynchronizeVTNObjects(TransactionManager txnMng) { var JavaScriptSerializer = new JavaScriptSerializer(); JavaScriptSerializer.MaxJsonLength = int.MaxValue; StringBuilder json = new StringBuilder("\"txnMng\":" + JavaScriptSerializer.Serialize(txnMng)); 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."); } Controller odl = new Controller(this.ConnectionString, this.Credential); List<Vtn> vtns = odl.ReadVTNObjects(string.Empty); ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty); string VtnHostName = this.ConnectionString; if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) { VtnHostName = VtnHostName.Substring(8); } VtnHostName = VtnHostName.Split('.', ':').First(); var vMNetworkConfig = new VMNetworkConfig(VtnHostName); try { txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.WriteMode); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}\n{1}", configFileIOErrorValidationMessage, ex.Message)); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}", configFileIOErrorValidationMessage)); } var logicalNetworkConfig = new LogicalNetworkConfig(VtnHostName); try { txnMng.SetConfigManager(logicalNetworkConfig, TransactionManager.OpenMode.ReadMode); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "LogicalNetwork.config {0}\n{1}", configFileIOErrorValidationMessage, ex.Message)); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "LogicalNetwork.config {0}", configFileIOErrorValidationMessage)); } string logicalNetworkName = VSEMODLConstants.LOGICAL_NETWORK_NAME; logicalNetworkName += VtnHostName; var logicalNetwork = logicalNetworkConfig.LogicalNetworks.FirstOrDefault(logicalnw => logicalnw.Name.Equals(logicalNetworkName)); if (logicalNetwork == null) { ODLVSEMETW.EventWriteGetFabricNetworkDefinitionError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "Logical network '{0}' not found.", logicalNetworkName)); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Logical network definition '{0}' not found.", logicalNetworkName)); } Guid logicalNetworkId = logicalNetwork.Id; Guid logicalNetworkDefinitionId = logicalNetwork.LogicalNetworkDefinitions.First().Id; ////for creation. this.CompareVTNObjects(vtns, vMNetworkConfig, logicalNetworkId, logicalNetworkDefinitionId, txnMng, VtnHostName); ////for removal. this.CompareVMNetworkObjects(vtns, vMNetworkConfig); vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.LastModifiedTimeStamp = DateTime.Now; }
/// <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> private void CompareVMNetworkObjects(List<Vtn> vtns, VMNetworkConfig vMNetworkConfig) { foreach (var networkInfo in vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo) { Vtn vtn = vtns.FirstOrDefault(odlItem => odlItem.Name.CompareTo(networkInfo.VTNName) == 0); if (vtn == null) { RemoveVMNetworkSync(vMNetworkConfig, networkInfo); } else { foreach (var subnetInfo in networkInfo.VMSubnetInfo) { Vbridge vbr = vtn.Vbridges.FirstOrDefault(odlItem => odlItem.Name.CompareTo(subnetInfo.VBridgeName) == 0); if (vbr == null) { RemoveVMSubnetSync(vMNetworkConfig, subnetInfo.VMSubnetID, null, null); } } } } }
/// <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> /// Publish VM Network. /// </summary> /// <param name="txnMng">TransactionManager instance.</param> /// <param name="vMNetwork">VM Network.</param> /// <param name="operationType">Operation type performed on SCVMM.</param> /// <param name="connection">Connection object.</param> /// <returns>Boolean indicating whether network service refresh is needed..</returns> public bool PublishVMNetwork(TransactionManager txnMng, VMNetwork vMNetwork, NetworkEntityPublishType operationType, VSEMConnection connection) { bool needRefresh = false; var JavaScriptSerializer = new JavaScriptSerializer(); JavaScriptSerializer.MaxJsonLength = int.MaxValue; StringBuilder json = new StringBuilder("\"txnMng\":" + JavaScriptSerializer.Serialize(txnMng)); json.Append(" \"vMNetwork\":" + JavaScriptSerializer.Serialize(vMNetwork)); json.Append(" \"operationType\":" + JavaScriptSerializer.Serialize(operationType)); 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 (vMNetwork == null) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'vMNetwork' is null or invalid."); throw new ArgumentException("The parameter 'vMNetworkName' 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 vmnetworkFound = vMNetworkConfig.VMNetwork.VmNetworks.FirstOrDefault( nw => nw.Id == vMNetwork.Id); var vmnetworkMappigInfoFound = vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.FirstOrDefault( nw => nw.VMNetworkID == vMNetwork.Id); if (vmnetworkMappigInfoFound == null) { return needRefresh; } if (operationType != NetworkEntityPublishType.Delete) { vmnetworkMappigInfoFound.VMNetworkName = vMNetwork.Name; if (vmnetworkMappigInfoFound.VTNName.CompareTo(string.Empty) == 0) { return needRefresh; } vmnetworkFound.OwnerName = vMNetwork.OwnerName; vmnetworkFound.RoutingDomainId = vMNetwork.RoutingDomainId; if (operationType == NetworkEntityPublishType.Create) { foreach (var subnetinfo in vmnetworkMappigInfoFound.VMSubnetInfo) { subnetinfo.VMSubnetName = subnetinfo.VBridgeName; subnetinfo.VMSubnetVlanId = subnetinfo.VBridgeVlanId; } Controller odl = new Controller(this.ConnectionString, this.Credential); List<Vtn> vtns = odl.ReadVTNObjects(vmnetworkMappigInfoFound.VTNName); if (vtns.Count == 0) { needRefresh = true; vMNetworkConfig.VMNetwork.VmNetworks.Remove(vmnetworkFound); vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Remove(vmnetworkMappigInfoFound); } if (vtns.Count != 0) { needRefresh = this.SyncVTN(vmnetworkFound.VMSubnets.First().LogicalNetworkDefinitionId, vmnetworkFound, vtns.First(), vmnetworkMappigInfoFound, txnMng, VtnHostName); } } vmnetworkMappigInfoFound.Description = string.Empty; } else { if (vmnetworkMappigInfoFound.VTNName.CompareTo(string.Empty) == 0) { vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Remove(vmnetworkMappigInfoFound); return needRefresh; } vmnetworkMappigInfoFound.VMNetworkName = string.Empty; vmnetworkMappigInfoFound.VMSubnetInfo.ForEach(subnet => { subnet.VMSubnetName = string.Empty; subnet.VMSubnetVlanId = 0; }); vmnetworkMappigInfoFound.Description = "VM Network corresponding to this VTN is not created on SCVMM"; vmnetworkFound.Name = vmnetworkMappigInfoFound.VTNName; vmnetworkFound.RoutingDomainId = null; vmnetworkFound.OwnerName = string.Empty; } string output = "\"VMNetwork\":" + JavaScriptSerializer.Serialize(vmnetworkFound); string outputMappingInfo = "\"VMNetworkInfo\":" + JavaScriptSerializer.Serialize(vmnetworkMappigInfoFound); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, output); return needRefresh; }
/// <summary> /// Create a new VM network with specified parameters using VTN on demand. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <param name="LogicalNetworkConfig">Config file of logical network.</param> /// <param name="vMNetworkConfig">Config file of vm network.</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="ipSubnet">IP pools to use.</param> /// <param name="logicalNetworkDefinitionId">ID of logical network definition /// to associate with.</param> /// <param name="conn">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 CreateVMNetworkDefinitionforVtn( TransactionManager txnMng, LogicalNetworkConfig LogicalNetworkConfig, VMNetworkConfig vMNetworkConfig, string name, Guid vMNetworkId, long? maxNumberOfPorts, IPSubnet[] ipSubnet, Guid logicalNetworkDefinitionId, VSEMConnection conn, out string vtnName, out string vbrName) { ODLVSEMETW.EventWriteCreateVMsubNetwork(MethodBase.GetCurrentMethod().Name, "Creating VM SubNetwork."); 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 (LogicalNetworkConfig == null) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'LogicalNetworkConfig' is null or invalid."); throw new ArgumentException( "The parameter 'LogicalNetworkConfig' is null or invalid."); } 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 (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 (conn == null) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'conn' is null or invalid."); throw new ArgumentException("The parameter 'conn' is null or invalid."); } string VtnHostName = this.ConnectionString; if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) { VtnHostName = VtnHostName.Substring(8); } VtnHostName = VtnHostName.Split('.', ':').First(); if (vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo == null) { ODLVSEMETW.EventWriteProcessLibraryError(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.", vMNetworkId.ToString("B"))); } var vmNetInfo = vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo.Where( vmNw => vmNw.VMNetworkID == vMNetworkId).FirstOrDefault(); if (vmNetInfo == null) { ODLVSEMETW.EventWriteProcessLibraryError(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.", vMNetworkId.ToString("B"))); } vtnName = vmNetInfo.VTNName; vbrName = this.CreateUniqueNameForVBridge(name, vmNetInfo.VTNName); VLANIDMap vLANIDMap = new VLANIDMap(txnMng, VtnHostName, TransactionManager.OpenMode.ReadMode); string vlanIdRange = vLANIDMap.GetVlanId(vmNetInfo.VMNetworkOriginalName, name); if (string.IsNullOrEmpty(vlanIdRange)) { ODLVSEMETW.EventWriteFoundNoVlanError(MethodBase.GetCurrentMethod().Name, "No VLAN ID found."); throw new ItemNotFoundException("No VLAN ID found."); } long vlanId = this.SelectVlanId(vlanIdRange, vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo); if (vlanId == 0) { ODLVSEMETW.EventWriteFoundNoUnusedVlanError(MethodBase.GetCurrentMethod().Name, "No unused VLAN ID found."); throw new ItemNotFoundException("No unused VLAN ID found."); } VMSubnet vmNetworkDef = CreateVbrVmNetworkDefinition( vMNetworkId, maxNumberOfPorts, ipSubnet, name, logicalNetworkDefinitionId, vbrName, vlanId); if (vmNetworkDef == null) { ODLVSEMETW.EventWriteProcessFailedVMNetworkError( MethodBase.GetCurrentMethod().Name, "Failed to create VM Subnet."); throw new InvalidOperationException("Failed to create VM Subnet."); } Vbridge vBridge = new Vbridge(this.ConnectionString, this.Credential); ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty); Controller odl_ctr = new Controller(this.ConnectionString, this.Credential); if (odl_ctr.Get_status(Constants.CTR_NAME)) { vBridge.AddVbridge(vbrName, vmNetInfo.VTNName, vlanId); ODLVSEMETW.EventWriteReturnODLLibrary("Return from ODL Library.", string.Empty); } else { ODLVSEMETW.EventWriteProcessFailedVMNetworkError( MethodBase.GetCurrentMethod().Name, "Failed to create VBR controller status is down."); throw new InvalidOperationException("Failed to create VBR."); } var vmNet = vMNetworkConfig.VMNetwork.VmNetworks.Where(nwk => nwk.Id == vMNetworkId).FirstOrDefault(); Array.Resize(ref vmNet.VMSubnets, vmNet.VMSubnets.Length + 1); vmNet.VMSubnets[vmNet.VMSubnets.Length - 1] = vmNetworkDef; vmNetInfo.VMSubnetInfo.Add(new VMSubnetInfo { VBridgeName = vbrName, VMSubnetVlanId = vlanId, VMSubnetID = vmNetworkDef.Id, VMSubnetName = vmNetworkDef.Name, CreatedFrom = "SCVMM", VBridgeVlanId = vlanId }); return vmNetworkDef; }
/// <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."; }); }
/// <summary> /// This method is responsible for extracting the list of VSEMVmNetwork /// from the VSEM repository for the corresponding connection. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <param name="VtnHostName">Host name of the VTNCoordinator.</param> /// <returns>List of VSEMVmNetwork instances attached with the connection.</returns> public static VMNetworkMappingInfo GetVSEMVMNetworkMappingInfo(TransactionManager txnMng, string VtnHostName) { var JavaScriptSerializer = new JavaScriptSerializer(); JavaScriptSerializer.MaxJsonLength = int.MaxValue; StringBuilder json = new StringBuilder("\"TransactionManager\":" + JavaScriptSerializer.Serialize(txnMng)); ODLVSEMETW.EventWriteStartLibrary( MethodBase.GetCurrentMethod().Name, json.ToString()); ODLVSEMETW.EventWriteExtractVMNetworkInfolist( "Extracting list of VMNetwork Info.", string.Empty); if (string.IsNullOrWhiteSpace(VtnHostName)) { VSEMConfig vsemConfig = new VSEMConfig(); try { txnMng.SetConfigManager(vsemConfig, TransactionManager.OpenMode.ReadMode); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VSEM.config {0}\n", configFileIOErrorValidationMessage) + ex.Message); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "Either the NetworkService is not added in SCVMM or VSEM.config {0}", configFileIOErrorValidationMessage)); } VtnHostName = vsemConfig.Info.ServerName; } if (string.IsNullOrWhiteSpace(VtnHostName)) { throw new ArgumentException( "Parameter 'VTNCoordinatorHostName' is null or invalid."); } var vMNetworkConfig = new VMNetworkConfig(VtnHostName); try { txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.ReadMode); var ret = vMNetworkConfig.VMNetwork.VMNetworkMappingInformation; string output = "\"VMNetwork\":" + JavaScriptSerializer.Serialize(ret); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, output); return ret; } catch (System.IO.FileNotFoundException) { // Ignore if the file is not yet created and return empty list. return new VMNetworkMappingInfo(); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}\n{1}", configFileIOErrorValidationMessage, ex.Message)); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}", configFileIOErrorValidationMessage)); } }
/// <summary> /// This method is responsible for extracting the list of VSEMVmNetwork /// from the VSEM repository for the corresponding connection. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <returns>List of VSEMVmNetwork instances attached with the connection.</returns> public List<VMNetwork> GetVSEMVMNetwork(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()); ODLVSEMETW.EventWriteExtractVMNetworkInfolist( "Extracting list of VMNetwork Info.", string.Empty); string VtnHostName = this.ConnectionString; if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) { VtnHostName = VtnHostName.Substring(8); } VtnHostName = VtnHostName.Split('.', ':').First(); var vMNetworkConfig = new VMNetworkConfig(VtnHostName); try { txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.ReadMode); var ret = vMNetworkConfig.VMNetwork.VmNetworks; string output = "\"VMNetwork\":" + JavaScriptSerializer.Serialize(ret); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, output); return ret; } catch (System.IO.FileNotFoundException) { // Ignore if the file is not yet created and return empty list. return new List<VMNetwork>(); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}\n{1}", configFileIOErrorValidationMessage, ex.Message)); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}", configFileIOErrorValidationMessage)); } }
/// <summary> /// Update the specified IP Address Pool with the specified info. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <param name="name">Name of IP Address Pool.</param> /// <param name="description">Description for IP address Pool.</param> /// <param name="subnet">IP address subnet.</param> /// <param name="addressStart">Starting address for IP Address Pool.</param> /// <param name="addressEnd">Ending address for IP Address Pool.</param> /// <param name="gatewayInfo">Network gateway info.</param> /// <param name="conn">VSEM connection object.</param> /// <param name="ipAddressPoolId">IP address pool.</param> /// <returns>Updated IP address pool.</returns> public IPAddressPool UpdateIpAddressPool(TransactionManager txnMng, string name, string description, string subnet, string addressStart, string addressEnd, NetworkGatewayInfo[] gatewayInfo, VSEMConnection conn, Guid ipAddressPoolId) { var JavaScriptSerializer = new JavaScriptSerializer(); JavaScriptSerializer.MaxJsonLength = int.MaxValue; StringBuilder json = new StringBuilder("\"txnMng\":" + JavaScriptSerializer.Serialize(txnMng)); json.Append(" \"gatewayInfo\":" + JavaScriptSerializer.Serialize(gatewayInfo)); json.Append("\"name\":\"" + name + "\""); json.Append("\"description\":\"" + description + "\""); json.Append("\"subnet\":\"" + subnet + "\""); json.Append("\"addressStart\":\"" + addressStart + "\""); json.Append("\"addressEnd\":\"" + addressEnd + "\""); json.Append("\"ipAddressPoolId\":\"" + ipAddressPoolId + "\""); 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 (conn == null) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'conn' is null or invalid."); throw new ArgumentException("The parameter 'conn' is null or invalid."); } if (name != null) { if (string.IsNullOrWhiteSpace(name)) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'name' is invalid."); throw new ArgumentException( "The parameter 'name' is 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)); } VMSubnet vmNetDefFrom = null; IPAddressPool pool = null; foreach (var vmNet in vMNetworkConfig.VMNetwork.VmNetworks) { foreach (var vmNetDef in vmNet.VMSubnets) { if (vmNetDef.IPSubnets != null) { foreach (var ipsunet in vmNetDef.IPSubnets) { pool = ipsunet.IPAddressPools.FirstOrDefault(p => p.Id == ipAddressPoolId); if (pool != null) { vmNetDefFrom = vmNetDef; break; } } } if (vmNetDefFrom != null) { break; } } if (vmNetDefFrom != null) { break; } } if (vmNetDefFrom == null) { ODLVSEMETW.EventWriteValidateIPAddressPoolError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "IP Address Pool '{0}' not found.", ipAddressPoolId)); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "IP Address Pool '{0}' not found.", ipAddressPoolId)); } this.SyncVMSubnet(vmNetDefFrom.Id, vMNetworkConfig); if (pool != null) { // Update the IP Address Pool if (name != null) { pool.Name = name; } if (description != null) { pool.Description = description; } if (subnet != null) { pool.IPAddressSubnet = subnet; } if (addressStart != null) { pool.AddressRangeStart = addressStart; } if (addressEnd != null) { pool.AddressRangeEnd = addressEnd; } if (gatewayInfo != null) { pool.NetworkGateways = gatewayInfo; } string error = Validations.IsIPSubnetListValid( vmNetDefFrom.IPSubnets.ToList(), ipAddressPoolId); if (!string.IsNullOrEmpty(error)) { ODLVSEMETW.EventWriteValidateVMNetDefinitionError( MethodBase.GetCurrentMethod().Name, error); throw new ArgumentException(error); } } string output = "\"pool\":" + JavaScriptSerializer.Serialize(pool); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, output); return pool; }
/// <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 IP Address Pool. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <param name="id">ID of IP Address Pool to remove.</param> /// <param name="conn">Connecion object.</param> /// <returns>True if successful, else false.</returns> public bool RemoveIpAddressPool(TransactionManager txnMng, Guid id, VSEMConnection conn) { 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, string.Empty); ODLVSEMETW.EventWriteRemoveIpAddressPool(MethodBase.GetCurrentMethod().Name, "Removing IP Address pool."); 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 'txnMng' is null or invalid."); throw new ArgumentException("The parameter 'txnMng' is null or invalid."); } if (conn == 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."); } 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); foreach (var vmNet in vMNetworkConfig.VMNetwork.VmNetworks) { foreach (var vmNetDef in vmNet.VMSubnets) { if (vmNetDef.IPSubnets != null) { foreach (var ipsunet in vmNetDef.IPSubnets) { var pool = ipsunet.IPAddressPools.FirstOrDefault(p => p.Id == id); if (pool != null) { var pools = ipsunet.IPAddressPools.ToList(); pools.Remove(pool); ipsunet.IPAddressPools = pools.ToArray(); ipsunet.LastModifiedTimeStamp = DateTime.Now; string output = "\"output\":" + JavaScriptSerializer.Serialize(true); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, output); return true; } } } } } string outputLib = "\"output\":" + JavaScriptSerializer.Serialize(false); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, outputLib); return false; }
/// <summary> /// Create an IP Address Pool and add it to the specified VM Subnet. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <param name="name">Name of IP address pool.</param> /// <param name="description">Description for IP address pool.</param> /// <param name="subnet">IP address subnet.</param> /// <param name="addressStart">Starting address for IP address pool.</param> /// <param name="addressEnd">Ending address for IP address pool.</param> /// <param name="gatewayInfo">Network gateway info.</param> /// <param name="vmNetworkDefinitionId">VM Subnet ID.</param> /// <returns>Newly created ip address pool.</returns> public IPAddressPool CreateIpAddressPool(TransactionManager txnMng, string name, string description, string subnet, string addressStart, string addressEnd, NetworkGatewayInfo[] gatewayInfo, Guid vmNetworkDefinitionId) { var JavaScriptSerializer = new JavaScriptSerializer(); JavaScriptSerializer.MaxJsonLength = int.MaxValue; StringBuilder json = new StringBuilder("\"txnMng\":" + JavaScriptSerializer.Serialize(txnMng)); json.Append(" \"gatewayInfo\":" + JavaScriptSerializer.Serialize(gatewayInfo)); json.Append("\"name\":\"" + name + "\""); json.Append("\"description\":\"" + description + "\""); json.Append("\"subnet\":\"" + subnet + "\""); json.Append("\"addressStart\":\"" + addressStart + "\""); json.Append("\"addressEnd\":\"" + addressEnd + "\""); 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 (string.IsNullOrEmpty(subnet)) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'subnet' is null or invalid."); throw new ArgumentException("The parameter 'subnet' is null or invalid."); } if (string.IsNullOrEmpty(addressStart)) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'addressStart' is null or invalid."); throw new ArgumentException("The parameter 'addressStart' is null or invalid."); } if (string.IsNullOrEmpty(addressEnd)) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'addressEnd' is null or invalid."); throw new ArgumentException( "The parameter 'addressEnd' 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)); } VMSubnet vmNetDef = null; foreach (var vmNet in vMNetworkConfig.VMNetwork.VmNetworks) { vmNetDef = vmNet.VMSubnets.FirstOrDefault(vmSubNet => vmSubNet.Id == vmNetworkDefinitionId); if (vmNetDef != null) { break; } } if (vmNetDef == null) { ODLVSEMETW.EventWriteValidateVMNetDefinitionError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VM Subnet '{0}' not found.", vmNetworkDefinitionId.ToString("B"))); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "VM Subnet '{0}' not found.", vmNetworkDefinitionId.ToString("B"))); } this.SyncVMSubnet(vmNetworkDefinitionId, vMNetworkConfig); IPAddressPool poolAdd = null; if (vmNetDef != null) { poolAdd = this.CreateIpAddressPool(name, description, subnet, addressStart, addressEnd, gatewayInfo); if (poolAdd != null) { AddressFamily addressFamily = AddressFamily.IPv4; IPAddress address = null; var parts = subnet.Split('/'); var parsingResult = IPAddress.TryParse(parts[0], out address); if (parsingResult == false || address == null) { ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'subnet' is null or invalid."); throw new ArgumentException("The parameter 'subnet' is invalid."); } switch (address.AddressFamily) { case System.Net.Sockets.AddressFamily.InterNetwork: addressFamily = AddressFamily.IPv4; break; case System.Net.Sockets.AddressFamily.InterNetworkV6: addressFamily = AddressFamily.IPv6; break; default: ODLVSEMETW.EventWriteArgumentError( MethodBase.GetCurrentMethod().DeclaringType.Name.ToString(), MethodBase.GetCurrentMethod().Name, "The parameter 'subnet' is null or invalid."); throw new ArgumentException("The IP address family of subnet is invalid."); } IPSubnet ipsub = null; if (vmNetDef.IPSubnets != null) { ipsub = vmNetDef.IPSubnets.FirstOrDefault(x => x.AddressFamily == addressFamily); } else { vmNetDef.IPSubnets = new IPSubnet[0]; } if (ipsub == null) { IPSubnet ipSubnet = new IPSubnet(); ipSubnet.Id = Guid.NewGuid(); poolAdd.IPSubnetId = ipSubnet.Id; ipSubnet.IPAddressPools = new IPAddressPool[] { poolAdd}; ipSubnet.AddressFamily = addressFamily; ipSubnet.LastModifiedTimeStamp = DateTime.Now; ipSubnet.Subnet = subnet; ipSubnet.SupportsDHCP = true; List<IPSubnet> subnets = vmNetDef.IPSubnets.ToList(); subnets.Add(ipSubnet); vmNetDef.IPSubnets = subnets.ToArray(); } else { List<IPAddressPool> pools = ipsub.IPAddressPools.ToList(); pools.Add(poolAdd); poolAdd.IPSubnetId = ipsub.Id; ipsub.IPAddressPools = pools.ToArray(); ipsub.LastModifiedTimeStamp = DateTime.Now; } vmNetDef.LastModifiedTimeStamp = DateTime.Now; string error = Validations.IsIPSubnetListValid( vmNetDef.IPSubnets.ToList(), poolAdd.Id); if (!string.IsNullOrEmpty(error)) { ODLVSEMETW.EventWriteValidateVMNetDefinitionError( MethodBase.GetCurrentMethod().Name, error); throw new ArgumentException(error); } } } return poolAdd; }
/// <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> /// 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> /// 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; }
/// <summary> /// Remove completely the entities removed on SCVMM after refresh network service. /// </summary> /// <param name="scvmsubnets">Collection of VMSubnets on SCVMM.</param> /// <param name="txnMng">Transaction manager instance.</param> private void HandleVMSubnetsDependencies(List<string> scvmsubnets, TransactionManager txnMng) { string VtnHostName = this.ConnectionString; if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) { VtnHostName = VtnHostName.Substring(8); } VtnHostName = VtnHostName.Split('.', ':').First(); var vMNetworkConfig = new VMNetworkConfig(VtnHostName); try { txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.WriteMode); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}\n{1}", configFileIOErrorValidationMessage, ex.Message)); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}", configFileIOErrorValidationMessage)); } List<string> vsemvmsubnets = new List<string>(); var vmnetworkInfo = vMNetworkConfig.VMNetwork.VMNetworkMappingInformation.VMNetworkInfo; vmnetworkInfo.ForEach(vmn => vmn.VMSubnetInfo.ForEach(vms => { if (vms.VBridgeName.CompareTo(string.Empty) == 0) { vsemvmsubnets.Add(vms.VMSubnetID.ToString()); } })); List<string> vmsubnetsDeleted = vsemvmsubnets.Except(scvmsubnets).ToList(); vmnetworkInfo.ForEach(vmn => vmn.VMSubnetInfo.RemoveAll(vms => vmsubnetsDeleted.Contains(vms.VMSubnetID.ToString()))); vmnetworkInfo.RemoveAll(vmn => vmn.VMSubnetInfo.Count == 0); vmnetworkInfo.ForEach(vmn => { vmn.VMSubnetInfo.ForEach(vms => { if (vms.VBridgeName.CompareTo(string.Empty) == 0) { vms.Description = "VM Subnet could not be deleted on SCVMM due to dependent resources.\nEither one or more VM Subnets associated with the corresponding VM Network have dependent resources such as VM templates, virtual network adapters etc. Or corresponding VM Network has dependent resources such as VM templates, virtual network adapters etc."; } }); if (vmn.VTNName.CompareTo(string.Empty) == 0) { vmn.Description = "VM Network could not be deleted on SCVMM due to dependent resources.\nIt has dependent resources such as VM templates, virtual network adapters etc."; } }); }
/// <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> /// This method is responsible for extracting the VSEMVmNetwork /// from the VSEM repository for the corresponding connection for the specified id. /// </summary> /// <param name="txnMng">Transaction manager.</param> /// <param name="id">Id of the VSEMVmNetwork.</param> /// <returns>VSEMVmNetwork instance attached with the connection.</returns> public VMNetwork GetVSEMVMNetworkById(TransactionManager txnMng, Guid id) { var JavaScriptSerializer = new JavaScriptSerializer(); JavaScriptSerializer.MaxJsonLength = int.MaxValue; StringBuilder json = new StringBuilder("\"TransactionManager\":" + JavaScriptSerializer.Serialize(txnMng)); json.Append(" \"id\":" + id.ToString("B")); ODLVSEMETW.EventWriteStartLibrary( MethodBase.GetCurrentMethod().Name, json.ToString()); 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."); } ODLVSEMETW.EventWriteExtractVMNetworkInfo( "Extracting VMNetwork Info.", string.Empty); string VtnHostName = this.ConnectionString; if (VtnHostName.StartsWith(@"https://", StringComparison.Ordinal)) { VtnHostName = VtnHostName.Substring(8); } VtnHostName = VtnHostName.Split('.', ':').First(); var vMNetworkConfig = new VMNetworkConfig(VtnHostName); try { txnMng.SetConfigManager(vMNetworkConfig, TransactionManager.OpenMode.ReadMode); var ret = vMNetworkConfig.VMNetwork.VmNetworks.FirstOrDefault( nw => nw.Id == id); string output = "\"VMNetwork\":" + JavaScriptSerializer.Serialize(ret); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, output); return ret; } catch (System.IO.FileNotFoundException) { // Ignore if the file is not yet created and return empty list. return new VMNetwork(); } catch (Exception ex) { ODLVSEMETW.EventWriteConfigManagerFileIOError( MethodBase.GetCurrentMethod().Name, string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}\n{1}", configFileIOErrorValidationMessage, ex.Message)); ODLVSEMETW.EventWriteEndLibrary(MethodBase.GetCurrentMethod().Name, string.Empty); throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, "VMNetwork.config {0}", configFileIOErrorValidationMessage)); } }
/// <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; }