def superLink(vrfcidr, supercidr): """Link a vrfcidr to a Supernet""" time = nglib.get_time() results = nglib.py2neo_ses.cypher.execute( 'MATCH (sn:Supernet {cidr:{supercidr}})' + '<-[e:SUPER]-(n:Network {vrfcidr:{vrfcidr}}) RETURN e', vrfcidr=vrfcidr, supercidr=supercidr) # Make a new Supernet Link if len(results) == 0: logger.info("New: Creating %s -[SUPER]-> %s Link", vrfcidr, supercidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (sn:Supernet {cidr:{supercidr}}), (n:Network {vrfcidr:{vrfcidr}}) ' + 'CREATE (sn)<-[e:SUPER {time:{time}}]-(n) RETURN e', vrfcidr=vrfcidr, supercidr=supercidr, time=time) else: logger.debug("Super Exists: %s -[SUPER]-> %s Link", vrfcidr, supercidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (sn:Supernet {cidr:{supercidr}})<-[e:SUPER]-' + '(n:Network {vrfcidr:{vrfcidr}}) SET e.time={time} RETURN e', vrfcidr=vrfcidr, supercidr=supercidr, time=time)
def import_supernet(snet): """Import a Supernet Entry""" time = nglib.get_time() cidr = snet['cidr'] desc = snet['description'] role = snet['role'] secure = snet['secure'] results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Supernet {cidr:{cidr}}) RETURN n', cidr=cidr) # Insert new supernet if len(results) == 0: logger.info("New: Inserting Supernet: " + cidr) results = nglib.py2neo_ses.cypher.execute( 'CREATE (n:Supernet {cidr:{cidr}, name:{cidr}, desc:{desc}, role:{role}, ' + 'secure:{secure}, time:{time}}) RETURN n', cidr=cidr, role=role, desc=desc, secure=secure, time=time) # Update existing Supernet else: logger.debug("Supernet Exists, updating: " + cidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Supernet {cidr:{cidr}}) ' + 'SET n += {desc:{desc}, role:{role}, secure:{secure}, time:{time}} RETURN n', cidr=cidr, role=role, desc=desc, secure=secure, time=time)
def link_router_to_vrf(router, vrf): """Create a link from a router to a VRF (called from net_update now)""" time = nglib.get_time() result = nglib.py2neo_ses.cypher.execute( 'MATCH( s:Switch:Router {name:{router}})<-[r:VRF_ON]-' + '(v:VRF {name:{vrf}}) RETURN r', router=router, vrf=vrf) if len(result) == 0: logger.info("New: Creating VRF_ON Relationship %s to %s", router, vrf) nglib.py2neo_ses.cypher.execute( 'MATCH (r:Switch:Router {name:{router}}), (v:VRF {name:{vrf}}) ' + 'CREATE (r)<-[e:VRF_ON {time:{time}}]-(v)', router=router, vrf=vrf, time=time) else: logger.debug("Debug: %s already linked to VRF %s, updating timestamp", router, vrf) result = nglib.py2neo_ses.cypher.execute( 'MATCH( s:Switch:Router {name:{router}})<-[r:VRF_ON]-(v:VRF {name:{vrf}}) ' + 'SET r += {time:{time}} RETURN r', router=router, vrf=vrf, time=time)
def clear_edges(hours): """ Clear Expired Edges Notes: nglib.verbose returns edges to delete but does not delete """ logger.info("Clearing Edges older than " + str(hours) + " hours") # Time shifted datetime age = nglib.get_time(hours=hours) edges = nglib.py2neo_ses.cypher.execute( 'MATCH ()-[e]->() WHERE e.time < {age} RETURN e', age=age) if len(edges) > 0: for e in edges: neighbors = getRelationship(e.e) logger.info("Expired Edge: " + neighbors) count = nglib.py2neo_ses.cypher.execute( 'MATCH ()-[e]->() WHERE e.time < {age} RETURN count(e) as count', age=age) if nglib.verbose: logger.info("Expired Edges: " + str(count[0].count)) else: logger.info("Deleting Edges: " + str(count[0].count)) nglib.py2neo_ses.cypher.execute( 'MATCH ()-[e]->() WHERE e.time < {age} DELETE e', age=age)
def clear_nodes(hours): """ Clear Expired Nodes Notes: verbose returns nodes to delete but does not delete """ logger.info("Finding Nodes to Clear older than " + str(hours) + " hours") # Time shifted datetime age = nglib.get_time(hours=hours) nodes = nglib.py2neo_ses.cypher.execute( 'MATCH (n) WHERE n.time < {age} RETURN n', age=age) if len(nodes) > 0: for r in nodes: label = getLabel(r.n) pj = getJSONProperties(r.n) logger.info("Expired Node: " + label + pj['name']) count = nglib.py2neo_ses.cypher.execute( 'MATCH (n) WHERE n.time < {age} RETURN count(n) as count', age=age) logger.info("Expired Nodes: " + str(count[0].count)) if not nglib.verbose: logger.info("Deleting Nodes: " + str(count[0].count)) nglib.py2neo_ses.cypher.execute( 'MATCH (n)-[e]-() WHERE n.time < {age} DELETE e', age=age) nglib.py2neo_ses.cypher.execute( 'MATCH (n) WHERE n.time < {age} DELETE n', age=age)
def link_vlan_switch(en): """Link a VLAN to a Switch (Bolt Driver)""" time = nglib.get_time() vname = en['MGMT'] + "-" + en['VID'] desc = en['VName'] switch = en['Switch'] stp = en['STP'] results = nglib.bolt_ses.run( 'MATCH (n:VLAN {name:{vname}})-[e:Switched]->(s:Switch {name:{switch}}) ' + 'RETURN n.name AS name', {"vname": vname, "switch": switch}) # Check for Results FIXME try: name = next(iter(results)) logger.debug("Updating: VLAN (%s)-[:Switched]->(%s) Relationship", vname, switch) except: logger.info("New: VLAN (%s)-[:Switched]->(%s) Relationship", vname, switch) nglib.bolt_ses.run( 'MATCH (v:VLAN {name:{vname}}), (s:Switch {name:{switch}}) ' + 'MERGE (v)-[e:Switched]->(s) SET e += {desc:{desc}, stp:{stp}, time:{time}} RETURN e', {"vname": vname, "switch": switch, "desc": desc, "stp": stp, "time": time})
def update_bridge(pmgmt, cmgmt, vlan, pswitch, cswitch): """Insert or Update a VLAN BRIDGE""" if nglib.verbose > 2: print("Bridge: ", pmgmt, cmgmt, vlan, pswitch, cswitch) pvlan = pmgmt + "-" + vlan cvlan = cmgmt + "-" + vlan time = nglib.get_time() # See if a Bridge Exists results = nglib.py2neo_ses.cypher.execute( 'MATCH (pv:VLAN {name:{pvlan}})-[e:BRIDGE]-(cv:VLAN {name:{cvlan}}) RETURN e', pvlan=pvlan, cvlan=cvlan) if len(results) == 0: logger.info("New: Bridge (%s)-[:BRIDGE]->(%s) Relationship", pvlan, cvlan) nglib.py2neo_ses.cypher.execute( 'MATCH (pv:VLAN {name:{pvlan}}), (cv:VLAN {name:{cvlan}}) ' + 'CREATE (pv)-[e:BRIDGE {pswitch:{pswitch}, cswitch:{cswitch}, time:{time}}]' + '->(cv) RETURN e', pvlan=pvlan, cvlan=cvlan, pswitch=pswitch, cswitch=cswitch, time=time) else: logger.debug("Updating VLAN %s-[:BRIDGE]-%s Relationship", pvlan, cvlan) results = nglib.py2neo_ses.cypher.execute( 'MATCH (pv:VLAN {name:{pvlan}})-[e:BRIDGE]-(cv:VLAN {name:{cvlan}}) ' + 'SET e += {time:{time}} RETURN e', pvlan=pvlan, cvlan=cvlan, pswitch=pswitch, cswitch=cswitch, time=time)
def link_vlan_to_root(vname, stp, rootSwitch): """Create a VLAN -[ROOT]-> Switch Relationship""" root = nglib.py2neo_ses.cypher.execute( 'MATCH (v:VLAN {name:{vname}})-[e:ROOT]-(s:Switch {name:{rootSwitch}}) RETURN e', vname=vname, rootSwitch=rootSwitch) time = nglib.get_time() # Create New Root Relationship if len(root) == 0: logger.info("New: Root for VLAN (%s)-[:ROOT]->(%s)", vname, rootSwitch) nglib.py2neo_ses.cypher.execute( 'MATCH (v:VLAN {name:{vname}}),(s:Switch {name:{rootSwitch}}) ' + 'CREATE (v)-[e:ROOT {stp:{stp}, time:{time}}]->(s) RETURN e', vname=vname, rootSwitch=rootSwitch, stp=stp, time=time) # Update existing else: logger.debug("Updating Root for VLAN (%s)-[:ROOT]->(%s)", vname, rootSwitch) nglib.py2neo_ses.cypher.execute( 'MATCH (v:VLAN {name:{vname}})-[e:ROOT]->(s:Switch {name:{rootSwitch}}) ' + 'SET e += {stp:{stp}, time:{time}} RETURN e', vname=vname, rootSwitch=rootSwitch, stp=stp, time=time)
def superLink(vrfcidr, supercidr): """Link a vrfcidr to a Supernet""" time = nglib.get_time() results = nglib.py2neo_ses.cypher.execute( 'MATCH (sn:Supernet {cidr:{supercidr}})' + '<-[e:SUPER]-(n:Network {vrfcidr:{vrfcidr}}) RETURN e', vrfcidr=vrfcidr, supercidr=supercidr) # Make a new Supernet Link if len(results) == 0: logger.info("New: Creating %s -[SUPER]-> %s Link", vrfcidr, supercidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (sn:Supernet {cidr:{supercidr}}), (n:Network {vrfcidr:{vrfcidr}}) ' + 'CREATE (sn)<-[e:SUPER {time:{time}}]-(n) RETURN e', vrfcidr=vrfcidr, supercidr=supercidr, time=time) else: logger.debug("Super Exists: %s -[SUPER]-> %s Link", vrfcidr, supercidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (sn:Supernet {cidr:{supercidr}})<-[e:SUPER]-' + '(n:Network {vrfcidr:{vrfcidr}}) SET e.time={time} RETURN e', vrfcidr=vrfcidr, supercidr=supercidr, time=time)
def clear_edges(hours): """ Clear Expired Edges Notes: nglib.verbose returns edges to delete but does not delete """ logger.info("Clearing Edges older than " + str(hours) + " hours") # Time shifted datetime age = nglib.get_time(hours=hours) edges = nglib.py2neo_ses.cypher.execute( 'MATCH ()-[e]->() WHERE e.time < {age} RETURN e', age=age) if len(edges) > 0: for e in edges: neighbors = getRelationship(e.e) logger.info("Expired Edge: " + neighbors) count = nglib.py2neo_ses.cypher.execute( 'MATCH ()-[e]->() WHERE e.time < {age} RETURN count(e) as count', age=age) if nglib.verbose: logger.info("Expired Edges: " + str(count[0].count)) else: logger.info("Deleting Edges: " + str(count[0].count)) nglib.py2neo_ses.cypher.execute( 'MATCH ()-[e]->() WHERE e.time < {age} DELETE e', age=age)
def import_mgmt_vlan(vdb, ignore_new): """Collate all MGMT-VID pairs, insert nodes and link to MgmtGroup""" vuniq = dict() time = nglib.get_time() for en in vdb: vname = en['MGMT'] + "-" + en['VID'] vuniq[vname] = 1 for en in vuniq.keys(): vname = en (mgmt, vid) = vname.split('-') vid = str(vid) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:VLAN {name:{vname}}) RETURN n', vname=vname) # Add new VLAN if len(results) == 0: logger.info("New: Inserting VLAN %s", en) results = nglib.py2neo_ses.cypher.execute( 'CREATE (v:VLAN {name:{vname}, vid:{vid}, mgmt:{mgmt}, time:{time}}) RETURN v', vname=vname, vid=vid, mgmt=mgmt, time=time) # Record New Network Unless Ignoring initial run if not ignore_new: # Store a NewVLAN Object for alerting nglib.py2neo_ses.cypher.execute( 'CREATE (v:NewVLAN {name:{vname}, time:{time}}) RETURN v', vname=vname, time=time) # Else update record else: logger.debug("Updating VLAN %s", vname) nglib.py2neo_ses.cypher.execute( 'MATCH (v:VLAN {name:{vname}}) SET v += ' + '{vid:{vid}, mgmt:{mgmt}, time:{time}} RETURN v', vname=vname, vid=vid, mgmt=mgmt, time=time)
def import_mgmt_vlan(vdb, ignore_new): """Collate all MGMT-VID pairs, insert nodes and link to MgmtGroup""" vuniq = dict() time = nglib.get_time() for en in vdb: vname = en['MGMT'] + "-" + en['VID'] vuniq[vname] = 1 for en in vuniq.keys(): vname = en (mgmt, vid) = vname.split('-') vid = str(vid) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:VLAN {name:{vname}}) RETURN n', vname=vname) # Add new VLAN if len(results) == 0: logger.info("New: Inserting VLAN %s", en) results = nglib.py2neo_ses.cypher.execute( 'CREATE (v:VLAN {name:{vname}, vid:{vid}, mgmt:{mgmt}, time:{time}}) RETURN v', vname=vname, vid=vid, mgmt=mgmt, time=time) # Record New Network Unless Ignoring initial run if not ignore_new: # Store a NewVLAN Object for alerting nglib.py2neo_ses.cypher.execute( 'CREATE (v:NewVLAN {name:{vname}, time:{time}}) RETURN v', vname=vname, time=time) # Else update record else: logger.debug("Updating VLAN %s", vname) nglib.py2neo_ses.cypher.execute( 'MATCH (v:VLAN {name:{vname}}) SET v += ' + '{vid:{vid}, mgmt:{mgmt}, time:{time}} RETURN v', vname=vname, vid=vid, mgmt=mgmt, time=time)
def import_neighbors(fileName): """Find if neighbors are adjacent, if so, links them""" logger.info("Importing Neighbors from " + fileName) f = open(fileName) ndb = csv.DictReader(f) time = nglib.get_time() for en in ndb: localName = en['LocalName'] localPort = en['LocalPort'] remoteName = en['RemoteName'] remotePort = en['RemotePort'] # Exclude Management Ports exPorts = '(mgmt|FastEthernet)' if re.search(exPorts, localPort) or re.search(exPorts, remotePort): logger.debug("Skipping NEI: " + remoteName) else: if nglib.verbose > 2: print("Debug importNeighbors", localName, localPort, remoteName, remotePort) checkLocal = nglib.py2neo_ses.cypher.execute( 'MATCH (s:Switch {name:{name}}) RETURN ' + 's.distance AS distance, s.seed AS seed', name=localName) checkRemote = nglib.py2neo_ses.cypher.execute( 'MATCH (s:Switch {name:{name}}) RETURN s.distance AS distance, s.seed AS seed', name=remoteName) # Found two neighbors, import if len(checkLocal) and len(checkRemote): # Get Distances localD = checkLocal.records[0].distance remoteD = checkRemote.records[0].distance import_adjacent_neighbors(en, localD, remoteD, time)
def clear_nodes(hours): """ Clear Expired Nodes Notes: verbose returns nodes to delete but does not delete """ logger.info("Finding Nodes to Clear older than " + str(hours) + " hours") # Time shifted datetime age = nglib.get_time(hours=hours) nodes = nglib.py2neo_ses.cypher.execute( 'MATCH (n) WHERE n.time < {age} RETURN n', age=age) if len(nodes) > 0: for r in nodes: label = getLabel(r.n) pj = getJSONProperties(r.n) logger.info("Expired Node: " + label + pj['name']) count = nglib.py2neo_ses.cypher.execute( 'MATCH (n) WHERE n.time < {age} RETURN count(n) as count', age=age) logger.info("Expired Nodes: " + str(count[0].count)) if not nglib.verbose: logger.info("Deleting Nodes: " + str(count[0].count)) nglib.py2neo_ses.cypher.execute( 'MATCH (n)-[e]-() WHERE n.time < {age} DELETE e', age=age) nglib.py2neo_ses.cypher.execute( 'MATCH (n) WHERE n.time < {age} DELETE n', age=age)
def import_single_vrf(vrf, seczone=0, desc=None): """Import a single VRF in to the database""" time = nglib.get_time() result = nglib.py2neo_ses.cypher.execute( 'MATCH (v:VRF {name:{vrf}}) RETURN v', vrf=vrf) # Insert new VRF if len(result) == 0: logger.info("Creating new VRF: " + vrf) nglib.py2neo_ses.cypher.execute( 'CREATE (v:VRF {name:{vrf}, seczone:{seczone}, time:{time}, desc:{desc}})', vrf=vrf, seczone=seczone, time=time, desc=desc) else: logger.debug("Updating Existing VRF: " + vrf) nglib.py2neo_ses.cypher.execute( 'MATCH (v:VRF {name:{vrf}}) ' + 'SET v.seczone={seczone}, v.time={time}, v.desc={desc}', vrf=vrf, seczone=seczone, time=time, desc=desc)
def import_supernet(snet): """Import a Supernet Entry""" time = nglib.get_time() cidr = snet['cidr'] desc = snet['description'] role = snet['role'] secure = snet['secure'] results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Supernet {cidr:{cidr}}) RETURN n', cidr=cidr) # Insert new supernet if len(results) == 0: logger.info("New: Inserting Supernet: " + cidr) results = nglib.py2neo_ses.cypher.execute( 'CREATE (n:Supernet {cidr:{cidr}, name:{cidr}, desc:{desc}, role:{role}, ' + 'secure:{secure}, time:{time}}) RETURN n', cidr=cidr, role=role, desc=desc, secure=secure, time=time) # Update existing Supernet else: logger.debug("Supernet Exists, updating: " + cidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Supernet {cidr:{cidr}}) ' + 'SET n += {desc:{desc}, role:{role}, secure:{secure}, time:{time}} RETURN n', cidr=cidr, role=role, desc=desc, secure=secure, time=time)
def import_fw_ints(fwdb): """Import Firewall Interfaces from FW File""" time = nglib.get_time() # Iterate through all firewall ints for fwint in fwdb: # DB Values name = fwint['Name'] hostname = fwint['Hostname'] #IP = fwint['IP'] vlanInt = fwint['Interface'] desc = fwint['Description'] seclevel = fwint['Security-Level'] logIndex = fwint['Log-Index'] #print(name, vlanInt, desc, seclevel, IP, hostname, logIndex) vlan = vlanInt.replace('Vlan', '') # Search for existing Firewall results = nglib.py2neo_ses.cypher.execute( 'MATCH (fw:Switch:Router:FW {name:{name}}) RETURN fw', name=name) # Insert new Firewall if len(results) == 0: logger.info("Creating New Firewall: " + name) results = nglib.py2neo_ses.cypher.execute( 'CREATE (fw:Switch:Router:FW {name:{name}, hostname:{hostname}, ' + 'logIndex:{logIndex}, time:{time}}) RETURN fw', name=name, hostname=hostname, logIndex=logIndex, time=time) # Update FW else: logger.debug("Updating Firewall: " + name) nglib.py2neo_ses.cypher.execute( 'MATCH (fw:Switch:Router:FW {name:{name}}) SET fw += ' + '{hostname:{hostname}, logIndex:{logIndex}, time:{time}} RETURN fw', name=name, hostname=hostname, logIndex=logIndex, time=time) # Search for existing Vlan results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vid:{vlan}})-[e:ROUTED_FW]->' + '(fw:Switch:Router:FW {name:{name}}) ' + 'RETURN e', vlan=vlan, name=name) if len(results) == 0: logger.info("Creating New ROUTED_FW Link: %s --> %s", vlan, name) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vid:{vlan}}), (fw:Switch:Router:FW {name:{name}})' + 'CREATE (n)-[e:ROUTED_FW ' + '{desc:{desc}, seclevel:{seclevel}, time:{time}}]->(fw)', vlan=vlan, name=name, desc=desc, seclevel=seclevel, time=time) # Update Firewall Interface else: logger.debug("Updating ROUTED_FW: %s --> %s", vlan, name) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vid:{vlan}})-[e:ROUTED_FW]->' + '(fw:Switch:Router:FW {name:{name}})' + 'SET e += {desc:{desc}, seclevel:{seclevel}, time:{time}} ' + 'RETURN e', vlan=vlan, name=name, desc=desc, seclevel=seclevel, time=time)
def import_devicelist(fileName, infoFile): """Import Devices from Devices.csv""" logger.info("Importing Devices from %s, %s", fileName, infoFile) time = nglib.get_time() # Get Seed devices for CDP Direction seed = dict() seeds = nglib.dev_seeds.split(',') for s in seeds: seed[s] = True f = open(fileName) devdb = csv.DictReader(f) # Import Location Info df = open(infoFile) devcsv = csv.DictReader(df) devinfodb = dict() for en in devcsv: devinfodb[en['Device']] = dict() devinfodb[en['Device']]['Location'] = 'Unknown' devinfodb[en['Device']]['Model'] = 'Unknown' devinfodb[en['Device']]['Version'] = 'Unknown' devinfodb[en['Device']]['FQDN'] = en['Device'] devinfodb[en['Device']]['Platform'] = 'Unknown' if 'Location' in en: devinfodb[en['Device']]['Location'] = en['Location'] if 'Model' in en: devinfodb[en['Device']]['Model'] = en['Model'] if 'Version' in en: devinfodb[en['Device']]['Version'] = en['Version'] if 'FQDN' in en: devinfodb[en['Device']]['FQDN'] = en['FQDN'] if 'Platform' in en: devinfodb[en['Device']]['Platform'] = en['Platform'] for en in devdb: device = en['Device'] group = en['MgmtGroup'] if group == "None": group = None rType = en['Type'] if device not in devinfodb: devinfodb[device] = {"Location": "Unknown", "Model": "Unknown", \ "Version": "Unknown", "FQDN": device, "Platform": "Unknown"} if 'Platform' in en and devinfodb[device]['Platform'] == 'Unknown': devinfodb[device]['Platform'] = en['Platform'] if rType == "Primary": if nglib.verbose > 3: print("R: " + device) import_router(device, group, time, seed, rType, devinfodb[device]) elif rType == "Standby": if nglib.verbose > 3: print("Rs: " + device) import_router(device, group, time, seed, rType, devinfodb[device]) else: if nglib.verbose > 3: print("S: " + device) import_switch(device, group, time, seed, devinfodb[device])
def import_single_net(net, ignore_new, vrfmap): """Import a CIDR Entry in to NetGrph""" time = nglib.get_time() router = net['Router'] gateway = net['Gateway'] cidr = net['Subnet'] desc = net['Description'] vrf = net['VRF'] vlan = net['VLAN'] p2p = net['P2P'] standby = net['Standby'] rip = net['Gateway_Physical'] vpriority = net['Virtual_Priority'] vgroup = net['Virtual_Group'] vproto = net['Virtual_Protocol'] vver = net['Virtual_Version'] secondary = False if 'Secondary' in net and net['Secondary'] == '1': print('secondary', cidr) secondary = True # Check VRF Mapping to remap defaults if vrf == 'default' and router in vrfmap: vrf = vrfmap[router] #print("VRF", vrf) # Process P2P and Standby Router bools if p2p == "True": p2p = True else: p2p = False if standby == "True": standby = True else: standby = False vrfcidr = '{0}-{1}'.format(vrf, cidr) #unique key # Check the Router VRF Cache only once to add new relationship to routers check_vrf_cache(router, vrf) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}) RETURN n', vrfcidr=vrfcidr) # Add new network if len(results) == 0: logger.info("New: Inserting CIDR %s", vrfcidr) results = nglib.py2neo_ses.cypher.execute( 'CREATE (n:Network {cidr:{cidr}, vrfcidr:{vrfcidr}, name:{vrfcidr}, ' + 'vrf:{vrf}, desc:{desc}, vid:{vlan}, virtual_proto:{vproto}, virtual_version:{vver}, ' + 'virtual_group:{vgroup}, gateway:{gateway}, secondary:{sec}, time:{time}}) RETURN n', cidr=cidr, vrfcidr=vrfcidr, vrf=vrf, vlan=vlan, desc=desc, gateway=gateway, vproto=vproto, vver=vver, vgroup=vgroup, sec=secondary, time=time) # Record New Network Unless Ignoring initial run if not ignore_new: # Store a NewNetwork Object for alerting results = nglib.py2neo_ses.cypher.execute( 'CREATE (n:NewNetwork {cidr:{cidr}, vrfcidr:{vrfcidr}, name:{vrfcidr}, ' + 'vrf:{vrf}, desc:{desc}, vid:{vlan}, gateway:{gateway}, virtual_proto:{vproto}, ' + 'virtual_version:{vver}, time:{time}}) RETURN n', cidr=cidr, vrfcidr=vrfcidr, vrf=vrf, vlan=vlan, desc=desc, vproto=vproto, vver=vver, gateway=gateway, time=time) # Else update record else: logger.debug("Updating CIDR in Network %s", vrfcidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}) SET n += {desc:{desc}, vid:{vlan}, ' + 'virtual_group:{vgroup}, gateway:{gateway}, virtual_proto:{vproto}, ' + 'virtual_version:{vver}, secondary:{sec}, time:{time}} RETURN n', vrfcidr=vrfcidr, desc=desc, vlan=vlan, gateway=gateway, vproto=vproto, vver=vver, vgroup=vgroup, sec=secondary, time=time) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:VRF_IN]->() RETURN e', vrfcidr=vrfcidr) # Member of VRF Edge if len(results) == 0: logger.info("New: Creating VRF Relationship %s -> %s ", net['Subnet'], net['VRF']) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (v:VRF {name:{vrf}}) ' + 'CREATE (n)-[e:VRF_IN]->(v) RETURN e', vrfcidr=vrfcidr, vrf=vrf) else: logger.debug("Found existing VRF Relationship %s -> %s ", net['Subnet'], net['VRF']) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:VRF_IN]->(v:VRF {name:{vrf}}) ' + 'SET e.time={time} RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time) # Make sure Routed By Primary and not p2p link if not standby and not p2p: results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_BY]->' + '(r:Switch:Router {name:{router}}) RETURN e', vrfcidr=vrfcidr, router=router) # New router for network if len(results) == 0: logger.info("New: Creating Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (r:Switch:Router {name:{router}}) ' + 'CREATE (n)-[e:ROUTED_BY {vrf:{vrf}, ipv4:{rip}, ' + 'v_prio:{vpriority}, time:{time}}]->(r) RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time, router=router, rip=rip, vpriority=vpriority) if len(results) == 0: logger.warning("Failed to Create Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) # Update relationship timestamp else: logger.debug("Updating Existing Router Relationship: " + "{0} -> {1}".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_BY]->' + '(r:Switch:Router {name:{router}}) ' + 'SET e += {vrf:{vrf}, ipv4:{rip}, v_prio:{vpriority}, time:{time}} RETURN n', vrfcidr=vrfcidr, vrf=vrf, router=router, time=time, rip=rip, vpriority=vpriority) # Standby Router for Network elif standby and not p2p: results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_STANDBY]->' + '(r:Switch:Router {name:{router}}) RETURN e', vrfcidr=vrfcidr, router=router) # New router for network if len(results) == 0: logger.info("New: Creating Standby Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (r:Switch:Router {name:{router}}) ' + 'CREATE (n)-[e:ROUTED_STANDBY {vrf:{vrf}, ipv4:{rip}, ' + 'v_prio:{vpriority}, time:{time}}]->(r) RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time, router=router, rip=rip, vpriority=vpriority) if len(results) == 0: logger.warning("Failed to Create Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) # Update relationship timestamp else: logger.debug("Updating Existing Standby Router Relationship: " + "{0} -> {1}".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_STANDBY]->' + '(r:Switch:Router {name:{router}}) SET e += {vrf:{vrf}, ipv4:{rip}, ' + 'v_prio:{vpriority}, time:{time}} RETURN n', vrfcidr=vrfcidr, vrf=vrf, router=router, time=time, rip=rip, vpriority=vpriority) # P2P Routed Network. Use Special ROUTED Label for each VRF elif p2p: results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED {vrf:{vrf}}]->' + '(r:Switch:Router {name:{router}}) RETURN e', vrfcidr=vrfcidr, vrf=vrf, router=router) # New P2P Relationship for network if len(results) == 0: logger.info("New: Creating P2P Router Relationship " + "{0} -> {1} ({2})".format(net['Subnet'], net['Router'], vrf)) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (r:Switch:Router {name:{router}}) ' + 'CREATE (n)-[e:ROUTED {vrf:{vrf}, gateway:{gateway}, ipv4:{rip}, time:{time}}]->(r) RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time, gateway=gateway, router=router, rip=rip) if len(results) == 0: logger.warning("Failed to Create Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) # Update relationship timestamp else: logger.debug("Updating Existing P2P Router Relationship: " + "{0} -> {1} ({2})".format(net['Subnet'], net['Router'], vrf)) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED]->' + '(r:Switch:Router {name:{router}}) ' + 'SET e += {vrf:{vrf}, gateway:{gateway}, ipv4:{rip}, time:{time}} RETURN n', vrfcidr=vrfcidr, vrf=vrf, router=router, gateway=gateway, rip=rip, time=time) # Link up L2 to L3 info link_l3_to_l2(vrfcidr, vlan, router, time)
def import_single_net(net, ignore_new, vrfmap): """Import a CIDR Entry in to NetGrph""" time = nglib.get_time() router = net['Router'] gateway = net['Gateway'] cidr = net['Subnet'] desc = net['Description'] vrf = net['VRF'] vlan = net['VLAN'] p2p = net['P2P'] standby = net['Standby'] # Check VRF Mapping to remap defaults if vrf == 'default' and router in vrfmap: vrf = vrfmap[router] #print("VRF", vrf) # Process P2P and Standby Router bools if p2p == "True": p2p = True else: p2p = False if standby == "True": standby = True else: standby = False vrfcidr = '{0}-{1}'.format(vrf, cidr) #unique key # Check the Router VRF Cache only once to add new relationship to routers check_vrf_cache(router, vrf) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}) RETURN n', vrfcidr=vrfcidr) # Add new network if len(results) == 0: logger.info("New: Inserting CIDR %s", vrfcidr) results = nglib.py2neo_ses.cypher.execute( 'CREATE (n:Network {cidr:{cidr}, vrfcidr:{vrfcidr}, name:{vrfcidr}, ' + 'vrf:{vrf}, desc:{desc}, vid:{vlan}, ' + 'gateway:{gateway}, time:{time}}) RETURN n', cidr=cidr, vrfcidr=vrfcidr, vrf=vrf, vlan=vlan, desc=desc, gateway=gateway, time=time) # Record New Network Unless Ignoring initial run if not ignore_new: # Store a NewNetwork Object for alerting results = nglib.py2neo_ses.cypher.execute( 'CREATE (n:NewNetwork {cidr:{cidr}, vrfcidr:{vrfcidr}, name:{vrfcidr}, ' + 'vrf:{vrf}, desc:{desc}, vid:{vlan}, gateway:{gateway}, time:{time}}) RETURN n', cidr=cidr, vrfcidr=vrfcidr, vrf=vrf, vlan=vlan, desc=desc, gateway=gateway, time=time) # Else update record else: logger.debug("Updating CIDR in Network %s", vrfcidr) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}) SET n += {desc:{desc}, vid:{vlan}, ' + 'gateway:{gateway}, time:{time}} RETURN n', vrfcidr=vrfcidr, desc=desc, vlan=vlan, gateway=gateway, time=time) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:VRF_IN]->() RETURN e', vrfcidr=vrfcidr) # Member of VRF Edge if len(results) == 0: logger.info("New: Creating VRF Relationship %s -> %s ", net['Subnet'], net['VRF']) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (v:VRF {name:{vrf}}) ' + 'CREATE (n)-[e:VRF_IN]->(v) RETURN e', vrfcidr=vrfcidr, vrf=vrf) else: logger.debug("Found existing VRF Relationship %s -> %s ", net['Subnet'], net['VRF']) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:VRF_IN]->(v:VRF {name:{vrf}}) ' + 'SET e.time={time} RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time) # Make sure Routed By Primary and not p2p link if not standby and not p2p: results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_BY]->' + '(r:Switch:Router {name:{router}}) RETURN e', vrfcidr=vrfcidr, router=router) # New router for network if len(results) == 0: logger.info("New: Creating Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (r:Switch:Router {name:{router}}) ' + 'CREATE (n)-[e:ROUTED_BY {vrf:{vrf}, time:{time}}]->(r) RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time, router=router) if len(results) == 0: logger.warning( "Failed to Create Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) # Update relationship timestamp else: logger.debug("Updating Existing Router Relationship: " + "{0} -> {1}".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_BY]->' + '(r:Switch:Router {name:{router}}) ' + 'SET e += {vrf:{vrf}, time:{time}} RETURN n', vrfcidr=vrfcidr, vrf=vrf, router=router, time=time) # Standby Router for Network elif standby and not p2p: results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_STANDBY]->' + '(r:Switch:Router {name:{router}}) RETURN e', vrfcidr=vrfcidr, router=router) # New router for network if len(results) == 0: logger.info("New: Creating Standby Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (r:Switch:Router {name:{router}}) ' + 'CREATE (n)-[e:ROUTED_STANDBY {vrf:{vrf}, time:{time}}]->(r) RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time, router=router) if len(results) == 0: logger.warning( "Failed to Create Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) # Update relationship timestamp else: logger.debug("Updating Existing Standby Router Relationship: " + "{0} -> {1}".format(net['Subnet'], net['Router'])) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED_STANDBY]->' + '(r:Switch:Router {name:{router}}) SET e += {vrf:{vrf}, time:{time}} RETURN n', vrfcidr=vrfcidr, vrf=vrf, router=router, time=time) # P2P Routed Network. Use Special ROUTED Label for each VRF elif p2p: results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED {vrf:{vrf}}]->' + '(r:Switch:Router {name:{router}}) RETURN e', vrfcidr=vrfcidr, vrf=vrf, router=router) # New P2P Relationship for network if len(results) == 0: logger.info( "New: Creating P2P Router Relationship " + "{0} -> {1} ({2})".format(net['Subnet'], net['Router'], vrf)) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}}), (r:Switch:Router {name:{router}}) ' + 'CREATE (n)-[e:ROUTED {vrf:{vrf}, gateway:{gateway}, time:{time}}]->(r) RETURN e', vrfcidr=vrfcidr, vrf=vrf, time=time, gateway=gateway, router=router) if len(results) == 0: logger.warning( "Failed to Create Router Relationship " + "{0} -> {1} ".format(net['Subnet'], net['Router'])) # Update relationship timestamp else: logger.debug( "Updating Existing P2P Router Relationship: " + "{0} -> {1} ({2})".format(net['Subnet'], net['Router'], vrf)) results = nglib.py2neo_ses.cypher.execute( 'MATCH (n:Network {vrfcidr:{vrfcidr}})-[e:ROUTED]->' + '(r:Switch:Router {name:{router}}) ' + 'SET e += {vrf:{vrf}, gateway:{gateway}, time:{time}} RETURN n', vrfcidr=vrfcidr, vrf=vrf, router=router, gateway=gateway, time=time) # Link up L2 to L3 info link_l3_to_l2(vrfcidr, vlan, router, time)