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