Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 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) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 25 | applications_url = 'http://%s:8181/onos/v1/applications' %(controller) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 26 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 27 | def __init__(self, app, controller = None): |
| 28 | self.app = app |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 29 | if controller is not None: |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 30 | self.controller = controller |
| 31 | self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app) |
| 32 | self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller) |
| 33 | self.auth = ('karaf', 'karaf') |
| 34 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 35 | @classmethod |
| 36 | def config(cls, config): |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 37 | if config: |
| 38 | json_data = json.dumps(config) |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 39 | resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 40 | return resp.ok, resp.status_code |
| 41 | return False, 400 |
| 42 | |
| 43 | def activate(self): |
| 44 | resp = requests.post(self.app_url + '/active', auth = self.auth) |
| 45 | return resp.ok, resp.status_code |
| 46 | |
| 47 | def deactivate(self): |
| 48 | resp = requests.delete(self.app_url + '/active', auth = self.auth) |
| 49 | return resp.ok, resp.status_code |
| 50 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 51 | @classmethod |
| 52 | def get_devices(cls): |
| 53 | url = 'http://%s:8181/onos/v1/devices' %(cls.controller) |
| 54 | result = requests.get(url, auth = cls.auth) |
| 55 | if result.ok: |
| 56 | devices = result.json()['devices'] |
| 57 | return filter(lambda d: d['available'], devices) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 58 | |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 59 | return None |
| 60 | |
| 61 | @classmethod |
| 62 | def get_flows(cls, device_id): |
| 63 | url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id |
| 64 | result = requests.get(url, auth = cls.auth) |
| 65 | if result.ok: |
| 66 | return result.json()['flows'] |
| 67 | return None |
| 68 | |
| 69 | @classmethod |
| 70 | def cord_olt_config(cls, olt_device_data = None): |
| 71 | '''Configures OLT data for existing devices/switches''' |
| 72 | if olt_device_data is None: |
| 73 | return |
| 74 | did_dict = {} |
| 75 | config = { 'devices' : did_dict } |
| 76 | devices = cls.get_devices() |
| 77 | if not devices: |
| 78 | return |
| 79 | device_ids = map(lambda d: d['id'], devices) |
| 80 | for did in device_ids: |
| 81 | access_device_dict = {} |
| 82 | access_device_dict['accessDevice'] = olt_device_data |
| 83 | did_dict[did] = access_device_dict |
| 84 | |
| 85 | ##configure the device list with access information |
| 86 | return cls.config(config) |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 87 | |
| 88 | @classmethod |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 89 | def install_app(cls, app_file, onos_ip = None): |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 90 | params = {'activate':'true'} |
| 91 | headers = {'content-type':'application/octet-stream'} |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 92 | 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] | 93 | with open(app_file, 'rb') as payload: |
Chetan Gaonker | 5209fe8 | 2016-04-19 10:09:53 -0700 | [diff] [blame] | 94 | result = requests.post(url, auth = cls.auth, |
Chetan Gaonker | 93e302d | 2016-04-05 10:51:07 -0700 | [diff] [blame] | 95 | params = params, headers = headers, |
| 96 | data = payload) |
| 97 | return result.ok, result.status_code |