-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.py
251 lines (205 loc) · 8.98 KB
/
router.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# for Python3 print function
from __future__ import print_function
# Jinja2 Template Engine
from jinja2 import Template, Environment
# PyEZ
from jnpr.junos import Device
from jnpr.junos.utils.config import Config
# JSNAPy
from jnpr.jsnapy import SnapAdmin
# arranged print
from pprint import pprint, pformat
class Router:
def __init__(self, hostname, model, ipaddress, username, password):
self.hostname = hostname
self.model = model
self.username = username
self.password = password
self.ipaddress = ipaddress
self.device = Device(host=ipaddress, user=username, password=password)
self.snap = SnapAdmin()
def open(self):
self.device.open()
self.device.bind(cu=Config)
def lock(self):
self.device.cu.lock()
def unlock(self):
self.device.cu.unlock()
def close(self):
self.device.close()
def commit(self):
return self.device.cu.commit()
def rollback(self):
return self.device.cu.rollback()
def diff_config(self):
if self.device.cu.diff():
message = self.device.cu.diff()
else:
message = ''
return message
def commit_check(self):
return self.device.cu.commit_check()
def load_config(self, operation_name, operation_param=None):
set_result = False
message = ''
if operation_name == 'set_add_interface':
template_filename = './set_templates/add_interface.jinja2'
tamplate_param = operation_param
elif operation_name == 'set_add_bgp_neighbor':
template_filename = './set_templates/add_bgp_neighbor.jinja2'
tamplate_param = operation_param
elif operation_name == 'set_add_bgp_policy_external':
template_filename =\
'./set_templates/add_bgp_policy_external.jinja2'
tamplate_param = operation_param
else:
pass
config_txt = self.generate_from_jinja2(
template_filename,
tamplate_param)
self.device.cu.load(
template_path=template_filename,
template_vars=tamplate_param,
format="text",
merge=True)
message = config_txt
set_result = True
return set_result, message
def nwtest(self, operation_name, operation_param=None):
nwtest_result = False
message = ''
template_filename = ''
if operation_name == 'nwtest_hostname':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = {'hostname': self.hostname}
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
self.hostname +\
'.yml'
elif operation_name == 'nwtest_model':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = {'model': self.model}
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
self.hostname +\
'.yml'
elif operation_name == 'nwtest_interface':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = operation_param
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
operation_param['interface_name'].replace('/', '-') + '_' +\
operation_param['interface_status'] +\
'.yml'
elif operation_name == 'nwtest_bgp_neighbor':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = operation_param
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
operation_param['neighbor_address_ipv4'].replace('.', '-') + '_' +\
operation_param['neighbor_status'] +\
'.yml'
elif operation_name == 'nwtest_bgp_received_route':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = operation_param
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
operation_param['neighbor_address_ipv4'].replace('.', '-') + '_' +\
operation_param['received_route_address_ipv4'].replace('.', '-') +\
'.yml'
elif operation_name == 'nwtest_bgp_advertised_route':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = operation_param
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
operation_param['neighbor_address_ipv4'].replace('.', '-') + '_' +\
operation_param['advertised_route_address_ipv4'].replace('.', '-') +\
'.yml'
elif operation_name == 'nwtest_ping':
template_filename =\
'./nwtest_templates/%s.jinja2' % (operation_name)
tamplate_param = operation_param
nwtest_filename =\
'./nwtests/' +\
operation_name + '_' +\
operation_param['target_ipaddress'].replace('.', '-') +\
'.yml'
else:
pass
self.generate_nwtestfile(
template_filename=template_filename,
template_param=tamplate_param,
nwtest_filename=nwtest_filename)
jsnapy_conf = 'nwtests:' + '\n' +\
' - %s' % (nwtest_filename)
snapcheck_dict = self.snap.snapcheck(data=jsnapy_conf, dev=self.device)
for snapcheck in snapcheck_dict:
if snapcheck.result == 'Passed':
nwtest_result = True
if operation_name == 'nwtest_bgp_received_route':
expected_value = '%s/%s' % (
operation_param['received_route_address_ipv4'],
operation_param['received_route_subnet_ipv4'])
acutual_value =\
snapcheck.test_details.values()[0][0]['passed'][0]['pre'].keys()[0]
elif operation_name == 'nwtest_bgp_advertised_route':
expected_value = '%s/%s' % (
operation_param['advertised_route_address_ipv4'],
operation_param['advertised_route_subnet_ipv4'])
acutual_value =\
snapcheck.test_details.values()[0][0]['passed'][0]['pre'].keys()[0]
else:
expected_value =\
snapcheck.test_details.values()[0][0]['expected_node_value']
acutual_value =\
snapcheck.test_details.values()[0][0]['passed'][0]['actual_node_value']
message =\
'nwtest file : %s\n' % nwtest_filename +\
'expected value : %s\n' % (expected_value) +\
'acutual value : %s' % (acutual_value)
elif snapcheck.result == 'Failed':
nwtest_result = False
if operation_name == 'nwtest_bgp_received_route':
expected_value = '%s/%s' % (
operation_param['received_route_address_ipv4'],
operation_param['received_route_subnet_ipv4'])
acutual_value = 'None'
elif operation_name == 'nwtest_bgp_advertised_route':
expected_value = '%s/%s' % (
operation_param['advertised_route_address_ipv4'],
operation_param['advertised_route_subnet_ipv4'])
acutual_value = 'None'
else:
expected_value =\
snapcheck.test_details.values()[0][0]['expected_node_value']
acutual_value =\
snapcheck.test_details.values()[0][0]['failed'][0]['actual_node_value']
message = 'nwtest file : %s\n' % nwtest_filename +\
'expected value : %s\n' % (expected_value) +\
'acutual value : %s' % (acutual_value)
return nwtest_result, message
def generate_nwtestfile(self, template_filename, template_param, nwtest_filename):
nwtest_yml = self.generate_from_jinja2(template_filename, template_param)
# write nwtest file (YAML format)
with open(nwtest_filename, 'w') as f:
f.write(nwtest_yml)
def generate_from_jinja2(self, template_filename, template_param):
# read template file (jinja2 format)
with open(template_filename, 'r') as f:
template_jinja2 = f.read()
# generate nwtest file from template file
return Environment().from_string(template_jinja2).render(template_param)