-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProviderEdgeProvision2.py
190 lines (160 loc) · 6.88 KB
/
ProviderEdgeProvision2.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from time import sleep
from aiohttp.helpers import BasicAuth
import ipaddress
import aiohttp
import asyncio
import requests
import json
import crayons
import os
from configparser import ConfigParser
###################
# Config Read #
###################
config_object = ConfigParser()
config_object.read("config.ini")
userinfo = config_object["new-customer"]
CustomerName = userinfo['CustomerName']
CustomerID = userinfo['CustomerID']
VpnId = userinfo['VpnId']
######################
# Provision #
######################
class MplsPe():
def __init__(self, HostName, MgmtIp, UserName, Password):
self.HostName = HostName
self.MgmtIp = MgmtIp
self.UserName = UserName
self.Password = Password
self.headers = {'Accept': 'application/yang-data+json',
'Content-Type': 'application/yang-data+json'}
def GetConfig(self):
result = requests.get(url=f"https://{self.MgmtIp}/restconf/data/Cisco-IOS-XE-native:native/", auth=(
self.UserName, self.Password), verify=False, headers=self.headers).text
return result
async def CreateVRF(self):
payload = {
"Cisco-IOS-XE-native:vrf": [
{
"name": f"cust-vrf-{CustomerName}",
"rd": f'{CustomerID}:{VpnId}',
"route-target": [
{
"direction": "export",
"target": f'{CustomerID}:{VpnId}'
},
{
"direction": "import",
"target": f'{CustomerID}:{VpnId}'
}
]
}
]
}
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False, limit_per_host=10), headers=self.headers) as session:
async with session.patch(url=f"https://{self.MgmtIp}/restconf/data/Cisco-IOS-XE-native:native/ip/vrf",
auth=aiohttp.BasicAuth(self.UserName, self.Password), data=json.dumps(payload)) as response:
resp = await response.text()
print('Actioning {}: Creating VRF for {}'.format(
self.HostName, CustomerName))
print(' '+crayons.green(response.status))
return resp
async def UpdateMpBGP(self):
payload = {
"Cisco-IOS-XE-bgp:vrf": [
{
"name": f"cust-vrf-{CustomerName}",
"ipv4-unicast": {
"redistribute-vrf": {
"connected": {},
"static": {}
}
}
}
]
}
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False, limit_per_host=10), headers=self.headers) as session:
async with session.patch(url=f"https://{self.MgmtIp}/restconf/data/Cisco-IOS-XE-native:native/router/Cisco-IOS-XE-bgp:bgp=65010/address-family/with-vrf/ipv4=unicast/vrf", auth=aiohttp.BasicAuth(self.UserName, self.Password), data=json.dumps(payload)) as response:
resp = await response.text()
print('Actioning {}: Creating IPv4 ADF for {}'.format(
self.HostName, CustomerName))
print(' '+crayons.green(response.status))
return resp
async def ProvisionServiceInterface(self):
# print(f'srv interface for {self.HostName}')
f = open('ipam.json', 'r+')
##################################################
# REFACTOR? / SEPERATE DB READ/WRITE FUNCTION???
###################################################
conn = json.load(f)
for x in conn['customer-peering-addresses']:
if not x['customer']:
# print(x)
x['customer'] = CustomerName
break
os.remove('ipam.json')
f = open('ipam.json', 'w')
json.dump(conn, f, indent=4)
f.close()
###################################################
##################################################
# Refactor? / Get Service Interface IP and Mask
###################################################
ip_addr = str(x['net'])+'/'+str(x['mask'])
assignablehosts = ipaddress.IPv4Network(ip_addr).hosts()
addrlist = list(assignablehosts)
netstring = ipaddress.IPv4Network(
str(x['net']+'/'+str(x['mask']))).with_netmask
###################################################
payload = {
"Cisco-IOS-XE-native:interface": {
"GigabitEthernet": [
{
"name": f"3.{CustomerID}",
"encapsulation": {
"dot1Q": {
"vlan-id": int(CustomerID)
}
},
"ip": {
"vrf": {
"forwarding": {
"word": f"cust-vrf-{CustomerName}"
}
},
"address": {
"primary": {
"address": str(addrlist[0]),
"mask": netstring.split('/')[1]
}
}
}
}
]
}}
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False, limit_per_host=10), headers=self.headers) as session:
async with session.patch(url=f"https://{self.MgmtIp}/restconf/data/Cisco-IOS-XE-native:native/interface", auth=aiohttp.BasicAuth(self.UserName, self.Password), data=json.dumps(payload)) as response:
resp = await response.text()
print('Actioning {}: Service Interface For {}'.format(
self.HostName, CustomerName))
print(' '+crayons.green(response.status))
return resp
async def main():
r1 = MplsPe('pe1', '192.168.137.200', 'craig', 'pa55w0rd1!')
r2 = MplsPe('pe2', '192.168.137.201', 'craig', 'pa55w0rd1!')
r3 = MplsPe('pe3', '192.168.137.202', 'craig', 'pa55w0rd1!')
edges = [r1, r2, r3]
for x in edges:
vrfTask = asyncio.create_task(x.CreateVRF())
await asyncio.gather(vrfTask)
await asyncio.sleep(1)
for x in edges:
bgptask = asyncio.create_task(x.UpdateMpBGP())
await asyncio.gather(bgptask)
await asyncio.sleep(3)
for x in edges:
provision = asyncio.create_task(x.ProvisionServiceInterface())
await asyncio.gather(provision)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())