ln.skyscanner/entities/Node.cs

184 lines
6.2 KiB
C#

// /**
// * File: IPNode.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using ln.types;
using System.Collections.Generic;
using System.Linq;
using ln.types.odb;
using ln.types.threads;
using ln.types.net;
using ln.skyscanner.checks;
using ln.skyscanner.perfvalue;
using ln.json.attributes;
using ln.skyscanner.services;
namespace ln.skyscanner.entities
{
public class Node
{
public Guid ID { get; set; } = Guid.NewGuid();
public IPv4 PrimaryIP { get; set; }
public String PrimaryMac { get; set; }
public String Name { get; set; }
public String Comment { get; set; }
public DateTime Created { get; }
public DateTime LastUpdate { get; set; }
public URI[] URIs => uris.ToArray();
public GeoLocation Location { get; set; }
public string Vendor { get; set; }
public string Product { get; set; }
public string ProductLine { get; set; }
public CheckSeverity Severity { get; set; } = CheckSeverity.PLANNED;
public DeviceType DeviceType { get; set; }
[JSONMapping(Private = true)]
public List<NetworkInterface> Interfaces { get; private set; } = new List<NetworkInterface>();
[JSONMapping(Private = true)]
public IEnumerable<IPv4> IPAdresses => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.IP));
[JSONMapping(Private = true)]
public IEnumerable<Network4> Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network)).Distinct();
[JSONMapping(Private = true)]
public IEnumerable<Subnet> Subnets => SkyScanner.Instance.Entities.SubnetCollection.Query(Query.Equals<Network4>("Network", Networks.Select(net => net.Network)));
private string uniqueIdentity;
public String UniqueIdentity {
get {
if (uniqueIdentity == null)
uniqueIdentity = PrimaryIP.ToString();
return uniqueIdentity;
}
set => uniqueIdentity = value;
}
public String[] Checks => checks.ToArray();
private HashSet<URI> uris = new HashSet<URI>();
private HashSet<string> checks = new HashSet<string>();
private Dictionary<string, SkyCheckState> checkStates = new Dictionary<string, SkyCheckState>();
public SkyCheckState[] CheckStates => checkStates.Values.ToArray();
public string[] CheckStateNames => checkStates.Keys.ToArray();
//private Dictionary<string, PerformanceValue> performanceValues = new Dictionary<string, PerformanceValue>();
//public PerformanceValue[] PerformanceValues => performanceValues.Values.ToArray();
//public string[] PerformanceValueNames => performanceValues.Keys.ToArray();
private Node()
{
}
public Node(IPv4 primaryIP)
{
PrimaryIP = primaryIP;
Name = PrimaryIP.ToString();
Created = DateTime.Now;
}
public bool AddCheck(string perfName)
{
if (checks == null)
checks = new HashSet<string>();
return checks.Add(perfName);
}
public void RemoveURI(string scheme)
{
RemoveURI(FindURIs(scheme));
}
public void RemoveURI(IEnumerable<URI> uris)
{
foreach (URI uri in uris.ToArray())
RemoveURI(uri);
}
public void RemoveURI(URI uri)
{
foreach (URI u in uris.ToArray())
{
if (uri.Equals(u))
uris.Remove(u);
}
}
public void AddURI(URI uri)
{
uris.Add(uri);
}
public IEnumerable<URI> FindURIs(string scheme)
{
return uris.Where((u) => scheme.Equals(u.Scheme)).ToArray();
}
public NetworkInterface GetInterface(String intfName)
{
return Interfaces.Where(intf => intf.Name.Equals(intfName)).FirstOrDefault();
}
public bool HasInterface(string intfName)
{
return GetInterface(intfName) != null;
}
public bool ContainsCheckState(string name) => checkStates.ContainsKey(name);
public SkyCheckState GetCheckState(string name) => checkStates[name];
public void AddCheckState(SkyCheckState checkState)
{
checkStates[checkState.CheckName] = checkState;
}
//public bool ContainsPerformanceValue(String name) => performanceValues.ContainsKey(name);
//public PerformanceValue GetPerformanceValue(string name) => performanceValues[name];
//public void AddPerformanceValue(PerformanceValue performanceValue)
//{
// performanceValues[Name] = performanceValue;
//}
public void WritePerformanceValue(PerformanceValueService performanceValueService, String checkName, String perfName, double value, double wLower = Double.MinValue, double wUpper = Double.MaxValue, double cLower = Double.MinValue, double cUpper = Double.MaxValue)
{
string[] perfPath = new string[] { UniqueIdentity, checkName, perfName };
perfName = string.Join("/", perfPath);
SkyCheckState skyCheckState = GetCheckState(checkName);
PerformanceValue performanceValue = skyCheckState.GetPerformanceValue(perfName);
if (performanceValue == null)
{
performanceValue = new PerformanceValue(perfPath, wLower, wUpper, cLower, cUpper);
skyCheckState.AddPerformanceValue(performanceValue);
}
performanceValueService.WriteValue(performanceValue, value);
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj is Node) && (ID.Equals((obj as Node).ID));
}
public override string ToString()
{
return String.Format("[Node ID={0} PrimaryIP={1} Name={2}]",ID,PrimaryIP,Name);
}
}
}