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