// /** // * 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 Newtonsoft.Json; using ln.types.threads; using ln.types.net; using ln.types.odb.attributes; namespace ln.skyscanner.entities { public class Node { [DocumentID] 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; } public List Interfaces { get; private set; } = new List(); [JsonIgnore] public IEnumerable IPAdresses => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.IP)); [JsonIgnore] public IEnumerable Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network)).Distinct(); [JsonIgnore] public IEnumerable Subnets => SkyScanner.Instance.Entities.SubnetCollection.Query(Query.Equals("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 uris = new HashSet(); private HashSet checks = new HashSet(); 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(); return checks.Add(perfName); } public void RemoveURI(string scheme) { RemoveURI(FindURIs(scheme)); } public void RemoveURI(IEnumerable 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 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 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); } } }