aboutsummaryrefslogtreecommitdiffstats
path: root/examples/tg19/netbox_tools
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tg19/netbox_tools')
-rw-r--r--examples/tg19/netbox_tools/cleanup_switchestxt.py149
-rw-r--r--examples/tg19/netbox_tools/create_switch.py163
-rw-r--r--examples/tg19/netbox_tools/netbox2gondul.py133
-rw-r--r--examples/tg19/netbox_tools/switchestxt2netbox.py478
4 files changed, 923 insertions, 0 deletions
diff --git a/examples/tg19/netbox_tools/cleanup_switchestxt.py b/examples/tg19/netbox_tools/cleanup_switchestxt.py
new file mode 100644
index 0000000..4091dce
--- /dev/null
+++ b/examples/tg19/netbox_tools/cleanup_switchestxt.py
@@ -0,0 +1,149 @@
+#!/usr/bin/python3
+
+import pynetbox
+import ipaddress
+import re
+from natsort import natsorted
+
+nb = pynetbox.api(
+ 'https://netbox.infra.gathering.org',
+ token='<Removed>'
+)
+
+FLOOR_SITE = 4
+CORE_DEVICE_ID = 16
+CORE_NAME = 'r1.noc'
+CORE_DISTRO_PORTS = ['xe-0/2/{}', 'xe-0/3/{}']
+
+DISTRO_DEVICE_TYPE = 7
+DISTRO_DEVICE_ROLE = 8
+DISTRO_MGMT_VLAN_ROLE = 4
+DISTRO_MGMT_VLAN_ID = 666
+DISTRO_UPLINK_AE = 'ae0'
+DISTRO_UPLINK_PORTS = ['xe-0/1/0', 'xe-1/1/0']
+DISTRO_DEVICE_PLATFORM = 3
+DISTRO_LINKNET_VLAN_ID = 888
+DISTRO_LINKNET_ROLE = 3
+
+
+EDGE_DEVICE_TYPE = 5
+EDGE_DEVICE_ROLE = 10
+EDGE_VLAN_ROLE = 2
+EDGE_LAG_NAME = 'ae0'
+EDGE_DEVICE_PLATFORM = 3
+
+LOOPBACK_POOL_V4_ID = 10
+LOOPBACK_POOL_V6_ID = 121
+
+LINKNET_POOL_V4_ID = 11
+LINKNET_POOL_V6_ID = 124
+
+with open('switches.txt') as f:
+ switchestxt = f.readlines()
+with open('patchlist.txt') as f:
+ patchlisttxt = f.readlines()
+
+nb_vlans = nb.ipam.vlans.filter(site_id = FLOOR_SITE)
+nb_prefixes = nb.ipam.prefixes.filter(site_id = FLOOR_SITE)
+nb_switches = nb.dcim.devices.filter(site_id = FLOOR_SITE)
+
+switches = {}
+distros = {}
+
+def cleanupswitchandnetwork(sw, site_id):
+ name = sw['sysname']
+ network_name = name
+
+ network = None
+ for vlan in nb_vlans:
+ if vlan.name == network_name:
+ network = vlan
+ break
+
+ prefix_v4 = None
+ prefix_v6 = None
+
+ for prefix in nb_prefixes:
+ if prefix.vlan is not None and prefix.vlan.id == network.id and prefix.family == 4:
+ prefix_v4 = prefix
+ if prefix.vlan is not None and prefix.vlan.id == network.id and prefix.family == 6:
+ prefix_v6 = prefix
+
+ if prefix_v4 and prefix_v6 is not None:
+ break
+
+ if prefix_v4 is not None:
+ print('Deleting Prefix {}'.format(sw['subnet4']))
+ prefix_v4.delete()
+
+ if prefix_v6 is not None:
+ print('Deleting Prefix {}'.format(sw['subnet6']))
+ prefix_v6.delete()
+
+ if network is None:
+ print('Deleting Network {}'.format(network_name))
+ network.delete()
+
+ switch = None
+ for device in nb_switches:
+ if device.name == name:
+ switch = device
+ break
+ if switch is not None:
+ print('Deleting Switch {}'.format(name))
+ switch.delete()
+
+ if sw['is_distro'] is False: # Edge switch that have a distro
+ distro_id = nb.dcim.devices.get(name=sw['distro_name']).id
+ mgmt_network = nb.ipam.vlans.get(name="mgmt.{}".format(sw['distro_name']))
+ ae_interface = nb.dcim.interfaces.get(device_id=switch.id, name=sw['lag_name'])
+ if ae_interface is not None:
+ print('Deleting {} for {}'.format(sw['lag_name'], name))
+ ae_interface.delete()
+ distro_ae_interface_name = 'ae{}'.format(sw['vlan_id'])
+ distro_ae_interface = nb.dcim.interfaces.get(device_id=distro_id, name=distro_ae_interface_name)
+ if distro_ae_interface is not None:
+ print('Deleting {} for {}'.format(distro_ae_interface_name,sw['distro_name']))
+ distro_ae_interface.delete()
+
+ distro_x = int(re.match('s(.*)\.floor', sw['distro_name']).group(1))
+ core_subif_name = 'ae{}.{}'.format(distro_x + 10,sw['vlan_id'])
+ core_subif_interface = nb.dcim.interfaces.get(device_id = sw['core_device_id'], name = core_subif_name)
+ if core_subif_interface is not None:
+ print('Deleting {} for core {}'.format(core_subif_name, sw['core_name']))
+ core_subif_interface.delete()
+
+for switch in switchestxt:
+ switch = switch.strip().split()
+ switches[switch[0]] = {
+ 'sysname': switch[0],
+ 'subnet4': switch[1],
+ 'subnet6': switch[2],
+ 'mgmt4': switch[3],
+ 'mgmt6': switch[4],
+ 'vlan_id': int(switch[5]),
+ 'distro_name': switch[6],
+ 'is_distro': False,
+ 'vlan_role_id': EDGE_VLAN_ROLE,
+ 'device_type_id': EDGE_DEVICE_TYPE,
+ 'device_role_id': EDGE_DEVICE_ROLE,
+ 'device_platform_id': EDGE_DEVICE_PLATFORM,
+ 'lag_name': EDGE_LAG_NAME,
+ 'core_device_id': CORE_DEVICE_ID,
+ 'core_name': CORE_NAME
+ }
+
+for patch in patchlisttxt:
+ patch = patch.strip().split()
+ uplink = []
+ for p in patch[2:]:
+ uplink.append(p)
+ switches[patch[0]].update({
+ 'uplinks': uplink
+ })
+
+print('Access started')
+for switch in natsorted(switches):
+ sw = switches[switch]
+ cleanupswitchandnetwork(sw, FLOOR_SITE)
+print('Access done')
diff --git a/examples/tg19/netbox_tools/create_switch.py b/examples/tg19/netbox_tools/create_switch.py
new file mode 100644
index 0000000..f2cf248
--- /dev/null
+++ b/examples/tg19/netbox_tools/create_switch.py
@@ -0,0 +1,163 @@
+#!/usr/bin/python3
+
+import pynetbox
+import ipaddress
+import re
+
+import pyargs
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument('-name')
+parser.add_argument('-vlanid')
+parser.add_argument('-intf', nargs='+')
+
+args = parser.parse_args()
+
+nb = pynetbox.api(
+ 'https://netbox.infra.gathering.org',
+ token='<Removed>'
+)
+
+
+RING_SITE = 2
+RING_DEVICE_ID = 15
+RING_VLAN_GROUP = 1
+RING_NAME = 'r1.ring'
+RING_MGMT_v4_PREFIX = 486
+RING_MGMT_V6_PREFIX_ID = 118
+RING_MGMT_V6_PREFIX = "2a06:5841:c:"
+
+RING_PREFIX_SUPERNET_v4 = 28
+RING_PREFIX_SUPERNET_v6 = 105
+
+EDGE_DEVICE_TYPE = 5
+EDGE_DEVICE_ROLE = 11
+EDGE_VLAN_ROLE = 1
+EDGE_LAG_NAME = 'ae0'
+EDGE_MGMT_INTF_NAME = 'vlan.666'
+EDGE_DEVICE_PLATFORM = 3
+EDGE_UPLINKS = ['ge-0/0/47', 'ge-0/0/46']
+
+sysname = args.name
+
+vlanid = args.vlanid
+dist_interfaces = args.intf
+
+print('Creating VLAN {} with ID {}'.format(sysname, vlanid))
+vlan = nb.ipam.vlans.create(
+ name=sysname,
+ vid=vlanid,
+ status=1,
+ site=RING_SITE,
+ group=RING_VLAN_GROUP,
+ role=EDGE_VLAN_ROLE
+)
+
+nb.ipam.prefixes.get(RING_PREFIX_SUPERNET_v4).available_prefixes.create({
+ "prefix_length": 26,
+ "vlan": vlan.id,
+ "site": RING_SITE,
+ "role": EDGE_VLAN_ROLE,
+ "description": sysname
+ })
+v4_prefix = nb.ipam.prefixes.get(vlan_id=vlan.id, family=4, status=1)
+print('Created Prefix {}'.format(v4_prefix))
+
+nb.ipam.prefixes.create(
+ family = 6,
+ prefix = RING_MGMT_V6_PREFIX + str(vlan.vid) + "::/64",
+ vlan = vlan.id,
+ status = 1,
+ site = RING_SITE,
+ role = EDGE_VLAN_ROLE,
+ description = sysname
+)
+v6_prefix = nb.ipam.prefixes.get(vlan_id=vlan.id, family=6, status=1)
+print('Created Prefix {}'.format(v6_prefix))
+
+print('Creating Device {}'.format(sysname))
+switch = nb.dcim.devices.create(
+ name = sysname,
+ device_type = EDGE_DEVICE_TYPE,
+ device_role = EDGE_DEVICE_ROLE,
+ platform = EDGE_DEVICE_PLATFORM,
+ site = RING_SITE,
+ status = 1
+)
+
+ae_interface = nb.dcim.interfaces.get(device_id=switch.id, name=EDGE_LAG_NAME)
+mgmt_interface = nb.dcim.interfaces.get(device_id=switch.id, name=EDGE_MGMT_INTF_NAME)
+ring_ae_interface = nb.dcim.interfaces.create(
+ device = RING_DEVICE_ID,
+ name = "ae" + vlanid,
+ form_factor = 200,
+ description = sysname
+)
+
+ring_irb_interface = nb.dcim.interfaces.create(
+ device = RING_DEVICE_ID,
+ name = "irb." + vlanid,
+ form_factor = 200,
+ description = sysname
+)
+ring_ipv4 = nb.ipam.ip_addresses.create(
+ interface = ring_irb_interface.id,
+ address = v4_prefix.available_ips.list()[1]['address'],
+ status = 1,
+ description = sysname
+)
+ring_ipv6 = nb.ipam.ip_addresses.create(
+ interface = ring_irb_interface.id,
+ address = v6_prefix.available_ips.list()[1]['address'],
+ status = 1,
+ description = sysname
+)
+
+for x, uplinks in enumerate(EDGE_UPLINKS):
+ uplink = nb.dcim.interfaces.get(device_id=switch.id, name=uplinks)
+ uplink.lag = ae_interface.id
+ uplink.save()
+ intf = nb.dcim.interfaces.get(device_id=RING_DEVICE_ID, name=dist_interfaces[x-1])
+ print('Creating cable {}:{} -> {}:{}'.format(RING_NAME, intf, sysname, uplink))
+ nb.dcim.cables.create(
+ termination_a_id = intf.id,
+ termination_a_type = 'dcim.interface',
+ termination_b_id = uplink.id,
+ termination_b_type = 'dcim.interface',
+ status = True,
+ color = 'c0c0c0'
+ )
+
+for x, interfaces in enumerate(dist_interfaces):
+ interface = nb.dcim.interfaces.get(device_id=RING_DEVICE_ID, name=interfaces)
+ interface.lag = ring_ae_interface.id
+ interface.save()
+
+mgmt_interface = nb.dcim.interfaces.get(device_id=switch.id, name=EDGE_MGMT_INTF_NAME)
+
+mgmt_v4_address = nb.ipam.prefixes.get(RING_MGMT_v4_PREFIX).available_ips.list()[0]['address']
+mgmt_v6_address = nb.ipam.prefixes.get(RING_MGMT_V6_PREFIX_ID)
+ipv4 = nb.ipam.ip_addresses.create(
+ interface = mgmt_interface.id,
+ address = "{}".format(str(mgmt_v4_address)),
+ status = 1,
+ description = sysname
+)
+
+p = re.compile('(.*)\.(.*)\.(.*)\.(.*)/(.*)')
+m = p.match(mgmt_v4_address)
+ipv4_last = m.group(4)
+print(str(mgmt_v6_address))
+p = re.compile('(.*)::/(.*)')
+m = p.match(str(mgmt_v6_address))
+
+ipv6 = nb.ipam.ip_addresses.create(
+ interface = mgmt_interface.id,
+ address = "{0}::{1}/64".format(m.group(1), ipv4_last),
+ status = 1,
+ description = sysname
+)
+switch.primary_ip4 = ipv4.id
+switch.primary_ip6 = ipv6.id
+switch.save()
diff --git a/examples/tg19/netbox_tools/netbox2gondul.py b/examples/tg19/netbox_tools/netbox2gondul.py
new file mode 100644
index 0000000..f0154dd
--- /dev/null
+++ b/examples/tg19/netbox_tools/netbox2gondul.py
@@ -0,0 +1,133 @@
+#!/usr/bin/python3
+
+import ipaddress
+import requests
+import json
+from requests.auth import HTTPBasicAuth
+
+import pynetbox
+
+gondul_url = 'https://gondul.tg19.gathering.org'
+gondul_user = 'tech'
+gondul_pass = '<Removed>'
+
+nb = pynetbox.api(
+ 'https://netbox.infra.gathering.org',
+ token='<Removed>'
+)
+
+snmp_community = '<Removed>'
+
+ipam_roles = [1, 2, 5, 4, 7]
+#ipam_roles = [1]
+
+switches_roles = [11, 10, 6, 8]
+#switches_roles = [11]
+
+# Create vlans/networks
+nb_vlans = nb.ipam.vlans.all()
+for vlan in nb_vlans:
+ if vlan.role is None:
+ continue
+ if vlan.role.id not in ipam_roles:
+ continue
+ prefix_v4 = nb.ipam.prefixes.filter(vlan_id=vlan.id, family=4, status=1)
+ if len(prefix_v4) <= 0:
+ print("No v4 prefix found for vlan {}".format(vlan.name))
+ prefix_v4 = None
+ gw4 = None
+ else:
+ prefix_v4 = prefix_v4[0].prefix
+ gw4 = ipaddress.IPv4Network(prefix_v4)[1].exploded
+
+ prefix_v6 = nb.ipam.prefixes.filter(vlan_id=vlan.id, family=6, status=1)
+ if len(prefix_v6) <= 0:
+ print("No v6 prefix found for vlan {}".format(vlan.name))
+ prefix_v6 = None
+ gw6 = None
+ else:
+ prefix_v6 = prefix_v6[0].prefix
+ gw6 = ipaddress.IPv6Network(prefix_v6)[1].exploded
+
+ router = nb.ipam.ip_addresses.filter(address=gw4)
+ if len(router) <= 0:
+ print("No router found for vlan {}".format(vlan.name))
+ router = None
+ else:
+ router = router[0].interface.device.name
+
+ #tags = [vlan.role.slug]
+ data = json.dumps([{'name': vlan.name, 'subnet4': prefix_v4, 'subnet6': prefix_v6, 'gw4': gw4, 'gw6': gw6, 'router': router, 'vlan': vlan.vid}])
+ r = requests.post("{}/api/write/networks".format(gondul_url), data=data, headers={'content-type': 'application/json'}, auth=HTTPBasicAuth(gondul_user, gondul_pass))
+ print(r.status_code, r.text, data)
+
+# Create switches/devices
+nb_switches = nb.dcim.devices.all()
+for switch in nb_switches:
+ if switch.device_role is None:
+ continue
+ if switch.device_role.id not in switches_roles:
+ continue
+ lag = nb.dcim.interfaces.filter(device_id=switch.id, name='ae0')
+ if len(lag) <= 0:
+ print("No ae0 found for switch {}, not setting distro".format(switch.name))
+ distro = None
+ uplink = None
+ else:
+ uplinks = nb.dcim.interfaces.filter(lag_id=lag[0].id)
+ if uplinks is not None and uplinks[0].connected_endpoint is not None:
+ distro = uplinks[0].connected_endpoint.device.name
+ uplink = "{}.0".format(uplinks[0].connected_endpoint.name)
+ else:
+ distro = None
+ uplink = None
+
+ if switch.primary_ip4 is not None:
+ mgmt_vlan = nb.ipam.prefixes.filter(contains=switch.primary_ip4.address, status=1)
+ print(mgmt_vlan)
+ ip4 = str(switch.primary_ip4.address)
+ if len(mgmt_vlan) <= 0:
+ print("mgmt_vlan not found for switch {}".format(switch.name))
+ mgmt_vlan_name = None
+ elif mgmt_vlan[0].vlan is None:
+ print("mgmt_vlan not found for switch {}".format(switch.name))
+ mgmt_vlan_name = None
+ else:
+ mgmt_vlan_name = mgmt_vlan[0].vlan.name
+ else:
+ mgmt_vlan_name = None
+ ip4 = ''
+
+ if switch.primary_ip6 is not None:
+ ip6 = str(switch.primary_ip6)
+ else:
+ ip6 = ''
+
+ print(switch.device_role.id)
+ if switch.device_role.id != 8:
+ traffic_vlan = switch.name
+ else:
+ traffic_vlan = None
+
+ data = {'sysname': switch.name, 'community': snmp_community}
+
+ if distro is not None:
+ data.update({'distro_name': distro})
+ if uplink is not None:
+ data.update({'distro_phy_port': uplink})
+ if traffic_vlan is not None:
+ data.update({'traffic_vlan': traffic_vlan})
+ if mgmt_vlan_name is not None:
+ data.update({'mgmt_vlan': mgmt_vlan_name})
+ if ip4 is not None and ip4 != '':
+ data.update({'mgmt_v4_addr': ip4})
+ if ip6 is not None and ip6 != '':
+ data.update({'mgmt_v6_addr': ip6})
+
+ tags = [switch.device_role.slug] + switch.tags
+ data.update({'tags': tags})
+
+ data = json.dumps([data])
+ print(data)
+ r = requests.post("{}/api/write/switches".format(gondul_url), data=data, headers={'content-type': 'application/json'}, auth=HTTPBasicAuth(gondul_user, gondul_pass))
+ print(r.status_code, r.reason, data)
diff --git a/examples/tg19/netbox_tools/switchestxt2netbox.py b/examples/tg19/netbox_tools/switchestxt2netbox.py
new file mode 100644
index 0000000..db7c80c
--- /dev/null
+++ b/examples/tg19/netbox_tools/switchestxt2netbox.py
@@ -0,0 +1,478 @@
+#!/usr/bin/python3
+
+import pynetbox
+import ipaddress
+import re
+from natsort import natsorted
+
+nb = pynetbox.api(
+ 'https://netbox.infra.gathering.org',
+ token='<Removed>'
+)
+
+FLOOR_SITE = 4
+CORE_DEVICE_ID = 16
+CORE_NAME = 'r1.noc'
+CORE_DISTRO_PORTS = ['xe-0/2/{}', 'xe-0/3/{}']
+
+DISTRO_DEVICE_TYPE = 7
+DISTRO_DEVICE_ROLE = 8
+DISTRO_MGMT_VLAN_ROLE = 4
+DISTRO_MGMT_VLAN_ID = 666
+DISTRO_UPLINK_AE = 'ae0'
+DISTRO_UPLINK_PORTS = ['xe-0/1/0', 'xe-1/1/0']
+DISTRO_DEVICE_PLATFORM = 3
+DISTRO_LINKNET_VLAN_ID = 888
+DISTRO_LINKNET_ROLE = 3
+
+
+EDGE_DEVICE_TYPE = 5
+EDGE_DEVICE_ROLE = 10
+EDGE_VLAN_ROLE = 2
+EDGE_LAG_NAME = 'ae0'
+EDGE_DEVICE_PLATFORM = 3
+
+LOOPBACK_POOL_V4_ID = 10
+LOOPBACK_POOL_V6_ID = 121
+
+LINKNET_POOL_V4_ID = 11
+LINKNET_POOL_V6_ID = 124
+
+with open('switches.txt') as f:
+ switchestxt = f.readlines()
+with open('patchlist.txt') as f:
+ patchlisttxt = f.readlines()
+
+nb_vlans = nb.ipam.vlans.filter(site_id = FLOOR_SITE)
+nb_prefixes = nb.ipam.prefixes.filter(site_id = FLOOR_SITE)
+nb_switches = nb.dcim.devices.filter(site_id = FLOOR_SITE)
+
+switches = {}
+distros = {}
+
+def createswitchandnetwork(sw, site_id):
+ name = sw['sysname']
+ network_name = name
+ if sw['is_distro']:
+ network_name = 'mgmt.{}'.format(name)
+
+ network = None
+ for vlan in nb_vlans:
+ if vlan.name == network_name:
+ if vlan.vid != sw['vlan_id']:
+ print("Vlan with name {} already exist, but have wrong vlan ID {}".format(vlan.name, vlan.vid))
+ exit(1)
+ network = vlan
+ break
+
+ if network is None:
+ print('Creating Network {}'.format(network_name))
+ network = nb.ipam.vlans.create(
+ name=network_name,
+ vid=sw['vlan_id'],
+ status=1,
+ site=site_id,
+ role=sw['vlan_role_id']
+ )
+
+ prefix_v4 = None
+ prefix_v6 = None
+
+ for prefix in nb_prefixes:
+ if prefix.vlan is not None and prefix.vlan.id == network.id and prefix.family == 4:
+ prefix_v4 = prefix
+ if prefix.vlan is not None and prefix.vlan.id == network.id and prefix.family == 6:
+ prefix_v6 = prefix
+
+ if prefix_v4 and prefix_v6 is not None:
+ break
+
+ if prefix_v4 is None:
+ print('Creating Prefix {}'.format(sw['subnet4']))
+ prefix_v4 = nb.ipam.prefixes.create(
+ family = 4,
+ prefix = sw['subnet4'],
+ vlan = network.id,
+ status = 1,
+ site = site_id,
+ role = sw['vlan_role_id']
+ )
+
+ if prefix_v6 is None:
+ print('Creating Prefix {}'.format(sw['subnet6']))
+ prefix_v6 = nb.ipam.prefixes.create(
+ family = 6,
+ prefix = sw['subnet6'],
+ vlan = network.id,
+ status = 1,
+ site = FLOOR_SITE,
+ role = sw['vlan_role_id']
+ )
+
+ switch = None
+ for device in nb_switches:
+ if device.name == name:
+ switch = device
+ break
+ if switch is None:
+ print('Creating Switch {}'.format(name))
+ switch = nb.dcim.devices.create(
+ name = name,
+ device_type = sw['device_type_id'],
+ device_role = sw['device_role_id'],
+ platform = sw['device_platform_id'],
+ site = FLOOR_SITE,
+ status = 1
+ )
+
+ if sw['is_distro'] is True: # Distro
+ if nb.dcim.interfaces.get(device_id=switch.id, name='lo0') is None:
+ print('Creating lo0 for {}'.format(name))
+ lo_interface = nb.dcim.interfaces.create(
+ device = switch.id,
+ name = 'lo0',
+ form_factor = 0,
+ description = "{} loopback".format(name)
+ )
+ loopback_v4_address = nb.ipam.prefixes.get(sw['loopback_pool_v4_id']).available_ips.list()[0]['address']
+ print("{}/32".format(str(ipaddress.ip_interface(loopback_v4_address).ip)))
+ ipv4 = nb.ipam.ip_addresses.create(
+ interface = lo_interface.id,
+ address = "{}/32".format(str(ipaddress.ip_interface(loopback_v4_address).ip)),
+ status = 1,
+ description = "{} loopback".format(name),
+ role = 10
+ )
+
+ loopback_v6_address = nb.ipam.prefixes.get(sw['loopback_pool_v6_id'])
+ p = re.compile('(.*)\.(.*)\.(.*)\.(.*)/(.*)')
+ m = p.match(loopback_v4_address)
+ ipv4_last = m.group(4)
+ print(str(loopback_v6_address))
+ p = re.compile('(.*)::/(.*)')
+ m = p.match(str(loopback_v6_address))
+
+ ipv6 = nb.ipam.ip_addresses.create(
+ interface = lo_interface.id,
+ address = "{0}::{1}/128".format(m.group(1), ipv4_last),
+ status = 1,
+ description = "{} loopback".format(name),
+ role = 10
+ )
+ switch.primary_ip4 = ipv4.id
+ switch.primary_ip6 = ipv6.id
+ switch.save()
+
+ mgmt_interface_name = 'vlan.{}'.format(sw['vlan_id'])
+ if nb.dcim.interfaces.get(device_id=switch.id, name=mgmt_interface_name) is None:
+ print('Creating {} for {}'.format(mgmt_interface_name, name))
+ mgmt_interface = nb.dcim.interfaces.create(
+ device = switch.id,
+ name = mgmt_interface_name,
+ form_factor = 0,
+ description = network_name
+ )
+ ipv4 = nb.ipam.ip_addresses.create(
+ interface = mgmt_interface.id,
+ address = "{}/{}".format(str(ipaddress.ip_network(sw['subnet4'])[1]),ipaddress.ip_network(sw['subnet4']).prefixlen),
+ status = 1,
+ description = network_name
+ )
+ ipv6 = nb.ipam.ip_addresses.create(
+ interface = mgmt_interface.id,
+ address = "{}/{}".format(str(ipaddress.ip_network(sw['subnet6'])[1]),ipaddress.ip_network(sw['subnet6']).prefixlen),
+ status = 1,
+ description = network_name
+ )
+
+ ae_interface = nb.dcim.interfaces.get(device_id=switch.id, name=sw['lag_name'])
+ if ae_interface is None:
+ print('Creating {} for {}'.format(sw['lag_name'], name))
+ ae_interface = nb.dcim.interfaces.create(
+ device = switch.id,
+ name = sw['lag_name'],
+ form_factor = 200,
+ description = sw['core_name'],
+ mode = 200
+ )
+
+ distro_x = int(re.match('s(.*)\.floor', name).group(1))
+ core_ae_interface_name = 'ae{}'.format(distro_x + 10)
+ core_ae_interface = nb.dcim.interfaces.get(device_id=sw['core_device_id'], name=core_ae_interface_name)
+ if core_ae_interface is None:
+ print('Creating {} for {}'.format(core_ae_interface_name, sw['core_name']))
+ core_ae_interface = nb.dcim.interfaces.create(
+ device = sw['core_device_id'],
+ name = core_ae_interface_name,
+ form_factor = 200,
+ description = name
+ )
+
+ sw['uplinks'].sort(reverse=True)
+ for x, uplink in enumerate(sw['uplinks']):
+ uplink = uplink.format(distro_x-1)
+ port = sw['local_uplink_ports'][x-1]
+ interface = nb.dcim.interfaces.get(device_id=switch.id, name=port)
+ if interface.connected_endpoint is None:
+ interface.lag = ae_interface.id
+ interface.description = '{}:{}'.format(sw['core_name'], uplink)
+ interface.save()
+
+ core_interface = nb.dcim.interfaces.get(device_id=sw['core_device_id'], name=uplink)
+ core_interface.lag = core_ae_interface.id
+ core_interface.description = '{}:{}'.format(name,port)
+ core_interface.save()
+
+ print('Creating cable {}:{} -> {}:{}'.format(sw['core_name'], uplink, sw['sysname'], port))
+ nb.dcim.cables.create(
+ termination_a_id = core_interface.id,
+ termination_a_type = 'dcim.interface',
+ termination_b_id = interface.id,
+ termination_b_type = 'dcim.interface',
+ status = True,
+ color = 'c0c0c0'
+ )
+
+ linknet_vlan = nb.ipam.vlans.get(name='{}-{}-linknet'.format(sw['core_name'], name))
+ if linknet_vlan is None:
+ linknet_vlan = nb.ipam.vlans.create(
+ name='{}-{}-linknet'.format(sw['core_name'], name),
+ vid=sw['linknet_vlan_id'],
+ status=1,
+ site=site_id,
+ role=sw['distro_linknet_role']
+ )
+
+ print(nb.ipam.prefixes.filter(vlan_id=linknet_vlan.id, family=4, status=1))
+ linknet_v4_prefix = nb.ipam.prefixes.get(vlan_id=linknet_vlan.id, family=4, status=1)
+ if linknet_v4_prefix is None:
+ nb.ipam.prefixes.get(sw['linknet_pool_v4_id']).available_prefixes.create({
+ "prefix_length": 31,
+ "vlan": linknet_vlan.id,
+ "role": 3,
+ "description": '{} - {}'.format(sw['core_name'], name)
+ })
+ linknet_v4_prefix = nb.ipam.prefixes.get(vlan_id=linknet_vlan.id, family=4, status=1)
+
+ linknet_v6_prefix = nb.ipam.prefixes.get(vlan_id=linknet_vlan.id, family=6, status=1)
+ if linknet_v6_prefix is None:
+ nb.ipam.prefixes.get(sw['linknet_pool_v6_id']).available_prefixes.create({
+ "prefix_length": 64,
+ "vlan": linknet_vlan.id,
+ "role": 3,
+ "description": '{} - {}'.format(sw['core_name'], name)
+ })
+ linknet_v6_prefix = nb.ipam.prefixes.get(vlan_id=linknet_vlan.id, family=6, status=1)
+
+ linknet_interface_name = 'vlan.{}'.format(sw['linknet_vlan_id'])
+ if nb.dcim.interfaces.get(device_id=switch.id, name=linknet_interface_name) is None:
+ print('Creating {} for {}'.format(linknet_interface_name, name))
+ linknet_interface = nb.dcim.interfaces.create(
+ device = switch.id,
+ name = linknet_interface_name,
+ form_factor = 0,
+ description = sw['core_name']
+ )
+ ipv4 = nb.ipam.ip_addresses.create(
+ interface = linknet_interface.id,
+ address = linknet_v4_prefix.available_ips.list()[1]['address'],
+ status = 1,
+ description = '{}-{}-linknet'.format(sw['core_name'], name)
+ )
+ ipv6 = nb.ipam.ip_addresses.create(
+ interface = linknet_interface.id,
+ address = linknet_v6_prefix.available_ips.list()[1]['address'],
+ status = 1,
+ description = '{}-{}-linknet'.format(sw['core_name'], name)
+ )
+
+ core_linknet_interface_name = '{}.{}'.format(core_ae_interface_name, sw['linknet_vlan_id'])
+ if nb.dcim.interfaces.get(device_id=sw['core_device_id'], name=core_linknet_interface_name) is None:
+ print('Creating {} for {}'.format(core_linknet_interface_name, sw['core_name']))
+ core_linknet_interface = nb.dcim.interfaces.create(
+ device = sw['core_device_id'],
+ name = core_linknet_interface_name,
+ form_factor = 0,
+ description = sw['core_name']
+ )
+ ipv4 = nb.ipam.ip_addresses.create(
+ interface = core_linknet_interface.id,
+ address = linknet_v4_prefix.available_ips.list()[0]['address'],
+ status = 1,
+ description = '{}-{}-linknet'.format(sw['core_name'], name)
+ )
+ ipv6 = nb.ipam.ip_addresses.create(
+ interface = core_linknet_interface.id,
+ address = linknet_v6_prefix.available_ips.list()[0]['address'],
+ status = 1,
+ description = '{}-{}-linknet'.format(sw['core_name'], name)
+ )
+
+ if sw['is_distro'] is False: # Edge switch that have a distro
+ distro_id = nb.dcim.devices.get(name=sw['distro_name']).id
+ mgmt_network = nb.ipam.vlans.get(name="mgmt.{}".format(sw['distro_name']))
+ ae_interface = nb.dcim.interfaces.get(device_id=switch.id, name=sw['lag_name'])
+ if ae_interface is None:
+ print('Creating {} for {}'.format(sw['lag_name'], name))
+ ae_interface = nb.dcim.interfaces.create(
+ device = switch.id,
+ name = sw['lag_name'],
+ form_factor = 200,
+ description = sw['distro_name'],
+ mode = 200,
+ tagged_vlans = [network.id, mgmt_network.id]
+ )
+ distro_ae_interface_name = 'ae{}'.format(sw['vlan_id'])
+ distro_ae_interface = nb.dcim.interfaces.get(device_id=distro_id, name=distro_ae_interface_name)
+ if distro_ae_interface is None:
+ print('Creating {} for {}'.format(distro_ae_interface_name,sw['distro_name']))
+ distro_ae_interface = nb.dcim.interfaces.create(
+ device = distro_id,
+ name = distro_ae_interface_name,
+ form_factor = 200,
+ description = name,
+ mode = 200,
+ tagged_vlans = [network.id, mgmt_network.id]
+ )
+
+ for x, uplink in enumerate(sw['uplinks']):
+ port = 'ge-0/0/{}'.format(44+x)
+ interface = nb.dcim.interfaces.get(device_id=switch.id, name=port)
+ if interface.connected_endpoint is None:
+ interface.lag = ae_interface.id
+ interface.description = '{}:{}'.format(sw['distro_name'], uplink)
+ interface.save()
+
+ distro_interface = nb.dcim.interfaces.get(device_id=distro_id, name=uplink)
+ distro_interface.lag = distro_ae_interface.id
+ distro_interface.description = '{}:{}'.format(name,port)
+ distro_interface.save()
+
+ print('Creating cable {}:{} -> {}:{}'.format(sw['distro_name'], uplink, sw['sysname'], port))
+ nb.dcim.cables.create(
+ termination_a_id = distro_interface.id,
+ termination_a_type = 'dcim.interface',
+ termination_b_id = interface.id,
+ termination_b_type = 'dcim.interface',
+ status = True,
+ color = 'c0c0c0'
+ )
+
+ vlan_name = 'vlan.{}'.format(DISTRO_MGMT_VLAN_ID)
+ vlan_interface = nb.dcim.interfaces.get(device_id=switch.id, name=vlan_name)
+ if vlan_interface is None:
+ print('Creating {} for {}'.format(vlan_name, name))
+ vlan_interface = nb.dcim.interfaces.create(
+ device = switch.id,
+ name=vlan_name,
+ form_factor=0,
+ description="mgmt.{}".format(sw['distro_name'])
+ )
+ if switch.primary_ip4 is None:
+ ipv4 = nb.ipam.ip_addresses.get(interface_id=vlan_interface.id, family=4)
+ if ipv4 is None:
+ ipv4 = nb.ipam.ip_addresses.create(
+ interface=vlan_interface.id,
+ address=sw['mgmt4'],
+ status=1,
+ description="mgmt.{}".format(sw['distro_name'])
+ )
+ switch.primary_ip4 = ipv4.id
+ if switch.primary_ip6 is None:
+ ipv6 = nb.ipam.ip_addresses.get(interface_id=vlan_interface.id, family=6)
+ if ipv6 is None:
+ ipv6 = nb.ipam.ip_addresses.create(
+ interface=vlan_interface.id,
+ address=sw['mgmt6'],
+ status=1,
+ description="mgmt.{}".format(sw['distro_name'])
+ )
+ switch.primary_ip6 = ipv6.id
+ switch.save()
+
+ distro_x = int(re.match('s(.*)\.floor', sw['distro_name']).group(1))
+ core_subif_name = 'ae{}.{}'.format(distro_x + 10,sw['vlan_id'])
+ core_subif_interface = nb.dcim.interfaces.get(device_id = sw['core_device_id'], name = core_subif_name)
+ if core_subif_interface is None:
+ print('Creating {} for core {}'.format(core_subif_name, sw['core_name']))
+ core_subif_interface = nb.dcim.interfaces.create(
+ device = sw['core_device_id'],
+ name = core_subif_name,
+ form_factor = 0,
+ description = "{} - {}:{}".format(network_name, sw['distro_name'], distro_ae_interface.name)
+ )
+ nb.ipam.ip_addresses.create(
+ interface = core_subif_interface.id,
+ address = "{}/{}".format(str(ipaddress.ip_network(sw['subnet4'])[1]),ipaddress.ip_network(sw['subnet4']).prefixlen),
+ status = 1,
+ description = network_name
+ )
+ nb.ipam.ip_addresses.create(
+ interface = core_subif_interface.id,
+ address = "{}/{}".format(str(ipaddress.ip_network(sw['subnet6'])[1]),ipaddress.ip_network(sw['subnet6']).prefixlen),
+ status = 1,
+ description = network_name
+ )
+
+for switch in switchestxt:
+ switch = switch.strip().split()
+ switches[switch[0]] = {
+ 'sysname': switch[0],
+ 'subnet4': switch[1],
+ 'subnet6': switch[2],
+ 'mgmt4': switch[3],
+ 'mgmt6': switch[4],
+ 'vlan_id': int(switch[5]),
+ 'distro_name': switch[6],
+ 'is_distro': False,
+ 'vlan_role_id': EDGE_VLAN_ROLE,
+ 'device_type_id': EDGE_DEVICE_TYPE,
+ 'device_role_id': EDGE_DEVICE_ROLE,
+ 'device_platform_id': EDGE_DEVICE_PLATFORM,
+ 'lag_name': EDGE_LAG_NAME,
+ 'core_device_id': CORE_DEVICE_ID,
+ 'core_name': CORE_NAME
+ }
+ if switch[6] not in distros:
+ distros[switch[6]] = {
+ 'sysname': switch[6],
+ 'subnet4': str(ipaddress.ip_network(switch[3], strict=False)),
+ 'subnet6': str(ipaddress.ip_network(switch[4], strict=False)),
+ 'is_distro': True,
+ 'vlan_role_id': DISTRO_MGMT_VLAN_ROLE,
+ 'device_type_id': DISTRO_DEVICE_TYPE,
+ 'device_role_id': DISTRO_DEVICE_ROLE,
+ 'device_platform_id': DISTRO_DEVICE_PLATFORM,
+ 'vlan_id': DISTRO_MGMT_VLAN_ID,
+ 'lag_name': DISTRO_UPLINK_AE,
+ 'uplinks': CORE_DISTRO_PORTS,
+ 'local_uplink_ports': DISTRO_UPLINK_PORTS,
+ 'core_device_id': CORE_DEVICE_ID,
+ 'core_name': CORE_NAME,
+ 'linknet_vlan_id': DISTRO_LINKNET_VLAN_ID,
+ 'loopback_pool_v4_id': LOOPBACK_POOL_V4_ID,
+ 'loopback_pool_v6_id': LOOPBACK_POOL_V6_ID,
+ 'linknet_pool_v4_id': LINKNET_POOL_V4_ID,
+ 'linknet_pool_v6_id': LINKNET_POOL_V6_ID,
+ 'distro_linknet_role': DISTRO_LINKNET_ROLE
+ }
+
+for patch in patchlisttxt:
+ patch = patch.strip().split()
+ uplink = []
+ for p in patch[2:]:
+ uplink.append(p)
+ switches[patch[0]].update({
+ 'uplinks': uplink
+ })
+
+print('Distro started')
+for distro in natsorted(distros):
+ sw = distros[distro]
+ createswitchandnetwork(sw, FLOOR_SITE)
+print('Distro done')
+print('Access started')
+for switch in natsorted(switches):
+ sw = switches[switch]
+ createswitchandnetwork(sw, FLOOR_SITE)
+print('Access done')