blob: 92437cbedba7492bc791480e2b74bc931ada5235 [file] [log] [blame]
A.R Karthick95d044e2016-06-10 18:44:36 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# 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 Karthick95d044e2016-06-10 18:44:36 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A.R Karthick95d044e2016-06-10 18:44:36 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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)
A.R Karthick95d044e2016-06-10 18:44:36 -070025 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070026 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070027
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080028 def __init__(self, app, controller = None):
29 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070030 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080031 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 Gaonkera2b87df2016-03-31 15:41:31 -070036 @classmethod
37 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080038 if config:
39 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070040 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080041 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 Gaonkera2b87df2016-03-31 15:41:31 -070052 @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 Gaonker4a25e2b2016-03-04 14:45:15 -080059
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070060 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 Gaonker93e302d2016-04-05 10:51:07 -070088
89 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -070090 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070091 params = {'activate':'true'}
92 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -070093 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 -070094 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -070095 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -070096 params = params, headers = headers,
97 data = payload)
98 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -070099
100 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700101 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 Karthickb7e80902016-05-17 09:38:31 -0700114 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