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