A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 2 | # Copyright 2016-present Ciena Corporation |
| 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 |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 | # |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 16 | import json |
| 17 | import requests |
| 18 | import os,sys,time |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 19 | from OltConfig import OltConfig |
| 20 | import fcntl, socket, struct |
| 21 | |
A R Karthick | 078e63a | 2016-07-28 13:59:31 -0700 | [diff] [blame] | 22 | def get_mac(iface = None, pad = 4): |
| 23 | if iface is None: |
| 24 | iface = os.getenv('TEST_SWITCH', 'ovsbr0') |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 25 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 26 | try: |
| 27 | info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15]))) |
| 28 | except: |
| 29 | info = ['0'] * 24 |
| 30 | s.close() |
| 31 | sep = '' |
| 32 | if pad == 0: |
| 33 | sep = ':' |
| 34 | return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]]) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 35 | |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 36 | def get_controller(): |
| 37 | controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost' |
| 38 | controller = controller.split(',')[0] |
| 39 | return controller |
| 40 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 41 | class OnosCtrl: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 42 | |
| 43 | auth = ('karaf', 'karaf') |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 44 | controller = get_controller() |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 45 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 46 | maven_repo = 'http://central.maven.org/maven2/org/onosproject' |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 47 | applications_url = 'http://%s:8181/onos/v1/applications' %(controller) |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 48 | host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 49 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 50 | def __init__(self, app, controller = None): |
| 51 | self.app = app |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 52 | if controller is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 53 | self.controller = controller |
| 54 | self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app) |
| 55 | self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller) |
| 56 | self.auth = ('karaf', 'karaf') |
| 57 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 58 | @classmethod |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 59 | def config(cls, config, controller=None): |
A R Karthick | 4e0c091 | 2016-08-17 16:57:42 -0700 | [diff] [blame] | 60 | if config is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 61 | json_data = json.dumps(config) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 62 | if controller is None: |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 63 | resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data) |
| 64 | else: |
| 65 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 66 | resp = requests.post(cfg_url, auth = cls.auth, data = json_data) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 67 | return resp.ok, resp.status_code |
| 68 | return False, 400 |
| 69 | |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 70 | @classmethod |
A.R Karthick | b17e202 | 2017-01-27 11:29:26 -0800 | [diff] [blame] | 71 | def get_config(cls, controller=None): |
| 72 | if controller is None: |
| 73 | controller = cls.controller |
| 74 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
| 75 | resp = requests.get(cfg_url, auth = cls.auth) |
| 76 | if resp.ok: |
| 77 | return resp.json() |
| 78 | return None |
| 79 | |
| 80 | @classmethod |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 81 | def delete(cls, config, controller=None): |
| 82 | if config: |
| 83 | json_data = json.dumps(config) |
| 84 | if controller is None: |
| 85 | print('default Onos config url is %s'%cls.cfg_url) |
| 86 | resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data) |
| 87 | else: |
| 88 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 89 | resp = requests.delete(cfg_url, auth = cls.auth, data = json_data) |
| 90 | return resp.ok, resp.status_code |
| 91 | return False, 400 |
| 92 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 93 | def activate(self): |
| 94 | resp = requests.post(self.app_url + '/active', auth = self.auth) |
| 95 | return resp.ok, resp.status_code |
| 96 | |
| 97 | def deactivate(self): |
| 98 | resp = requests.delete(self.app_url + '/active', auth = self.auth) |
| 99 | return resp.ok, resp.status_code |
| 100 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 101 | @classmethod |
A R Karthick | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 102 | def get_devices(cls, controller = None): |
| 103 | if controller is None: |
| 104 | controller = cls.controller |
| 105 | url = 'http://%s:8181/onos/v1/devices' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 106 | result = requests.get(url, auth = cls.auth) |
| 107 | if result.ok: |
| 108 | devices = result.json()['devices'] |
| 109 | return filter(lambda d: d['available'], devices) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 110 | |
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 | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 125 | def get_device_id(cls, controller = None): |
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''' |
| 127 | olt = OltConfig() |
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 | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 130 | devices = cls.get_devices(controller = controller) |
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 | 946141b | 2017-01-24 16:37:47 -0800 | [diff] [blame] | 142 | def get_device_ids(cls, controller = None): |
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''' |
| 144 | olt = OltConfig() |
| 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 |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -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) |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 274 | url = cls.host_cfg_url if onos_ip is None else 'http://%s:8181/onos/v1/hosts/'.format(onos_ip) |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 275 | resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data) |
| 276 | return resp.ok, resp.status_code |
| 277 | return False, 400 |