|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +from future.utils import iteritems |
| 5 | +import sys |
| 6 | +import lxml.etree |
| 7 | + |
| 8 | +iface_link_map = {} |
| 9 | +link_members = {} |
| 10 | +node_ifaces = {} |
| 11 | +link_netmasks = {} |
| 12 | +allifaces = {} |
| 13 | + |
| 14 | +f = open(sys.argv[1],'r') |
| 15 | +contents = f.read() |
| 16 | +f.close() |
| 17 | +root = lxml.etree.fromstring(contents) |
| 18 | + |
| 19 | +mycluster = None |
| 20 | +if len(sys.argv) > 2: |
| 21 | + mycluster = sys.argv[2] |
| 22 | + |
| 23 | +# Find all the links: |
| 24 | +for elm in root.getchildren(): |
| 25 | + if not elm.tag.endswith("}link"): |
| 26 | + continue |
| 27 | + name = elm.get("client_id") |
| 28 | + ifacerefs = [] |
| 29 | + cluster = None |
| 30 | + for elm2 in elm.getchildren(): |
| 31 | + if elm2.tag.endswith("}interface_ref"): |
| 32 | + ifacename = elm2.get("client_id") |
| 33 | + ifacerefs.append(ifacename) |
| 34 | + if elm2.tag.endswith("}label") and elm2.get("name") == "cluster": |
| 35 | + cluster = elm2.text |
| 36 | + if not mycluster or not cluster or mycluster == cluster: |
| 37 | + for ifacename in ifacerefs: |
| 38 | + iface_link_map[ifacename] = name |
| 39 | + link_members[name] = ifacerefs |
| 40 | + |
| 41 | +# Find all the node interfaces |
| 42 | +for elm in root.getchildren(): |
| 43 | + if not elm.tag.endswith("}node"): |
| 44 | + continue |
| 45 | + name = elm.get("client_id") |
| 46 | + ifaces = {} |
| 47 | + cluster = None |
| 48 | + for elm2 in elm.getchildren(): |
| 49 | + if elm2.tag.endswith("}interface"): |
| 50 | + ifacename = elm2.get("client_id") |
| 51 | + for elm3 in elm2.getchildren(): |
| 52 | + if not elm3.tag.endswith("}ip"): |
| 53 | + continue |
| 54 | + if not elm3.get("type") == 'ipv4': |
| 55 | + continue |
| 56 | + addrtuple = (elm3.get("address"),elm3.get("netmask")) |
| 57 | + ifaces[ifacename] = addrtuple |
| 58 | + allifaces[ifacename] = addrtuple |
| 59 | + break |
| 60 | + if elm2.tag.endswith("}label") and elm2.get("name") == "cluster": |
| 61 | + cluster = elm2.text |
| 62 | + if not mycluster or not cluster or mycluster == cluster: |
| 63 | + for (k,v) in iteritems(ifaces): |
| 64 | + allifaces[k] = v |
| 65 | + node_ifaces[name] = ifaces |
| 66 | + |
| 67 | +# Dump the nodes a la topomap |
| 68 | +print("# nodes: vname,links") |
| 69 | +for n in node_ifaces.keys(): |
| 70 | + for (i,(addr,mask)) in iteritems(node_ifaces[n]): |
| 71 | + print("%s,%s:%s" % (n,iface_link_map[i],addr)) |
| 72 | + |
| 73 | +# Dump the links a la topomap -- but with fixed cost of 1 |
| 74 | +print("# lans: vname,mask,cost") |
| 75 | +for m in link_members.keys(): |
| 76 | + ifref = link_members[m][0] |
| 77 | + (ip,mask) = allifaces[ifref] |
| 78 | + print("%s,%s,1" % (m,mask)) |
| 79 | + |
| 80 | +sys.exit(0) |
0 commit comments