blob: f41bd6a54bc88a26d0c7f3141dad5808d2f275af [file] [log] [blame]
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -08001import json
2import requests
3import os,sys,time
4
5class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -07006
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 Gaonker93e302d2016-04-05 10:51:07 -070010 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070011
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080012 def __init__(self, app, controller = None):
13 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070014 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080015 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 Gaonkera2b87df2016-03-31 15:41:31 -070020 @classmethod
21 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080022 if config:
23 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070024 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080025 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 Gaonkera2b87df2016-03-31 15:41:31 -070036 @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 Gaonker4a25e2b2016-03-04 14:45:15 -080043
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070044 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 Gaonker93e302d2016-04-05 10:51:07 -070072
73 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -070074 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070075 params = {'activate':'true'}
76 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -070077 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070078 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -070079 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -070080 params = params, headers = headers,
81 data = payload)
82 return result.ok, result.status_code