summaryrefslogtreecommitdiff
path: root/haircontrol/data.py
blob: da85100274218a0dfd2b513847873eace64c0a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class EtherDomain:
    def __init__(self):
        self.equipments = {}
        self.ip2mac = {}
        self.mac2ip = {}
        self.gw = None
        self.root_iface = None

    def __repr__(self):
        return '([\n%s\n],\n%s\n)'%(',\n'.join('  %s'%repr(e) for e in self.get_equipment_list_sorted()), repr(self.ip2mac))

    def print_tree(self):
        self._print_tree(self.gw, self.root_iface, 0)

    def _print_tree(self,eq_root, iface, indent):
        print(" "*indent + eq_root.name + '(' + iface.name + ')')
        ei_pairs = []
        for iface in eq_root.ifaces.values():
            for ip in iface.direct_neighbours:
                ei_pairs.append( (self.equipments[ip], iface) )
        ei_pairs.sort(key=lambda x: x[0].name)

        for (e,i) in ei_pairs:
            self._print_tree(e, i, indent+1)

    def get_equipment_list_sorted(self):
        e_list = list(self.equipments.values())
        e_list.sort(key=lambda x: x.name)
        return e_list

    def add_equipment(self,e):
        old = self.equipments.get(e.mgmtip)
        if old:
            print("Warn : %s replaced by %s"%(old,e))
        self.equipments[e.mgmtip] = e

    def index_mac_ip(self,mac, ip):
        oldmac = self.ip2mac.get(ip)
        if oldmac:
            print("Warn : %s replaced by %s for %s"%(oldmac, mac, ip))
        oldip = self.mac2ip.get(mac)
        if oldip:
            print("Warn : %s replaced by %s for %s"%(oldip, ip, mac))
        self.ip2mac[ip] = mac
        self.mac2ip[mac] = ip

class Equipment:
    def __init__(self, name=None, mgmtip=None, first_ifname=None):
        self.name = name
        self.mgmtip = mgmtip
        self.ifaces = {}
        self.inspected = False
        if first_ifname:
            self.ifaces[first_ifname] = Interface(first_ifname, None)

    def __repr__(self):
        return repr( (self.name, self.mgmtip, list(self.ifaces.values())) )

    def add_iface(self,  ifname, mac):
        iface = self.ifaces.get(ifname)
        mac_lower = mac.lower()
        if iface:
            iface.mac = mac_lower
        else:
            self.ifaces[ifname] = Interface(ifname, mac_lower)

    def add_seen_mac(self, ifname, mac):
        iface = self.ifaces.get(ifname)
        mac_lower = mac.lower()
        if not iface:
            print("Warn : add_seen_mac(%s, %s) auto-create iface on %s"%(ifname, mac_lower, self.name))
            iface = Interface(ifname)
            self.ifaces[ifname] = iface
        iface.mac_seen.append(mac_lower)

class Interface:
    def __init__(self, name=None, mac=None):
        self.name = name
        self.mac = mac
        self.mac_seen = []
        self.direct_neighbours = []

    def __repr__(self):
        #return repr( ( self.mac, self.name, self.direct_neighbours, self.mac_seen ) )
        return repr( ( self.mac, self.name, self.direct_neighbours, '[ %i mac_seen ]'%len(self.mac_seen) ) )