ln.skyscanner/entities/Node.cs

117 lines
3.1 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 Newtonsoft.Json;
using LiteDB;
using ln.types.threads;
using ln.types.net;
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 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; }
[JsonIgnore]
public IEnumerable<NetworkInterface> Interfaces => SkyScanner.Instance.Entities.interfaceCollection.Select("NodeID", ID);
[JsonIgnore]
public IEnumerable<IPv4> IPAdresses => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.IP));
[JsonIgnore]
public IEnumerable<Network4> Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network));
private HashSet<URI> uris = new HashSet<URI>();
private Node()
{
}
public Node(IPv4 primaryIP)
{
PrimaryIP = primaryIP;
Name = PrimaryIP.ToString();
Created = DateTime.Now;
}
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));
}
public NetworkInterface GetInterface(String intfName)
{
return SkyScanner.Instance.Entities.interfaceCollection.Where(intf => intf.NodeID.Equals(ID) && 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);
}
}
}