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