CP-36392: Refactored so that GetHashCode() does not call the homonymous base class method.

This required refactoring the OVF.AddVirtualSystem() method to accept the
system ID as a parameter. Also, removed some unused methods that were
calling OVF.AddVirtualSystem().

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2022-04-05 22:24:11 +01:00
parent 177c47f79f
commit ec33caf4b5
6 changed files with 20 additions and 348 deletions

View File

@ -175,7 +175,7 @@ namespace XenAdmin.Wizards.CrossPoolMigrateWizard
continue;
vmsFromSelection.Add(vm);
m_vmMappings.Add(item.XenObject.opaque_ref, new VmMapping { VmNameLabel = item.XenObject.Name() });
m_vmMappings.Add(item.XenObject.opaque_ref, new VmMapping(item.XenObject.opaque_ref) {VmNameLabel = item.XenObject.Name()});
}
HasTemplatesOnly = vmsFromSelection.Count > 0 && vmsFromSelection.All(vm => vm.is_a_template);

View File

@ -209,7 +209,7 @@ namespace XenAdmin.Wizards.ImportWizard
m_vmMappings.Clear();
string[] sysIds = OVF.FindSystemIds(_selectedOvfPackage.OvfEnvelope);
foreach (string sysId in sysIds)
m_vmMappings.Add(sysId, new VmMapping {VmNameLabel = FindVMName(_selectedOvfPackage.OvfEnvelope, sysId)});
m_vmMappings.Add(sysId, new VmMapping(sysId) {VmNameLabel = FindVMName(_selectedOvfPackage.OvfEnvelope, sysId)});
m_pageHost.SelectedOvfEnvelope = _selectedOvfPackage.OvfEnvelope;
m_pageHost.SetDefaultTarget(m_pageHost.ChosenItem ?? m_selectedObject);
@ -262,7 +262,9 @@ namespace XenAdmin.Wizards.ImportWizard
}
else if (type == typeof(ImageVMConfigPage))
{
var newMapping = new VmMapping
var sysId = Guid.NewGuid().ToString();
var newMapping = new VmMapping(sysId)
{
VmNameLabel = m_pageVMconfig.VmName,
CpuCount = m_pageVMconfig.CpuCount,
@ -284,14 +286,13 @@ namespace XenAdmin.Wizards.ImportWizard
if (!newMapping.Equals(oldMapping))
{
m_envelopeFromVhd = OVF.CreateOvfEnvelope(newMapping.VmNameLabel,
m_envelopeFromVhd = OVF.CreateOvfEnvelope(sysId, newMapping.VmNameLabel,
newMapping.CpuCount, newMapping.Memory,
newMapping.BootParams, newMapping.PlatformSettings,
newMapping.Capacity,
m_pageImportSource.FilePath, m_pageImportSource.ImageLength, BrandManager.ProductBrand);
m_vmMappings.Clear();
var sysId = OVF.FindSystemIds(m_envelopeFromVhd).First();
m_vmMappings.Add(sysId, newMapping);
m_pageHost.VmMappings = m_vmMappings;

View File

@ -61,7 +61,8 @@ namespace XenAdmin.Actions.OvfActions
#region CREATE ENVELOPE / ADD VIRTUAL SYSTEM
EnvelopeType ovfEnv = OVF.CreateEnvelope(m_applianceFileName);
string vsId = OVF.AddVirtualSystem(ovfEnv, vm.name_label);
string vsId = Guid.NewGuid().ToString();
OVF.AddVirtualSystem(ovfEnv, vm.name_label, vsId);
string vhsId = OVF.AddVirtualHardwareSection(ovfEnv, vsId);
#endregion

View File

@ -36,8 +36,11 @@ namespace XenAdmin.Mappings
{
public class VmMapping
{
public VmMapping()
private readonly string _id;
public VmMapping(string id)
{
_id = id;
Storage = new Dictionary<string, SR>();
StorageToAttach = new Dictionary<string, VDI>();
Networks = new Dictionary<string, XenAPI.Network>();
@ -93,7 +96,7 @@ namespace XenAdmin.Mappings
public override int GetHashCode()
{
return base.GetHashCode();
return _id.GetHashCode();
}
}
}

View File

@ -153,317 +153,10 @@ namespace XenOvf
}
}
public EnvelopeType ConvertPhysicaltoOVF(DiskInfo[] vhdExports, string pathToOvf, string ovfName)
{
return ConvertPhysicaltoOVF(vhdExports, pathToOvf, ovfName, LANGUAGE);
}
public EnvelopeType ConvertPhysicaltoOVF(DiskInfo[] vhdExports, string pathToOvf, string ovfName, string lang)
{
CollectInformation();
var env = CreateEnvelope(ovfName, lang);
string vmUuid = AddVirtualSystem(env, lang, ovfName);
string vhsId = AddVirtualHardwareSection(env, vmUuid, lang);
AddVssd(env, vmUuid, vhsId);
AddCPUs(env, vmUuid);
AddMemory(env, vmUuid);
AddNetworks(env, vmUuid, lang);
CreateConnectedDevices(env, vmUuid, vhdExports);
#region CREATE PVP ENTRIES
foreach (DiskInfo di in vhdExports)
{
string pvpFilename = string.Format(@"{0}.pvp", Path.GetFileNameWithoutExtension(di.VhdFileName));
string pvpPathWithFilename = Path.Combine(pathToOvf, pvpFilename);
if (File.Exists(pvpPathWithFilename))
{
AddExternalFile(env, pvpFilename, null);
}
}
#endregion
FinalizeEnvelope(env);
log.DebugFormat("OVF.Create completed, {0}", ovfName);
return env;
}
/// <summary>
/// Convert Hyper-V Export CIM XML vm meta data to OVF xml
/// </summary>
/// <param name="hvxmlFileName">filename</param>
/// <param name="ovfName">ovfname</param>
/// <param name="lang">language</param>
/// <returns>ovf xml string</returns>
public string ConvertHyperVtoOVF(string hvxmlFileName, string ovfName, string lang)
{
string hvxml = Tools.LoadFile(hvxmlFileName);
hvxml = hvxml.Replace("utf-16", "utf-8"); // fails if we don't do this.
string xmlstring = null;
Ms_Declarations_Type hvobj = Tools.Deserialize<Ms_Declarations_Type>(hvxml);
if (hvobj != null &&
hvobj.declgroups != null &&
hvobj.declgroups.Count > 0 &&
hvobj.declgroups[0].values != null &&
hvobj.declgroups[0].values.Count > 0)
{
EnvelopeType env = ConvertFromHyperVXml(hvobj, ovfName, lang);
xmlstring = Tools.Serialize(env, typeof(EnvelopeType), Tools.LoadNamespaces());
log.Debug("XenOvf::ConvertHyperVtoOVF completed");
}
else
{
throw new InvalidDataException(Messages.CONVERSION_NO_DATA);
}
return xmlstring;
}
#endregion
#region PRIVATE
private EnvelopeType ConvertFromHyperVXml(Ms_Declarations_Type hvobj, string ovfname, string lang)
{
EnvelopeType env = CreateEnvelope(ovfname, lang);
string systemId = AddVirtualSystem(env, lang, ovfname);
string vhsid = AddVirtualHardwareSection(env, systemId, lang);
foreach (Ms_WrapperInstance_Type wrap in hvobj.declgroups[0].values)
{
RASD_Type rasd = new RASD_Type();
switch (wrap.instance.className)
{
#region CASE: Msvm_VirtualSystemSettingData
case "Msvm_VirtualSystemSettingData":
{
string ElementName = null;
string InstanceId = null;
string SystemName = null;
string VirtualSystemType = null;
foreach (Ms_Property_Base_Type prop in wrap.instance.Properties)
{
switch (prop.Name)
{
case "ElementName":
{
ElementName = ((Ms_ParameterValue_Type)prop).Value;
break;
}
case "InstanceID":
{
InstanceId = ((Ms_ParameterValue_Type)prop).Value;
break;
}
case "SystemName":
{
SystemName = ((Ms_ParameterValue_Type)prop).Value;
break;
}
case "VirtualSystemType":
{
VirtualSystemType = ((Ms_ParameterValue_Type)prop).Value;
break;
}
}
}
AddVirtualSystemSettingData(env, systemId, vhsid, ElementName, ElementName, ElementName, InstanceId, VirtualSystemType);
UpdateVirtualSystemName(env, systemId, lang, ElementName);
break;
}
#endregion
#region CASE: ResourceAllocationSettingData
case "Msvm_ProcessorSettingData":
case "Msvm_MemorySettingData":
case "Msvm_SyntheticEthernetPortSettingData":
case "Msvm_ResourceAllocationSettingData":
{
foreach (Ms_Property_Base_Type prop in wrap.instance.Properties)
{
if (prop is Ms_ParameterValue_Type)
{
if (((Ms_ParameterValue_Type)prop).Value == null ||
((Ms_ParameterValue_Type)prop).Value.Length <= 0)
{
continue;
}
}
else if (prop is Ms_ParameterValueArray_Type)
{
if (((Ms_ParameterValueArray_Type)prop).Values == null ||
((Ms_ParameterValueArray_Type)prop).Values.Length <= 0)
{
continue;
}
}
PropertyInfo[] properties = rasd.GetType().GetProperties();
foreach (PropertyInfo pi in properties)
{
if (pi.Name.ToLower().Equals(prop.Name.ToLower()))
{
object newvalue = null;
if (prop is Ms_ParameterValue_Type)
{
switch (prop.Type.ToLower())
{
case "string":
{
newvalue = new cimString((string)((Ms_ParameterValue_Type)prop).Value);
break;
}
case "boolean":
{
newvalue = new cimBoolean();
((cimBoolean)newvalue).Value = Convert.ToBoolean(((Ms_ParameterValue_Type)prop).Value);
break;
}
case "uint16":
{
newvalue = new cimUnsignedShort();
((cimUnsignedShort)newvalue).Value = Convert.ToUInt16(((Ms_ParameterValue_Type)prop).Value);
break;
}
case "uint32":
{
newvalue = new cimUnsignedInt();
((cimUnsignedInt)newvalue).Value = Convert.ToUInt32(((Ms_ParameterValue_Type)prop).Value);
break;
}
case "uint64":
{
newvalue = new cimUnsignedLong();
((cimUnsignedLong)newvalue).Value = Convert.ToUInt64(((Ms_ParameterValue_Type)prop).Value);
break;
}
}
}
else if (prop is Ms_ParameterValueArray_Type)
{
switch (prop.Type.ToLower())
{
case "string":
{
List<cimString> sarray = new List<cimString>();
foreach (Ms_ParameterValue_Type svalue in ((Ms_ParameterValueArray_Type)prop).Values)
{
sarray.Add(new cimString(svalue.Value));
}
newvalue = sarray.ToArray();
break;
}
}
}
object tmpobject = null;
switch (pi.Name.ToLower())
{
case "caption":
{
newvalue = new Caption(((cimString)newvalue).Value);
break;
}
case "changeabletype":
{
tmpobject = newvalue;
newvalue = new ChangeableType();
((ChangeableType)newvalue).Value = ((cimUnsignedShort)tmpobject).Value;
break;
}
case "consumervisibility":
{
tmpobject = newvalue;
newvalue = new ConsumerVisibility();
((ConsumerVisibility)newvalue).Value = ((cimUnsignedShort)tmpobject).Value;
break;
}
case "mappingbehavior":
{
tmpobject = newvalue;
newvalue = new MappingBehavior();
((MappingBehavior)newvalue).Value = ((cimUnsignedShort)tmpobject).Value;
break;
}
case "resourcetype":
{
tmpobject = newvalue;
newvalue = new ResourceType();
((ResourceType)newvalue).Value = ((cimUnsignedShort)tmpobject).Value;
break;
}
case "connection":
case "hostresource":
default:
{
break;
}
}
pi.SetValue(rasd, newvalue, null);
}
}
}
if (rasd != null)
{
if (FillEmptyRequiredFields(rasd))
{
if (rasd.ResourceType.Value == 21 &&
rasd.Caption.Value.ToLower().StartsWith(_ovfrm.GetString("RASD_19_CAPTION").ToLower()))
{
string filename = Path.GetFileName(rasd.Connection[0].Value);
AddFileReference(env, lang, filename, rasd.InstanceID.Value, 0, WIN_FILE_FORMAT_URI);
AddRasd(env, systemId, rasd);
}
else if (rasd.ResourceType.Value == 10)
{
AddNetwork(env, systemId, lang, rasd.InstanceID.Value, rasd.Caption.Value, rasd.Address.Value);
}
else
{
AddRasd(env, systemId, rasd);
}
}
}
break;
}
#endregion
#region CASE: Msvm_VLANEndpointSettingData
case "Msvm_VLANEndpointSettingData":
#endregion
#region CASE: SKIPPED / DEFAULT
case "Msvm_VirtualSystemExportSettingData":
case "Msvm_VirtualSystemGlobalSettingData":
case "Msvm_HeartbeatComponentSettingData":
case "Msvm_KvpExchangeComponentSettingData":
case "Msvm_ShutdownComponentSettingData":
case "Msvm_TimeSyncComponentSettingData":
case "Msvm_VssComponentSettingData":
case "Msvm_SwitchPort":
case "Msvm_VirtualSwitch":
default:
{
break;
}
#endregion
}
}
FinalizeEnvelope(env);
return env;
}
private void CollectInformation()
{
Win32_ComputerSystem = null;

View File

@ -1305,32 +1305,26 @@ namespace XenOvf
return vhs;
}
public static string AddVirtualSystem(EnvelopeType ovfObj, string ovfname)
{
return AddVirtualSystem(ovfObj, LANGUAGE, ovfname);
}
/// <summary>
/// Add a Virtual System Section to OVF
/// MUST be done at least once.
/// </summary>
/// <param name="ovfObj">object of type EnvelopeType</param>
/// <param name="lang">Language</param>
/// <param name="ovfname">Name of the OVF</param>
/// <param name="sysId">the unique id of the virtual system</param>
/// <param name="lang">Language</param>
/// <returns>InstanceId of Virtual System</returns>
public static string AddVirtualSystem(EnvelopeType ovfObj, string lang, string ovfname)
public static void AddVirtualSystem(EnvelopeType ovfObj, string ovfname, string sysId, string lang = LANGUAGE)
{
VirtualSystem_Type vs = new VirtualSystem_Type();
vs.id = Guid.NewGuid().ToString();
VirtualSystem_Type vs = new VirtualSystem_Type {id = sysId};
string info = _ovfrm.GetString("VIRTUAL_SYSTEM_TYPE_INFO");
vs.Info = new Msg_Type(AddToStringSection(ovfObj, lang, info), info);
vs.Name = new Msg_Type[1] { new Msg_Type(AddToStringSection(ovfObj, lang, ovfname), ovfname) };
vs.Name = new[] {new Msg_Type(AddToStringSection(ovfObj, lang, ovfname), ovfname)};
AddVirtualSystem(ovfObj, vs);
AddOperatingSystemSection(ovfObj, vs.id, lang, null, null);
log.Debug("OVF.AddVirtualSystem(obj,lang,ovfname) completed");
return vs.id;
}
public static void AddVirtualSystem(EnvelopeType ovfEnv, VirtualSystem_Type vs)
@ -1391,12 +1385,12 @@ namespace XenOvf
#region CREATEs
public static EnvelopeType CreateOvfEnvelope(string vmName, ulong cpuCount, ulong memory,
public static EnvelopeType CreateOvfEnvelope(string systemID, string vmName, ulong cpuCount, ulong memory,
string bootParams, string platformSettings, ulong capacity,
string diskPath, ulong imageLength, string productBrand)
{
EnvelopeType env = CreateEnvelope(vmName);
string systemID = AddVirtualSystem(env, vmName);
AddVirtualSystem(env, vmName, systemID);
string hdwareSectionId = AddVirtualHardwareSection(env, systemID);
string guid = Guid.NewGuid().ToString();
@ -1465,26 +1459,6 @@ namespace XenOvf
return env;
}
public EnvelopeType Create(DiskInfo[] vhdExports, string pathToOvf, string ovfName)
{
return Create(vhdExports, pathToOvf, ovfName, LANGUAGE);
}
/// <summary>
/// Create an OVF (xml string) from local system.
/// *** Element[0] of VHDExports MUST be the BOOT Disk ***
/// DiskInfo.VHDFileName == The name the VHD will have after export.
/// DiskInfo.DriveId == "PHYSICALDRIVE0","PHYSICALDRIVE1"...
/// </summary>
/// <param name="vhdExports">LIST of Names to the VHD Files to be created REQUIREMENTS: 1. Element[0] MUST be the boot device</param>
/// <param name="pathToOvf"></param>
/// <param name="lang"></param>
/// <param name="ovfName">Name of the OVF Package</param>
/// <returns>xml string representing the OVF</returns>
public EnvelopeType Create(DiskInfo[] vhdExports, string pathToOvf, string ovfName, string lang)
{
return ConvertPhysicaltoOVF(vhdExports, pathToOvf, ovfName, lang);
}
public static EnvelopeType CreateEnvelope(string ovfName)
{
return CreateEnvelope(ovfName, LANGUAGE);