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 |
| 59 | def config(cls, config): |
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) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 62 | resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 63 | return resp.ok, resp.status_code |
| 64 | return False, 400 |
| 65 | |
| 66 | def activate(self): |
| 67 | resp = requests.post(self.app_url + '/active', auth = self.auth) |
| 68 | return resp.ok, resp.status_code |
| 69 | |
| 70 | def deactivate(self): |
| 71 | resp = requests.delete(self.app_url + '/active', auth = self.auth) |
| 72 | return resp.ok, resp.status_code |
| 73 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 74 | @classmethod |
| 75 | def get_devices(cls): |
| 76 | url = 'http://%s:8181/onos/v1/devices' %(cls.controller) |
| 77 | result = requests.get(url, auth = cls.auth) |
| 78 | if result.ok: |
| 79 | devices = result.json()['devices'] |
| 80 | return filter(lambda d: d['available'], devices) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 81 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 82 | return None |
| 83 | |
| 84 | @classmethod |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 85 | def get_device_id(cls): |
| 86 | '''If running under olt, we get the first switch connected to onos''' |
| 87 | olt = OltConfig() |
A R Karthick | 078e63a | 2016-07-28 13:59:31 -0700 | [diff] [blame] | 88 | did = 'of:' + get_mac() |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 89 | if olt.on_olt(): |
| 90 | devices = cls.get_devices() |
| 91 | if devices: |
| 92 | dids = map(lambda d: d['id'], devices) |
| 93 | if len(dids) == 1: |
| 94 | did = dids[0] |
| 95 | else: |
| 96 | ###If we have more than 1, then check for env before using first one |
| 97 | did = os.getenv('OLT_DEVICE_ID', dids[0]) |
A R Karthick | b03cecd | 2016-07-27 10:27:55 -0700 | [diff] [blame] | 98 | |
| 99 | return did |
| 100 | |
| 101 | @classmethod |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 102 | def get_flows(cls, device_id): |
| 103 | url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id |
| 104 | result = requests.get(url, auth = cls.auth) |
| 105 | if result.ok: |
| 106 | return result.json()['flows'] |
| 107 | return None |
| 108 | |
| 109 | @classmethod |
| 110 | def cord_olt_config(cls, olt_device_data = None): |
| 111 | '''Configures OLT data for existing devices/switches''' |
| 112 | if olt_device_data is None: |
| 113 | return |
| 114 | did_dict = {} |
| 115 | config = { 'devices' : did_dict } |
| 116 | devices = cls.get_devices() |
| 117 | if not devices: |
| 118 | return |
| 119 | device_ids = map(lambda d: d['id'], devices) |
| 120 | for did in device_ids: |
| 121 | access_device_dict = {} |
| 122 | access_device_dict['accessDevice'] = olt_device_data |
| 123 | did_dict[did] = access_device_dict |
| 124 | |
| 125 | ##configure the device list with access information |
| 126 | return cls.config(config) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 127 | |
| 128 | @classmethod |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 129 | def install_app(cls, app_file, onos_ip = None): |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 130 | params = {'activate':'true'} |
| 131 | headers = {'content-type':'application/octet-stream'} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 132 | 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] | 133 | with open(app_file, 'rb') as payload: |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 134 | result = requests.post(url, auth = cls.auth, |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 135 | params = params, headers = headers, |
| 136 | data = payload) |
| 137 | return result.ok, result.status_code |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 138 | |
| 139 | @classmethod |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 140 | def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None): |
| 141 | params = {'activate':'true'} |
| 142 | headers = {'content-type':'application/json'} |
| 143 | if app_url is None: |
| 144 | app_oar_file = '{}-{}.oar'.format(app_name, app_version) |
| 145 | app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file) |
| 146 | params['url'] = app_url |
| 147 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 148 | result = requests.post(url, auth = cls.auth, |
| 149 | json = params, headers = headers) |
| 150 | return result.ok, result.status_code |
| 151 | |
| 152 | @classmethod |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 153 | def uninstall_app(cls, app_name, onos_ip = None): |
| 154 | params = {'activate':'true'} |
| 155 | headers = {'content-type':'application/octet-stream'} |
| 156 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 157 | app_url = '{}/{}'.format(url, app_name) |
| 158 | resp = requests.delete(app_url, auth = cls.auth) |
| 159 | return resp.ok, resp.status_code |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 160 | |
| 161 | @classmethod |
| 162 | def host_config(cls, config): |
| 163 | if config: |
| 164 | json_data = json.dumps(config) |
| 165 | resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data) |
| 166 | return resp.ok, resp.status_code |
| 167 | return False, 400 |