ln.skyscanner/entities/Node.cs

142 lines
4.0 KiB
C#
Raw Normal View History

2019-03-11 09:00:07 +01:00
// /**
// * 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;
2019-03-13 08:20:53 +01:00
using ln.types;
using System.Collections.Generic;
2019-03-21 14:06:36 +01:00
using System.Linq;
2019-03-26 12:53:42 +01:00
using ln.types.odb;
using Newtonsoft.Json;
2019-03-29 08:55:09 +01:00
using ln.types.threads;
2019-04-01 15:17:41 +02:00
using ln.types.net;
2019-04-08 08:47:12 +02:00
using ln.types.odb.attributes;
2019-03-11 09:00:07 +01:00
namespace ln.skyscanner.entities
{
public class Node
{
2019-03-26 12:53:42 +01:00
[DocumentID]
2019-03-27 07:49:49 +01:00
public Guid ID { get; set; } = Guid.NewGuid();
2019-03-13 08:20:53 +01:00
2019-04-01 15:17:41 +02:00
public IPv4 PrimaryIP { get; set; }
2019-03-21 14:06:36 +01:00
public String PrimaryMac { get; set; }
2019-03-26 12:53:42 +01:00
2019-03-21 14:06:36 +01:00
public String Name { get; set; }
2019-06-28 10:01:30 +02:00
public String Comment { get; set; }
2019-03-13 08:20:53 +01:00
2019-03-21 14:06:36 +01:00
public DateTime Created { get; }
2019-03-26 12:53:42 +01:00
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; }
2019-04-08 08:47:12 +02:00
public CheckSeverity Severity { get; set; } = CheckSeverity.PLANNED;
2019-03-11 09:00:07 +01:00
2019-04-11 13:05:07 +02:00
public DeviceType DeviceType { get; set; }
2019-04-02 13:00:15 +02:00
public List<NetworkInterface> Interfaces { get; private set; } = new List<NetworkInterface>();
2019-04-03 09:19:37 +02:00
[JsonIgnore]
2019-04-02 01:25:44 +02:00
public IEnumerable<IPv4> IPAdresses => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.IP));
2019-04-03 09:19:37 +02:00
[JsonIgnore]
public IEnumerable<Network4> Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network)).Distinct();
[JsonIgnore]
2019-04-11 08:30:13 +02:00
public IEnumerable<Subnet> Subnets => SkyScanner.Instance.Entities.SubnetCollection.Query(Query.Equals<Network4>("Network", Networks.Select(net => net.Network)));
2019-03-21 14:06:36 +01:00
2019-04-08 08:47:12 +02:00
private string uniqueIdentity;
public String UniqueIdentity {
get {
if (uniqueIdentity == null)
uniqueIdentity = PrimaryIP.ToString();
return uniqueIdentity;
}
set => uniqueIdentity = value;
}
2019-04-04 00:50:53 +02:00
public String[] Checks => checks.ToArray();
2019-03-26 12:53:42 +01:00
private HashSet<URI> uris = new HashSet<URI>();
2019-04-04 00:50:53 +02:00
private HashSet<string> checks = new HashSet<string>();
2019-03-21 14:06:36 +01:00
private Node()
2019-03-11 09:00:07 +01:00
{
}
2019-03-13 08:20:53 +01:00
2019-04-02 01:25:44 +02:00
public Node(IPv4 primaryIP)
2019-03-11 09:00:07 +01:00
{
2019-03-13 08:20:53 +01:00
PrimaryIP = primaryIP;
2019-03-21 14:06:36 +01:00
Name = PrimaryIP.ToString();
Created = DateTime.Now;
}
2019-04-04 00:50:53 +02:00
public bool AddCheck(string perfName)
{
if (checks == null)
checks = new HashSet<string>();
return checks.Add(perfName);
}
2019-03-26 12:53:42 +01:00
public void RemoveURI(string scheme)
{
RemoveURI(FindURIs(scheme));
}
public void RemoveURI(IEnumerable<URI> uris)
2019-03-21 14:06:36 +01:00
{
2019-03-26 12:53:42 +01:00
foreach (URI uri in uris.ToArray())
RemoveURI(uri);
}
public void RemoveURI(URI uri)
{
foreach (URI u in uris.ToArray())
2019-03-21 14:06:36 +01:00
{
if (uri.Equals(u))
uris.Remove(u);
}
}
2019-03-26 12:53:42 +01:00
public void AddURI(URI uri)
2019-03-21 14:06:36 +01:00
{
uris.Add(uri);
}
2019-03-26 12:53:42 +01:00
public IEnumerable<URI> FindURIs(string scheme)
2019-03-21 14:06:36 +01:00
{
2019-04-12 00:52:55 +02:00
return uris.Where((u) => scheme.Equals(u.Scheme)).ToArray();
2019-03-11 09:00:07 +01:00
}
2019-03-26 12:53:42 +01:00
public NetworkInterface GetInterface(String intfName)
{
2019-04-03 09:19:37 +02:00
return Interfaces.Where(intf => intf.Name.Equals(intfName)).FirstOrDefault();
2019-03-26 12:53:42 +01:00
}
public bool HasInterface(string intfName)
{
return GetInterface(intfName) != null;
}
2019-04-01 07:48:27 +02:00
public override int GetHashCode()
{
return ID.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj is Node) && (ID.Equals((obj as Node).ID));
}
2019-04-01 15:17:41 +02:00
public override string ToString()
{
return String.Format("[Node ID={0} PrimaryIP={1} Name={2}]",ID,PrimaryIP,Name);
}
2019-03-11 09:00:07 +01:00
}
}