-
Notifications
You must be signed in to change notification settings - Fork 3
/
configs_lib.py
153 lines (135 loc) · 5.55 KB
/
configs_lib.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
"""Configuration module for Mist/Arista provisioning.
One 'could' populate a list of APs from a given heatmap for multi-ap
configuration.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import logging
from absl import flags
import constants
import gnmi_lib
import six
import sys
import json
import pyangbind.lib.pybindJSON as pybindJSON
from ap_manager_bindings import openconfig_ap_manager
from access_points_bindings import openconfig_access_points
ap_manager_configs = openconfig_ap_manager()
access_point_configs = openconfig_access_points()
FLAGS = flags.FLAGS
class Error(Exception):
"""Module-level Exception class."""
def GnmiSetUp(ap):
"""Set up gNMI channel for each AP.
Args:
ap: AP Class object.
"""
if ap.targetport == '443': # Target is ap-mgr.
creds = gnmi_lib.CreateCreds(ap.targetip, ap.targetport, None, None,
None, None)
ap.stub = gnmi_lib.CreateStub(creds, ap.targetip, ap.targetport,
'openconfig.mist.com')
elif ap.targetport == '8080': # Targt is AP
creds = gnmi_lib.CreateCreds(ap.targetip, ap.targetport, ' ', None,
None, None)
ap.stub = gnmi_lib.CreateStub(creds, ap.targetip, ap.targetport,
'openconfig.mojonetworks.com')
elif ap.targetport == '10161': # Target is ap-mgr.
creds = gnmi_lib.CreateCreds(ap.targetip, ap.targetport, root_cert)
ap.stub = gnmi_lib.CreateStub(creds, ap.targetip, ap.targetport,
'openconfig.cisco.com')
elif ap.targetport == '10181': # Targt is AP
creds = gnmi_lib.CreateCreds(ap.targetip, ap.targetport, root_cert)
ap.stub = gnmi_lib.CreateStub(creds, ap.targetip, ap.targetport,
'openconfig.arubanetworks.com')
def Provision(ap):
"""Triggers the provisioning workflow.
Args:
ap: AP Class object.
This populates the conig object (from PyangBind, from YANG model) for day-0
provisioning.
"""
paths = gnmi_lib.ParsePath(gnmi_lib.PathNames((
'/provision-aps/provision-ap[mac=%s]/' % ap.mac)))
provision_apconfigs = ap_manager_configs.provision_aps.provision_ap
day0 = provision_apconfigs.add(ap.mac)
day0.config.mac = ap.mac
day0.config.hostname = ap.ap_name
day0.config.country_code = 'US'
json_value = json.loads(pybindJSON.dumps(day0, mode='ietf', indent=2))
if FLAGS.dry_run:
print('Here\'s the JSON that was created, for sending to the Target:')
print('*'*25, '\n\n', json.dumps(json_value, indent=2),'\n\n', '*'*25)
print('JSON written as dry_run_provision.json')
f = six.moves.builtins.open('dry_run_provision.json', 'w')
f.write(json.dumps(json_value, indent=2) + '\n')
sys.exit()
else:
r = gnmi_lib.Set(ap.stub, paths, ap.targetuser, ap.targetpass, json_value, 'update')
print('provisioning succesfull, with the following gNMI SetResponse:\n', r)
def ConfigPhyMac(ap, student_ssids):
"""Triggers the configuration of PHY and MAC layer.
Args:
ap: AP Class object.
student_ssids: (list) SSIDs to configure on the AP.
This populates the conig object (from PyangBind, from YANG model) for day-1+
configuration.
"""
paths = gnmi_lib.ParsePath(gnmi_lib.PathNames((
'/access-points/access-point[hostname=%s]/' % ap.ap_name)))
ap_configs = access_point_configs.access_points.access_point
ap_configs.add(ap.ap_name)
open_ssid = ap_configs[ap.ap_name].ssids.ssid.add(ap.openssid)
open_ssid.config.name = ap.openssid
open_ssid.config.enabled = True
open_ssid.config.hidden = False
open_ssid.config.operating_frequency = 'FREQ_5GHZ'
open_ssid.config.opmode = 'OPEN'
open_ssid.wmm.config.trust_dscp = True
psk_ssid = ap_configs[ap.ap_name].ssids.ssid.add(ap.pskssid)
psk_ssid.config.enabled = True
psk_ssid.config.name = ap.pskssid
psk_ssid.config.hidden = False
psk_ssid.config.operating_frequency = 'FREQ_2_5_GHZ'
psk_ssid.config.opmode = 'WPA2_PERSONAL'
psk_ssid.config.wpa2_psk = 'testing123'
psk_ssid.wmm.config.trust_dscp = True
# PHY Layer stuff.
fiveg = ap_configs[ap.ap_name].radios.radio.add(0)
fiveg.config.id = 0
fiveg.config.operating_frequency = 'FREQ_5GHZ'
fiveg.config.enabled = True
fiveg.config.dca = False
fiveg.config.transmit_power = 3
fiveg.config.channel_width = 20
fiveg.config.channel = 165
twog = ap_configs[ap.ap_name].radios.radio.add(1)
twog.config.id = 1
twog.config.operating_frequency = 'FREQ_2GHZ'
twog.config.enabled = True
twog.config.dca = False
twog.config.transmit_power = 3
twog.config.channel_width = 20
twog.config.channel = 6
json_value = _int_fixer(json.loads(pybindJSON.dumps(
access_point_configs, mode='ietf', indent=2))['openconfig-access-points:access-points']['access-point'][0])
if FLAGS.dry_run:
print('Here\'s the JSON that was created, for sending to the Target:')
print('*'*25, '\n\n', json.dumps(json_value, indent=2),'\n\n', '*'*25)
print('JSON written as dry_run_configs.json')
f = six.moves.builtins.open('dry_run_configs.json', 'w')
f.write(json.dumps(json_value, indent=2) + '\n')
sys.exit()
else:
r = gnmi_lib.Set(ap.stub, paths, ap.targetuser, ap.targetpass, json_value, 'update')
print('Configuration succesfull, with the following gNMI SetResponse:\n', r)
return json.dumps(json_value)
def _int_fixer(js):
"""Small helper to fix integer-as-string PyangBind bug."""
for radio in js['radios']['radio']:
if radio['id'] == "0":
radio['id'] = 0
if radio['id'] == "1":
radio['id'] = 1
return js