blob: e3231127126ce73ff031845ba417221bee36537b [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)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -070027 host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070028
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080029 def __init__(self, app, controller = None):
30 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070031 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080032 self.controller = controller
33 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
34 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
35 self.auth = ('karaf', 'karaf')
36
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070037 @classmethod
38 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080039 if config:
40 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070041 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080042 return resp.ok, resp.status_code
43 return False, 400
44
45 def activate(self):
46 resp = requests.post(self.app_url + '/active', auth = self.auth)
47 return resp.ok, resp.status_code
48
49 def deactivate(self):
50 resp = requests.delete(self.app_url + '/active', auth = self.auth)
51 return resp.ok, resp.status_code
52
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070053 @classmethod
54 def get_devices(cls):
55 url = 'http://%s:8181/onos/v1/devices' %(cls.controller)
56 result = requests.get(url, auth = cls.auth)
57 if result.ok:
58 devices = result.json()['devices']
59 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080060
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070061 return None
62
63 @classmethod
64 def get_flows(cls, device_id):
65 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
66 result = requests.get(url, auth = cls.auth)
67 if result.ok:
68 return result.json()['flows']
69 return None
70
71 @classmethod
72 def cord_olt_config(cls, olt_device_data = None):
73 '''Configures OLT data for existing devices/switches'''
74 if olt_device_data is None:
75 return
76 did_dict = {}
77 config = { 'devices' : did_dict }
78 devices = cls.get_devices()
79 if not devices:
80 return
81 device_ids = map(lambda d: d['id'], devices)
82 for did in device_ids:
83 access_device_dict = {}
84 access_device_dict['accessDevice'] = olt_device_data
85 did_dict[did] = access_device_dict
86
87 ##configure the device list with access information
88 return cls.config(config)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070089
90 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -070091 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070092 params = {'activate':'true'}
93 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -070094 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 -070095 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -070096 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -070097 params = params, headers = headers,
98 data = payload)
99 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700100
101 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700102 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
103 params = {'activate':'true'}
104 headers = {'content-type':'application/json'}
105 if app_url is None:
106 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
107 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
108 params['url'] = app_url
109 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
110 result = requests.post(url, auth = cls.auth,
111 json = params, headers = headers)
112 return result.ok, result.status_code
113
114 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700115 def uninstall_app(cls, app_name, onos_ip = None):
116 params = {'activate':'true'}
117 headers = {'content-type':'application/octet-stream'}
118 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
119 app_url = '{}/{}'.format(url, app_name)
120 resp = requests.delete(app_url, auth = cls.auth)
121 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700122
123 @classmethod
124 def host_config(cls, config):
125 if config:
126 json_data = json.dumps(config)
127 resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data)
128 return resp.ok, resp.status_code
129 return False, 400