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 | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 36 | from CordTestUtils import get_mac, get_controller |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 37 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 38 | class OnosCtrl: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 39 | |
| 40 | auth = ('karaf', 'karaf') |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 41 | controller = get_controller() |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 42 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 43 | maven_repo = 'http://central.maven.org/maven2/org/onosproject' |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 44 | applications_url = 'http://%s:8181/onos/v1/applications' %(controller) |
A R Karthick | 36ae025 | 2017-04-03 17:15:24 -0700 | [diff] [blame] | 45 | host_cfg_url = 'http://%s:8181/onos/v1/network/configuration/hosts/' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 46 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 47 | def __init__(self, app, controller = None): |
| 48 | self.app = app |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 49 | if controller is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 50 | self.controller = controller |
| 51 | self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app) |
| 52 | self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller) |
| 53 | self.auth = ('karaf', 'karaf') |
| 54 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 55 | @classmethod |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 56 | def config(cls, config, controller=None): |
A R Karthick | 4e0c091 | 2016-08-17 16:57:42 -0700 | [diff] [blame] | 57 | if config is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 58 | json_data = json.dumps(config) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 59 | if controller is None: |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 60 | resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data) |
| 61 | else: |
| 62 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 63 | resp = requests.post(cfg_url, auth = cls.auth, data = json_data) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 64 | return resp.ok, resp.status_code |
| 65 | return False, 400 |
| 66 | |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 67 | @classmethod |
A.R Karthick | b17e202 | 2017-01-27 11:29:26 -0800 | [diff] [blame] | 68 | def get_config(cls, controller=None): |
| 69 | if controller is None: |
| 70 | controller = cls.controller |
| 71 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
| 72 | resp = requests.get(cfg_url, auth = cls.auth) |
| 73 | if resp.ok: |
| 74 | return resp.json() |
| 75 | return None |
| 76 | |
| 77 | @classmethod |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 78 | def delete(cls, config, controller=None): |
| 79 | if config: |
| 80 | json_data = json.dumps(config) |
| 81 | if controller is None: |
| 82 | print('default Onos config url is %s'%cls.cfg_url) |
| 83 | resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data) |
| 84 | else: |
| 85 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 86 | resp = requests.delete(cfg_url, auth = cls.auth, data = json_data) |
| 87 | return resp.ok, resp.status_code |
| 88 | return False, 400 |
| 89 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 90 | def activate(self): |
| 91 | resp = requests.post(self.app_url + '/active', auth = self.auth) |
| 92 | return resp.ok, resp.status_code |
| 93 | |
| 94 | def deactivate(self): |
| 95 | resp = requests.delete(self.app_url + '/active', auth = self.auth) |
| 96 | return resp.ok, resp.status_code |
| 97 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 98 | @classmethod |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 99 | def get_devices(cls, controller = None, mfr = None): |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 100 | if controller is None: |
| 101 | controller = cls.controller |
| 102 | url = 'http://%s:8181/onos/v1/devices' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 103 | result = requests.get(url, auth = cls.auth) |
| 104 | if result.ok: |
| 105 | devices = result.json()['devices'] |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 106 | devices = filter(lambda d: d['available'], devices) |
| 107 | if mfr: |
| 108 | devices = filter(lambda d: d['mfr'].startswith(mfr), devices) |
| 109 | return devices |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 110 | return None |
| 111 | |
| 112 | @classmethod |
A.R Karthick | d4eed64 | 2017-03-09 14:40:52 -0800 | [diff] [blame] | 113 | def get_links(cls, controller = None): |
| 114 | if controller is None: |
| 115 | controller = cls.controller |
| 116 | url = 'http://%s:8181/onos/v1/links' %(controller) |
| 117 | result = requests.get(url, auth = cls.auth) |
| 118 | if result.ok: |
| 119 | links = result.json()['links'] |
| 120 | return links |
| 121 | return None |
| 122 | |
| 123 | @classmethod |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 124 | def get_device_id(cls, controller = None, mfr = None): |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 125 | '''If running under olt, we get the first switch connected to onos''' |
| 126 | olt = OltConfig() |
A R Karthick | 078e63a | 2016-07-28 13:59:31 -0700 | [diff] [blame] | 127 | did = 'of:' + get_mac() |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 128 | if olt.on_olt(): |
A R Karthick | 861ef16 | 2017-08-22 10:45:26 -0700 | [diff] [blame] | 129 | devices = cls.get_devices(controller = controller, mfr = mfr) |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 130 | if devices: |
| 131 | dids = map(lambda d: d['id'], devices) |
| 132 | if len(dids) == 1: |
| 133 | did = dids[0] |
| 134 | else: |
| 135 | ###If we have more than 1, then check for env before using first one |
| 136 | did = os.getenv('OLT_DEVICE_ID', dids[0]) |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 137 | |
| 138 | return did |
| 139 | |
| 140 | @classmethod |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 141 | def get_device_ids(cls, controller = None): |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 142 | '''If running under olt, we get the first switch connected to onos''' |
| 143 | olt = OltConfig() |
| 144 | did = 'of:' + get_mac() |
| 145 | device_ids = [] |
| 146 | if olt.on_olt(): |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 147 | devices = cls.get_devices(controller = controller) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 148 | if devices: |
| 149 | device_ids = map(lambda d: d['id'], devices) |
| 150 | else: |
| 151 | device_ids.append(did) |
| 152 | |
| 153 | return device_ids |
| 154 | |
| 155 | @classmethod |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 156 | def get_flows(cls, device_id,controller=None): |
| 157 | if controller is None: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 158 | url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 159 | else: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 160 | url = 'http://%s:8181/onos/v1/flows/' %(controller) + device_id |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 161 | result = requests.get(url, auth = cls.auth) |
| 162 | if result.ok: |
| 163 | return result.json()['flows'] |
| 164 | return None |
| 165 | |
| 166 | @classmethod |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 167 | def get_ports_device(cls, device_id, controller = None): |
| 168 | if controller is None: |
| 169 | url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(cls.controller, device_id) |
| 170 | else: |
| 171 | url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(controller, device_id) |
| 172 | |
| 173 | result = requests.get(url, auth = cls.auth) |
| 174 | if result.ok: |
| 175 | return result.json()['ports'] |
| 176 | return None |
| 177 | |
| 178 | @classmethod |
| 179 | def cord_olt_device_map(cls, olt_config, controller = None): |
| 180 | olt_device_list = [] |
| 181 | olt_port_map, _ = olt_config.olt_port_map() |
| 182 | switches = olt_port_map['switches'] |
| 183 | if len(switches) > 1: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 184 | device_ids = cls.get_device_ids(controller = controller) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 185 | else: |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 186 | did = cls.get_device_id(controller = controller) |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 187 | if did is None: |
| 188 | return olt_device_list |
| 189 | uplink_dict = {} |
| 190 | uplink_dict['did'] = did |
| 191 | uplink_dict['switch'] = switches[0] |
| 192 | uplink_dict['uplink'] = str(olt_config.olt_conf['uplink']) |
| 193 | uplink_dict['vlan'] = str(olt_config.olt_conf['vlan']) |
| 194 | olt_device_list.append(uplink_dict) |
| 195 | return olt_device_list |
| 196 | |
| 197 | for did in device_ids: |
| 198 | ports = cls.get_ports_device(did, controller = controller) |
| 199 | if ports: |
| 200 | matched = False |
| 201 | for port in ports: |
| 202 | for switch in switches: |
| 203 | if port['annotations']['portName'] == switch: |
| 204 | uplink_dict = {} |
| 205 | uplink = olt_port_map[switch]['uplink'] |
| 206 | uplink_dict['did'] = did |
| 207 | uplink_dict['switch'] = switch |
| 208 | uplink_dict['uplink'] = str(uplink) |
| 209 | uplink_dict['vlan'] = str(olt_config.olt_conf['vlan']) |
| 210 | olt_device_list.append(uplink_dict) |
| 211 | matched = True |
| 212 | break |
| 213 | if matched == True: |
| 214 | break |
| 215 | |
| 216 | return olt_device_list |
| 217 | |
| 218 | @classmethod |
| 219 | def cord_olt_config(cls, olt_config, controller=None): |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 220 | '''Configures OLT data for existing devices/switches''' |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 221 | did_dict = {} |
| 222 | config = { 'devices' : did_dict } |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 223 | olt_device_list = cls.cord_olt_device_map(olt_config, controller = controller) |
| 224 | if not olt_device_list: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 225 | return |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 226 | for olt_map in olt_device_list: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 227 | access_device_dict = {} |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 228 | device_data = {'uplink': olt_map['uplink'], 'vlan': olt_map['vlan']} |
| 229 | access_device_dict['accessDevice'] = device_data |
| 230 | did_dict[olt_map['did']] = access_device_dict |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 231 | |
| 232 | ##configure the device list with access information |
A R Karthick | 0f6b684 | 2016-12-06 17:17:44 -0800 | [diff] [blame] | 233 | return cls.config(config, controller=controller) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 234 | |
| 235 | @classmethod |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 236 | def install_app(cls, app_file, onos_ip = None): |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 237 | params = {'activate':'true'} |
| 238 | headers = {'content-type':'application/octet-stream'} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 239 | 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] | 240 | with open(app_file, 'rb') as payload: |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 241 | result = requests.post(url, auth = cls.auth, |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 242 | params = params, headers = headers, |
| 243 | data = payload) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 244 | 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] | 245 | return result.ok, result.status_code |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 246 | |
| 247 | @classmethod |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 248 | def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None): |
| 249 | params = {'activate':'true'} |
| 250 | headers = {'content-type':'application/json'} |
| 251 | if app_url is None: |
| 252 | app_oar_file = '{}-{}.oar'.format(app_name, app_version) |
| 253 | app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file) |
| 254 | params['url'] = app_url |
| 255 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 256 | result = requests.post(url, auth = cls.auth, |
| 257 | json = params, headers = headers) |
| 258 | return result.ok, result.status_code |
| 259 | |
| 260 | @classmethod |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 261 | def uninstall_app(cls, app_name, onos_ip = None): |
| 262 | params = {'activate':'true'} |
| 263 | headers = {'content-type':'application/octet-stream'} |
| 264 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 265 | app_url = '{}/{}'.format(url, app_name) |
| 266 | resp = requests.delete(app_url, auth = cls.auth) |
| 267 | return resp.ok, resp.status_code |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 268 | |
| 269 | @classmethod |
A R Karthick | 36ae025 | 2017-04-03 17:15:24 -0700 | [diff] [blame] | 270 | def host_config(cls, config, onos_ip=None): |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 271 | if config: |
| 272 | json_data = json.dumps(config) |
A R Karthick | 36ae025 | 2017-04-03 17:15:24 -0700 | [diff] [blame] | 273 | url = cls.host_cfg_url if onos_ip is None else 'http://{}:8181/onos/v1/network/configuration/hosts/'.format(onos_ip) |
| 274 | resp = requests.post(url, auth = cls.auth, data = json_data) |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 275 | return resp.ok, resp.status_code |
| 276 | return False, 400 |
A R Karthick | 38d5df4 | 2017-07-10 13:33:26 -0700 | [diff] [blame] | 277 | |
| 278 | @classmethod |
| 279 | def config_device_driver(cls, controller = None, dids = None, driver = 'pmc-olt'): |
| 280 | driver_apps = ('org.onosproject.drivers', 'org.onosproject.openflow-base',) |
| 281 | if dids is None: |
| 282 | dids = cls.get_device_ids(controller = controller) |
| 283 | device_map = {} |
| 284 | for did in dids: |
| 285 | device_map[did] = { 'basic' : { 'driver' : driver } } |
| 286 | network_cfg = { 'devices' : device_map } |
| 287 | cls.config(network_cfg) |
| 288 | for driver in driver_apps: |
| 289 | cls(driver).deactivate() |
| 290 | time.sleep(2) |
| 291 | for driver in driver_apps: |
| 292 | cls(driver).activate() |
| 293 | time.sleep(5) |