Added null checks where missing and reduced use of try-catch blocks in favour of TryParse() methods.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2021-03-15 12:37:21 +00:00
parent abf4ae3f35
commit 5a91f615b2

View File

@ -66,39 +66,22 @@ namespace XenAPI
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return DEFAULT_NUM_VCPUS_ALLOWED;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='vcpus-max']");
if (int.TryParse(xn?.Attributes?["max"]?.Value, out var result))
return result;
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='vcpus-max']");
try
{
return Convert.ToInt32(xn.Attributes["max"].Value);
}
catch
{
return DEFAULT_NUM_VCPUS_ALLOWED;
}
return DEFAULT_NUM_VCPUS_ALLOWED;
}
public int MinVCPUs()
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return 1;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='vcpus-min']");
if (int.TryParse(xn?.Attributes?["min"]?.Value, out var result))
return result;
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='vcpus-min']");
if (xn == null || xn.Attributes == null)
return 1;
try
{
return Convert.ToInt32(xn.Attributes["min"].Value);
}
catch
{
return 1;
}
return 1;
}
public bool IsRunning()
@ -196,57 +179,33 @@ namespace XenAPI
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return DEFAULT_MEM_ALLOWED;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='memory-static-max']");
if (long.TryParse(xn?.Attributes?["max"]?.Value, out var result))
return result;
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='memory-static-max']");
try
{
return Convert.ToInt64(xn.Attributes["max"].Value);
}
catch
{
return DEFAULT_MEM_ALLOWED;
}
return DEFAULT_MEM_ALLOWED;
}
public int MaxVIFsAllowed()
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return DEFAULT_NUM_VIFS_ALLOWED;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@property='number-of-vifs']");
if (int.TryParse(xn?.Attributes?["max"]?.Value, out var result))
return result;
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@property='number-of-vifs']");
try
{
return Convert.ToInt32(xn.Attributes["max"].Value);
}
catch
{
return DEFAULT_NUM_VIFS_ALLOWED;
}
return DEFAULT_NUM_VIFS_ALLOWED;
}
public int MaxVBDsAllowed()
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return DEFAULT_NUM_VBDS_ALLOWED;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@property='number-of-vbds']");
if (int.TryParse(xn?.Attributes?["max"]?.Value, out var result))
return result;
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@property='number-of-vbds']");
try
{
return Convert.ToInt32(xn.Attributes["max"].Value);
}
catch
{
return DEFAULT_NUM_VBDS_ALLOWED;
}
return DEFAULT_NUM_VBDS_ALLOWED;
}
private XmlDocument GetRecommendations()
@ -423,67 +382,34 @@ namespace XenAPI
XmlDocument xd = GetRecommendations();
if (xd == null)
return true;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='allow-gpu-passthrough']");
if (int.TryParse(xn?.Attributes?["value"]?.Value, out var result))
return result != 0;
try
{
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='allow-gpu-passthrough']");
if (xn == null)
return true;
return
Convert.ToInt32(xn.Attributes["value"].Value) != 0;
}
catch
{
return true;
}
return true;
}
public bool HasSriovRecommendation()
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return false;
try
{
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='allow-network-sriov']");
if (xn == null || xn.Attributes == null)
return false;
return
Convert.ToInt32(xn.Attributes["value"].Value) != 0;
}
catch
{
return false;
}
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='allow-network-sriov']");
if (int.TryParse(xn?.Attributes?["value"]?.Value, out var result))
return result != 0;
return false;
}
public bool HasVendorDeviceRecommendation()
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return false;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='has-vendor-device']");
if (bool.TryParse(xn?.Attributes?["value"]?.Value, out var result))
return result;
try
{
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='has-vendor-device']");
if (xn == null || xn.Attributes == null)
return false;
return bool.Parse(xn.Attributes["value"].Value);
}
catch (Exception ex)
{
log.Error("Error parsing has-vendor-device on the template.", ex);
return false;
}
log.Error("Error parsing has-vendor-device on the template.");
return false;
}
/// <summary>Returns true if
@ -498,22 +424,11 @@ namespace XenAPI
XmlDocument xd = GetRecommendations();
if (xd == null)
return true;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='allow-vgpu']");
if (int.TryParse(xn?.Attributes?["value"]?.Value, out var result))
return result != 0;
try
{
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='allow-vgpu']");
if (xn == null || xn.Attributes == null)
return true;
return
Convert.ToInt32(xn.Attributes["value"].Value) != 0;
}
catch
{
return true;
}
return true;
}
#region Supported Boot Mode Recommendations
@ -532,21 +447,9 @@ namespace XenAPI
{
XmlDocument xd = GetRecommendations();
if (xd == null)
return string.Empty;
XmlNode xn = xd?.SelectSingleNode(@"restrictions/restriction[@field='" + fieldName + "']");
try
{
XmlNode xn = xd.SelectSingleNode(@"restrictions/restriction[@field='" + fieldName + "']");
if (xn == null || xn.Attributes == null || xn.Attributes["value"] == null)
return string.Empty;
return xn.Attributes["value"].Value;
}
catch
{
return string.Empty;
}
return xn?.Attributes?["value"]?.Value ?? string.Empty;
}
#endregion