blob: 880e8f61c749a2280b60711d02150b73786648b1 [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
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 Gaonker4a25e2b2016-03-04 14:45:15 -080016import json
17import requests
18import os,sys,time
19
20class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070021
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 Gaonker93e302d2016-04-05 10:51:07 -070025 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070026
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080027 def __init__(self, app, controller = None):
28 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070029 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080030 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 Gaonkera2b87df2016-03-31 15:41:31 -070035 @classmethod
36 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080037 if config:
38 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070039 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080040 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 Gaonkera2b87df2016-03-31 15:41:31 -070051 @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 Gaonker4a25e2b2016-03-04 14:45:15 -080058
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070059 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 Gaonker93e302d2016-04-05 10:51:07 -070087
88 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -070089 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070090 params = {'activate':'true'}
91 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -070092 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 -070093 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -070094 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -070095 params = params, headers = headers,
96 data = payload)
97 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -070098
99 @classmethod
100 def uninstall_app(cls, app_name, onos_ip = None):
101 params = {'activate':'true'}
102 headers = {'content-type':'application/octet-stream'}
103 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
104 app_url = '{}/{}'.format(url, app_name)
105 resp = requests.delete(app_url, auth = cls.auth)
106 return resp.ok, resp.status_code