Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 17 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 18 | # Copyright 2016-present Ciena Corporation |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | # you may not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 23 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 25 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
| 31 | # |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 32 | import json |
| 33 | import requests |
| 34 | import os,sys,time |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 35 | from OltConfig import OltConfig |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 36 | from CordTestUtils import get_mac, get_controller, log_test |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 37 | from EapolAAA import get_radius_macs, get_radius_networks |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 38 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 39 | class OnosCtrl: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 40 | |
| 41 | auth = ('karaf', 'karaf') |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 42 | controller = get_controller() |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 43 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 44 | maven_repo = 'http://central.maven.org/maven2/org/onosproject' |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 45 | applications_url = 'http://%s:8181/onos/v1/applications' %(controller) |
A R Karthick | 36ae025 | 2017-04-03 17:15:24 -0700 | [diff] [blame] | 46 | host_cfg_url = 'http://%s:8181/onos/v1/network/configuration/hosts/' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 47 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 48 | def __init__(self, app, controller = None): |
| 49 | self.app = app |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 50 | if controller is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 51 | self.controller = controller |
| 52 | self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app) |
| 53 | self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller) |
| 54 | self.auth = ('karaf', 'karaf') |
| 55 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 56 | @classmethod |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 57 | def config(cls, config, controller=None): |
A R Karthick | 4e0c091 | 2016-08-17 16:57:42 -0700 | [diff] [blame] | 58 | if config is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 59 | json_data = json.dumps(config) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 60 | if controller is None: |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 61 | resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data) |
| 62 | else: |
| 63 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 64 | resp = requests.post(cfg_url, auth = cls.auth, data = json_data) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 65 | return resp.ok, resp.status_code |
| 66 | return False, 400 |
| 67 | |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 68 | @classmethod |
A.R Karthick | b17e202 | 2017-01-27 11:29:26 -0800 | [diff] [blame] | 69 | def get_config(cls, controller=None): |
| 70 | if controller is None: |
| 71 | controller = cls.controller |
| 72 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
| 73 | resp = requests.get(cfg_url, auth = cls.auth) |
| 74 | if resp.ok: |
| 75 | return resp.json() |
| 76 | return None |
| 77 | |
| 78 | @classmethod |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 79 | def delete(cls, config, controller=None): |
| 80 | if config: |
| 81 | json_data = json.dumps(config) |
| 82 | if controller is None: |
| 83 | print('default Onos config url is %s'%cls.cfg_url) |
| 84 | resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data) |
| 85 | else: |
| 86 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 87 | resp = requests.delete(cfg_url, auth = cls.auth, data = json_data) |
| 88 | return resp.ok, resp.status_code |
| 89 | return False, 400 |
| 90 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 91 | def activate(self): |
| 92 | resp = requests.post(self.app_url + '/active', auth = self.auth) |
| 93 | return resp.ok, resp.status_code |
| 94 | |
| 95 | def deactivate(self): |
| 96 | resp = requests.delete(self.app_url + '/active', auth = self.auth) |
| 97 | return resp.ok, resp.status_code |
| 98 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 99 | @classmethod |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 100 | def get_devices(cls, controller = None, mfr = None): |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 101 | if controller is None: |
| 102 | controller = cls.controller |
| 103 | url = 'http://%s:8181/onos/v1/devices' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 104 | result = requests.get(url, auth = cls.auth) |
| 105 | if result.ok: |
| 106 | devices = result.json()['devices'] |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 107 | devices = filter(lambda d: d['available'], devices) |
| 108 | if mfr: |
| 109 | devices = filter(lambda d: d['mfr'].startswith(mfr), devices) |
| 110 | return devices |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 111 | return None |
| 112 | |
| 113 | @classmethod |
A.R Karthick | d4eed64 | 2017-03-09 14:40:52 -0800 | [diff] [blame] | 114 | def get_links(cls, controller = None): |
| 115 | if controller is None: |
| 116 | controller = cls.controller |
| 117 | url = 'http://%s:8181/onos/v1/links' %(controller) |
| 118 | result = requests.get(url, auth = cls.auth) |
| 119 | if result.ok: |
| 120 | links = result.json()['links'] |
| 121 | return links |
| 122 | return None |
| 123 | |
| 124 | @classmethod |
A R Karthick | a4d958a | 2017-09-05 19:01:37 -0700 | [diff] [blame] | 125 | def get_device_id(cls, controller = None, mfr = None, olt_conf_file = ''): |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 126 | '''If running under olt, we get the first switch connected to onos''' |
A R Karthick | a4d958a | 2017-09-05 19:01:37 -0700 | [diff] [blame] | 127 | olt = OltConfig(olt_conf_file = olt_conf_file) |
A R Karthick | 078e63a | 2016-07-28 13:59:31 -0700 | [diff] [blame] | 128 | did = 'of:' + get_mac() |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 129 | if olt.on_olt(): |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 130 | devices = cls.get_devices(controller = controller, mfr = mfr) |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 131 | if devices: |
| 132 | dids = map(lambda d: d['id'], devices) |
| 133 | if len(dids) == 1: |
| 134 | did = dids[0] |
| 135 | else: |
| 136 | ###If we have more than 1, then check for env before using first one |
| 137 | did = os.getenv('OLT_DEVICE_ID', dids[0]) |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 138 | |
| 139 | return did |
| 140 | |
| 141 | @classmethod |
A R Karthick | a4d958a | 2017-09-05 19:01:37 -0700 | [diff] [blame] | 142 | def get_device_ids(cls, controller = None, olt_conf_file = ''): |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 143 | '''If running under olt, we get the first switch connected to onos''' |
A R Karthick | a4d958a | 2017-09-05 19:01:37 -0700 | [diff] [blame] | 144 | olt = OltConfig(olt_conf_file = olt_conf_file) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 145 | did = 'of:' + get_mac() |
| 146 | device_ids = [] |
| 147 | if olt.on_olt(): |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 148 | devices = cls.get_devices(controller = controller) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 149 | if devices: |
| 150 | device_ids = map(lambda d: d['id'], devices) |
| 151 | else: |
| 152 | device_ids.append(did) |
| 153 | |
| 154 | return device_ids |
| 155 | |
| 156 | @classmethod |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 157 | def get_flows(cls, device_id,controller=None): |
| 158 | if controller is None: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 159 | url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 160 | else: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 161 | url = 'http://%s:8181/onos/v1/flows/' %(controller) + device_id |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 162 | result = requests.get(url, auth = cls.auth) |
| 163 | if result.ok: |
| 164 | return result.json()['flows'] |
| 165 | return None |
| 166 | |
| 167 | @classmethod |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 168 | def get_ports_device(cls, device_id, controller = None): |
| 169 | if controller is None: |
| 170 | url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(cls.controller, device_id) |
| 171 | else: |
| 172 | url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(controller, device_id) |
| 173 | |
| 174 | result = requests.get(url, auth = cls.auth) |
| 175 | if result.ok: |
| 176 | return result.json()['ports'] |
| 177 | return None |
| 178 | |
| 179 | @classmethod |
| 180 | def cord_olt_device_map(cls, olt_config, controller = None): |
| 181 | olt_device_list = [] |
| 182 | olt_port_map, _ = olt_config.olt_port_map() |
| 183 | switches = olt_port_map['switches'] |
| 184 | if len(switches) > 1: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 185 | device_ids = cls.get_device_ids(controller = controller) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 186 | else: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 187 | did = cls.get_device_id(controller = controller) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 188 | if did is None: |
| 189 | return olt_device_list |
| 190 | uplink_dict = {} |
| 191 | uplink_dict['did'] = did |
| 192 | uplink_dict['switch'] = switches[0] |
| 193 | uplink_dict['uplink'] = str(olt_config.olt_conf['uplink']) |
| 194 | uplink_dict['vlan'] = str(olt_config.olt_conf['vlan']) |
| 195 | olt_device_list.append(uplink_dict) |
| 196 | return olt_device_list |
| 197 | |
| 198 | for did in device_ids: |
| 199 | ports = cls.get_ports_device(did, controller = controller) |
| 200 | if ports: |
| 201 | matched = False |
| 202 | for port in ports: |
| 203 | for switch in switches: |
| 204 | if port['annotations']['portName'] == switch: |
| 205 | uplink_dict = {} |
| 206 | uplink = olt_port_map[switch]['uplink'] |
| 207 | uplink_dict['did'] = did |
| 208 | uplink_dict['switch'] = switch |
| 209 | uplink_dict['uplink'] = str(uplink) |
| 210 | uplink_dict['vlan'] = str(olt_config.olt_conf['vlan']) |
| 211 | olt_device_list.append(uplink_dict) |
| 212 | matched = True |
| 213 | break |
| 214 | if matched == True: |
| 215 | break |
| 216 | |
| 217 | return olt_device_list |
| 218 | |
| 219 | @classmethod |
| 220 | def cord_olt_config(cls, olt_config, controller=None): |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 221 | '''Configures OLT data for existing devices/switches''' |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 222 | did_dict = {} |
| 223 | config = { 'devices' : did_dict } |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 224 | olt_device_list = cls.cord_olt_device_map(olt_config, controller = controller) |
| 225 | if not olt_device_list: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 226 | return |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 227 | for olt_map in olt_device_list: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 228 | access_device_dict = {} |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 229 | device_data = {'uplink': olt_map['uplink'], 'vlan': olt_map['vlan']} |
| 230 | access_device_dict['accessDevice'] = device_data |
| 231 | did_dict[olt_map['did']] = access_device_dict |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 232 | |
| 233 | ##configure the device list with access information |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 234 | return cls.config(config, controller=controller) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 235 | |
| 236 | @classmethod |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 237 | def install_app(cls, app_file, onos_ip = None): |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 238 | params = {'activate':'true'} |
| 239 | headers = {'content-type':'application/octet-stream'} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 240 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 241 | with open(app_file, 'rb') as payload: |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 242 | result = requests.post(url, auth = cls.auth, |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 243 | params = params, headers = headers, |
| 244 | data = payload) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 245 | print('result.ok, result.status_code are %s and %s'%(result.ok, result.status_code)) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 246 | return result.ok, result.status_code |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 247 | |
| 248 | @classmethod |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 249 | def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None): |
| 250 | params = {'activate':'true'} |
| 251 | headers = {'content-type':'application/json'} |
| 252 | if app_url is None: |
| 253 | app_oar_file = '{}-{}.oar'.format(app_name, app_version) |
| 254 | app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file) |
| 255 | params['url'] = app_url |
| 256 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 257 | result = requests.post(url, auth = cls.auth, |
| 258 | json = params, headers = headers) |
| 259 | return result.ok, result.status_code |
| 260 | |
| 261 | @classmethod |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 262 | def uninstall_app(cls, app_name, onos_ip = None): |
| 263 | params = {'activate':'true'} |
| 264 | headers = {'content-type':'application/octet-stream'} |
| 265 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 266 | app_url = '{}/{}'.format(url, app_name) |
| 267 | resp = requests.delete(app_url, auth = cls.auth) |
| 268 | return resp.ok, resp.status_code |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 269 | |
| 270 | @classmethod |
A R Karthick | 36ae025 | 2017-04-03 17:15:24 -0700 | [diff] [blame] | 271 | def host_config(cls, config, onos_ip=None): |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 272 | if config: |
| 273 | json_data = json.dumps(config) |
A R Karthick | 36ae025 | 2017-04-03 17:15:24 -0700 | [diff] [blame] | 274 | url = cls.host_cfg_url if onos_ip is None else 'http://{}:8181/onos/v1/network/configuration/hosts/'.format(onos_ip) |
| 275 | resp = requests.post(url, auth = cls.auth, data = json_data) |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 276 | return resp.ok, resp.status_code |
| 277 | return False, 400 |
A R Karthick | 38d5df4 | 2017-07-10 13:33:26 -0700 | [diff] [blame] | 278 | |
| 279 | @classmethod |
| 280 | def config_device_driver(cls, controller = None, dids = None, driver = 'pmc-olt'): |
| 281 | driver_apps = ('org.onosproject.drivers', 'org.onosproject.openflow-base',) |
| 282 | if dids is None: |
| 283 | dids = cls.get_device_ids(controller = controller) |
| 284 | device_map = {} |
| 285 | for did in dids: |
| 286 | device_map[did] = { 'basic' : { 'driver' : driver } } |
| 287 | network_cfg = { 'devices' : device_map } |
| 288 | cls.config(network_cfg) |
| 289 | for driver in driver_apps: |
| 290 | cls(driver).deactivate() |
| 291 | time.sleep(2) |
| 292 | for driver in driver_apps: |
| 293 | cls(driver).activate() |
| 294 | time.sleep(5) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 295 | |
| 296 | @classmethod |
| 297 | def device_id_to_mac(cls, device_id): |
| 298 | device_mac_raw = device_id[-12:] |
| 299 | hwaddrs = [] |
| 300 | for i in xrange(0, 12, 2): |
| 301 | hwaddrs.append(device_mac_raw[i:i+2]) |
| 302 | |
| 303 | device_mac = ':'.join(hwaddrs) |
| 304 | return device_mac |
| 305 | |
| 306 | @classmethod |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 307 | def aaa_load_config(cls, controller = None, olt_conf_file = '', conn_type = 'socket'): |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 308 | ovs_devices = cls.get_devices(controller = controller, mfr = 'Nicira') |
| 309 | if not ovs_devices: |
| 310 | log_test.info('No OVS devices found to configure AAA connect points') |
| 311 | return |
| 312 | olt = OltConfig(olt_conf_file = olt_conf_file) |
| 313 | port_map, _ = olt.olt_port_map() |
| 314 | app = 'org.opencord.aaa' |
| 315 | cfg = { 'apps' : { app : { 'AAA' : {} } } } |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 316 | if conn_type == 'socket': |
| 317 | customizer = 'default' |
| 318 | else: |
| 319 | customizer = 'sample' |
| 320 | aaa_cfg = dict(radiusConnectionType = conn_type, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 321 | radiusSecret = 'radius_password', |
| 322 | radiusServerPort = '1812', |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 323 | packetCustomizer = customizer, |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 324 | vlanId = -1) |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 325 | radius_networks = get_radius_networks(len(port_map['switch_radius_port_list'])) |
| 326 | index = 0 |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 327 | for switch, ports in port_map['switch_radius_port_list']: |
| 328 | radius_macs = get_radius_macs(len(ports)) |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 329 | prefix, _, _ = radius_networks[index] |
| 330 | index += 1 |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 331 | aaa_cfg['nasIp'] = controller or cls.controller |
| 332 | aaa_cfg['nasMac'] = radius_macs[0] |
| 333 | aaa_cfg['radiusMac'] = radius_macs[0] |
| 334 | connect_points = [] |
| 335 | radius_port = port_map[ ports[0] ] |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 336 | radius_ip = '{}.{}'.format(prefix, radius_port) |
| 337 | if conn_type == 'socket': |
| 338 | radius_ip = os.getenv('ONOS_AAA_IP') |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 339 | aaa_cfg['radiusIp'] = radius_ip |
| 340 | for dev in ovs_devices: |
A R Karthick | e723209 | 2017-09-07 18:01:33 -0700 | [diff] [blame] | 341 | device_id = dev['id'] |
| 342 | ports = OnosCtrl.get_ports_device(device_id, controller = controller) |
| 343 | radius_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and \ |
| 344 | p['annotations']['portName'].startswith('r'), |
| 345 | ports) |
| 346 | radius_port = radius_ports[0]['port'] |
| 347 | connect_point = '{}/{}'.format(device_id, radius_port) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 348 | connect_points.append(connect_point) |
| 349 | aaa_cfg['radiusServerConnectPoints'] = connect_points |
| 350 | break |
| 351 | |
| 352 | cfg['apps'][app]['AAA'] = aaa_cfg |
| 353 | cls.config(cfg, controller = controller) |
| 354 | |
| 355 | @classmethod |
| 356 | def get_ovs_switch_map(cls, controller = None, olt_conf_file = ''): |
| 357 | port_map = None |
| 358 | #build ovs switch map |
| 359 | if olt_conf_file: |
| 360 | olt = OltConfig(olt_conf_file = olt_conf_file) |
| 361 | port_map, _ = olt.olt_port_map() |
| 362 | |
| 363 | devices = cls.get_devices(controller = controller, mfr = 'Nicira') |
| 364 | switch_map = {} |
| 365 | for dev in devices: |
| 366 | device_id = dev['id'] |
| 367 | serial = dev['serial'] |
| 368 | ports = cls.get_ports_device(dev['id'], controller = controller) |
| 369 | ports = filter(lambda p: p['isEnabled'] and 'annotations' in p, ports) |
| 370 | #just create dummy ctag/uni port numbers |
| 371 | onu_ports = [1] * len(ports) |
| 372 | onu_names = map(lambda p: p['annotations']['portName'], ports) |
| 373 | onu_macs = map(lambda p: p['annotations']['portMac'], ports) |
| 374 | switch_map[device_id] = dict(uplink_vlan = 1, |
| 375 | serial = serial, |
| 376 | ports = onu_ports, |
| 377 | names = onu_names, |
| 378 | macs = onu_macs) |
| 379 | return switch_map |
| 380 | |
| 381 | @classmethod |
| 382 | def sadis_load_config(cls, controller = None, olt_switch_map = {}, olt_conf_file = ''): |
| 383 | sadis_app = 'org.opencord.sadis' |
| 384 | aaa_app = 'org.opencord.aaa' |
| 385 | sadis_cfg = { |
| 386 | 'apps' : { |
| 387 | sadis_app : { |
| 388 | 'sadis' : { |
| 389 | 'integration' : { |
| 390 | 'cache' : { |
| 391 | 'enabled' : False, |
| 392 | 'maxsize' : 50, |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 393 | 'ttl' : 'PT0m', |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 394 | }, |
| 395 | }, |
| 396 | 'entries' : [], |
| 397 | }, |
| 398 | }, |
| 399 | } |
| 400 | } |
| 401 | sadis_entries = sadis_cfg['apps'][sadis_app]['sadis']['entries'] |
| 402 | nasId = '1/1/2' |
| 403 | nasPortId = '1/1/2' |
| 404 | switch_map = olt_switch_map.copy() |
| 405 | ovs_switch_map = cls.get_ovs_switch_map(controller = controller, |
| 406 | olt_conf_file = olt_conf_file) |
| 407 | #log_test.info('OVS switch map: %s' %ovs_switch_map) |
| 408 | switch_map.update(ovs_switch_map) |
| 409 | for device, entries in switch_map.iteritems(): |
| 410 | uni_ports = entries['ports'] |
| 411 | uni_port_names = entries['names'] |
| 412 | uni_port_macs = entries['macs'] |
| 413 | s_tag = entries['uplink_vlan'] |
| 414 | serial = entries['serial'] |
| 415 | #add entries for uni ports and device |
| 416 | for p in xrange(len(uni_ports)): |
| 417 | sadis_entry = dict(nasId = nasId, nasPortId = nasPortId, slot = 1) |
| 418 | sadis_entry['id'] = uni_port_names[p] |
| 419 | sadis_entry['hardwareIdentifier'] = uni_port_macs[p] |
| 420 | sadis_entry['cTag'] = uni_ports[p] |
| 421 | sadis_entry['sTag'] = s_tag |
| 422 | sadis_entry['port'] = uni_ports[p] |
| 423 | sadis_entry['ipAddress'] = controller or cls.controller |
| 424 | sadis_entries.append(sadis_entry) |
| 425 | #add entry for the device itself |
| 426 | sadis_entry = dict(nasId = nasId, nasPortId = nasPortId, slot = 1) |
| 427 | sadis_entry['id'] = serial |
| 428 | sadis_entry['hardwareIdentifier'] = cls.device_id_to_mac(device) |
| 429 | sadis_entry['cTag'] = uni_ports[p] |
| 430 | sadis_entry['sTag'] = s_tag |
| 431 | sadis_entry['port'] = uni_ports[p] |
| 432 | sadis_entry['ipAddress'] = controller or cls.controller |
| 433 | sadis_entries.append(sadis_entry) |
| 434 | |
| 435 | #log_test.info('Sadis cfg: %s' %json.dumps(sadis_cfg, indent=4)) |
| 436 | cls.config(sadis_cfg, controller = controller) |
| 437 | |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 438 | @classmethod |
| 439 | def config_olt_access(cls, uplink_vlan, controller = None, defaultVlan = '0', olt_conf_file = ''): |
| 440 | olt = OltConfig(olt_conf_file = olt_conf_file) |
| 441 | port_map, _ = olt.olt_port_map() |
| 442 | uplink = str(port_map['uplink']) |
| 443 | device_config = { 'devices' : {} } |
| 444 | ovs_devices = cls.get_devices(controller = controller, mfr = 'Nicira') |
| 445 | for dev in ovs_devices: |
| 446 | device_id = dev['id'] |
| 447 | device_config['devices'][device_id] = {} |
| 448 | device_config['devices'][device_id]['basic'] = dict(driver = 'default') |
| 449 | device_config['devices'][device_id]['accessDevice'] = dict(uplink = uplink, |
| 450 | vlan = uplink_vlan, |
| 451 | defaultVlan = defaultVlan) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 452 | |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 453 | cls.config(device_config, controller = controller) |
A R Karthick | 2178298 | 2017-10-02 10:49:22 -0700 | [diff] [blame] | 454 | |
| 455 | @classmethod |
| 456 | def config_olt_component(cls, controller = None, enableDhcpIgmpOnProvisioning = True, defaultVlan = 0): |
| 457 | if controller is None: |
| 458 | controller = cls.controller |
| 459 | olt_property_url = 'configuration/org.opencord.olt.impl.Olt' |
| 460 | property_url = 'http://{}:8181/onos/v1/{}'.format(controller, olt_property_url) |
| 461 | cfg = dict(enableDhcpIgmpOnProvisioning = enableDhcpIgmpOnProvisioning, defaultVlan = defaultVlan) |
| 462 | resp = requests.post(property_url, auth = cls.auth, data = json.dumps(cfg)) |
| 463 | return resp.ok, resp.status_code |
A R Karthick | c0a008f | 2017-10-04 18:35:25 -0700 | [diff] [blame] | 464 | |
| 465 | @classmethod |
| 466 | def config_extraneous_flows(cls, controller = None, enable = True): |
| 467 | if controller is None: |
| 468 | controller = cls.controller |
| 469 | flow_property_url = 'configuration/org.onosproject.net.flow.impl.FlowRuleManager' |
| 470 | property_url = 'http://{}:8181/onos/v1/{}'.format(controller, flow_property_url) |
| 471 | cfg = dict(allowExtraneousRules = enable) |
| 472 | resp = requests.post(property_url, auth = cls.auth, data = json.dumps(cfg)) |
| 473 | return resp.ok, resp.status_code |