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 |
| 19 | |
| 20 | class OnosCtrl: |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 21 | |
| 22 | auth = ('karaf', 'karaf') |
| 23 | controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost' |
| 24 | cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller) |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 25 | maven_repo = 'http://central.maven.org/maven2/org/onosproject' |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 26 | applications_url = 'http://%s:8181/onos/v1/applications' %(controller) |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 27 | host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 28 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 29 | def __init__(self, app, controller = None): |
| 30 | self.app = app |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 31 | if controller is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 32 | self.controller = controller |
| 33 | self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app) |
| 34 | self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller) |
| 35 | self.auth = ('karaf', 'karaf') |
| 36 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 37 | @classmethod |
| 38 | def config(cls, config): |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 39 | if config: |
| 40 | json_data = json.dumps(config) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 41 | resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 42 | return resp.ok, resp.status_code |
| 43 | return False, 400 |
| 44 | |
| 45 | def activate(self): |
| 46 | resp = requests.post(self.app_url + '/active', auth = self.auth) |
| 47 | return resp.ok, resp.status_code |
| 48 | |
| 49 | def deactivate(self): |
| 50 | resp = requests.delete(self.app_url + '/active', auth = self.auth) |
| 51 | return resp.ok, resp.status_code |
| 52 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 53 | @classmethod |
| 54 | def get_devices(cls): |
| 55 | url = 'http://%s:8181/onos/v1/devices' %(cls.controller) |
| 56 | result = requests.get(url, auth = cls.auth) |
| 57 | if result.ok: |
| 58 | devices = result.json()['devices'] |
| 59 | return filter(lambda d: d['available'], devices) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 60 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 61 | return None |
| 62 | |
| 63 | @classmethod |
| 64 | def get_flows(cls, device_id): |
| 65 | url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id |
| 66 | result = requests.get(url, auth = cls.auth) |
| 67 | if result.ok: |
| 68 | return result.json()['flows'] |
| 69 | return None |
| 70 | |
| 71 | @classmethod |
| 72 | def cord_olt_config(cls, olt_device_data = None): |
| 73 | '''Configures OLT data for existing devices/switches''' |
| 74 | if olt_device_data is None: |
| 75 | return |
| 76 | did_dict = {} |
| 77 | config = { 'devices' : did_dict } |
| 78 | devices = cls.get_devices() |
| 79 | if not devices: |
| 80 | return |
| 81 | device_ids = map(lambda d: d['id'], devices) |
| 82 | for did in device_ids: |
| 83 | access_device_dict = {} |
| 84 | access_device_dict['accessDevice'] = olt_device_data |
| 85 | did_dict[did] = access_device_dict |
| 86 | |
| 87 | ##configure the device list with access information |
| 88 | return cls.config(config) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 89 | |
| 90 | @classmethod |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 91 | def install_app(cls, app_file, onos_ip = None): |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 92 | params = {'activate':'true'} |
| 93 | headers = {'content-type':'application/octet-stream'} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 94 | 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] | 95 | with open(app_file, 'rb') as payload: |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 96 | result = requests.post(url, auth = cls.auth, |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 97 | params = params, headers = headers, |
| 98 | data = payload) |
| 99 | return result.ok, result.status_code |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 100 | |
| 101 | @classmethod |
A.R Karthick | 95d044e | 2016-06-10 18:44:36 -0700 | [diff] [blame] | 102 | def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None): |
| 103 | params = {'activate':'true'} |
| 104 | headers = {'content-type':'application/json'} |
| 105 | if app_url is None: |
| 106 | app_oar_file = '{}-{}.oar'.format(app_name, app_version) |
| 107 | app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file) |
| 108 | params['url'] = app_url |
| 109 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 110 | result = requests.post(url, auth = cls.auth, |
| 111 | json = params, headers = headers) |
| 112 | return result.ok, result.status_code |
| 113 | |
| 114 | @classmethod |
A R Karthick | b7e8090 | 2016-05-17 09:38:31 -0700 | [diff] [blame] | 115 | def uninstall_app(cls, app_name, onos_ip = None): |
| 116 | params = {'activate':'true'} |
| 117 | headers = {'content-type':'application/octet-stream'} |
| 118 | url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip) |
| 119 | app_url = '{}/{}'.format(url, app_name) |
| 120 | resp = requests.delete(app_url, auth = cls.auth) |
| 121 | return resp.ok, resp.status_code |
ChetanGaonker | f9c2f8b | 2016-07-19 15:49:41 -0700 | [diff] [blame] | 122 | |
| 123 | @classmethod |
| 124 | def host_config(cls, config): |
| 125 | if config: |
| 126 | json_data = json.dumps(config) |
| 127 | resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data) |
| 128 | return resp.ok, resp.status_code |
| 129 | return False, 400 |