blob: 7f2277ec462c236a6b38a64160d1e1918e71eab0 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
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
16
A.R Karthick95d044e2016-06-10 18:44:36 -070017#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070018# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
A.R Karthick95d044e2016-06-10 18:44:36 -070023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A.R Karthick95d044e2016-06-10 18:44:36 -070025#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070026# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080032import json
33import requests
34import os,sys,time
A R Karthickb03cecd2016-07-27 10:27:55 -070035from OltConfig import OltConfig
A.R Karthickbe7768c2017-03-17 11:39:41 -070036from CordTestUtils import get_mac, get_controller
A R Karthick2b93d6a2016-09-06 15:19:09 -070037
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080038class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070039
40 auth = ('karaf', 'karaf')
A R Karthick2b93d6a2016-09-06 15:19:09 -070041 controller = get_controller()
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070042 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A.R Karthick95d044e2016-06-10 18:44:36 -070043 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070044 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
A R Karthick36ae0252017-04-03 17:15:24 -070045 host_cfg_url = 'http://%s:8181/onos/v1/network/configuration/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070046
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080047 def __init__(self, app, controller = None):
48 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070049 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080050 self.controller = controller
51 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
52 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
53 self.auth = ('karaf', 'karaf')
54
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070055 @classmethod
A R Karthickbd82f362016-11-10 15:08:52 -080056 def config(cls, config, controller=None):
A R Karthick4e0c0912016-08-17 16:57:42 -070057 if config is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080058 json_data = json.dumps(config)
ChetanGaonker689b3862016-10-17 16:25:01 -070059 if controller is None:
ChetanGaonker689b3862016-10-17 16:25:01 -070060 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
61 else:
62 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
ChetanGaonker689b3862016-10-17 16:25:01 -070063 resp = requests.post(cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080064 return resp.ok, resp.status_code
65 return False, 400
66
A R Karthickbd82f362016-11-10 15:08:52 -080067 @classmethod
A.R Karthickb17e2022017-01-27 11:29:26 -080068 def get_config(cls, controller=None):
69 if controller is None:
70 controller = cls.controller
71 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
72 resp = requests.get(cfg_url, auth = cls.auth)
73 if resp.ok:
74 return resp.json()
75 return None
76
77 @classmethod
A R Karthickbd82f362016-11-10 15:08:52 -080078 def delete(cls, config, controller=None):
79 if config:
80 json_data = json.dumps(config)
81 if controller is None:
82 print('default Onos config url is %s'%cls.cfg_url)
83 resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data)
84 else:
85 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A R Karthickbd82f362016-11-10 15:08:52 -080086 resp = requests.delete(cfg_url, auth = cls.auth, data = json_data)
87 return resp.ok, resp.status_code
88 return False, 400
89
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080090 def activate(self):
91 resp = requests.post(self.app_url + '/active', auth = self.auth)
92 return resp.ok, resp.status_code
93
94 def deactivate(self):
95 resp = requests.delete(self.app_url + '/active', auth = self.auth)
96 return resp.ok, resp.status_code
97
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070098 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -080099 def get_devices(cls, controller = None):
100 if controller is None:
101 controller = cls.controller
102 url = 'http://%s:8181/onos/v1/devices' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700103 result = requests.get(url, auth = cls.auth)
104 if result.ok:
105 devices = result.json()['devices']
106 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800107
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700108 return None
109
110 @classmethod
A.R Karthickd4eed642017-03-09 14:40:52 -0800111 def get_links(cls, controller = None):
112 if controller is None:
113 controller = cls.controller
114 url = 'http://%s:8181/onos/v1/links' %(controller)
115 result = requests.get(url, auth = cls.auth)
116 if result.ok:
117 links = result.json()['links']
118 return links
119 return None
120
121 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800122 def get_device_id(cls, controller = None):
A R Karthickb03cecd2016-07-27 10:27:55 -0700123 '''If running under olt, we get the first switch connected to onos'''
124 olt = OltConfig()
A R Karthick078e63a2016-07-28 13:59:31 -0700125 did = 'of:' + get_mac()
A R Karthickb03cecd2016-07-27 10:27:55 -0700126 if olt.on_olt():
A R Karthick946141b2017-01-24 16:37:47 -0800127 devices = cls.get_devices(controller = controller)
A R Karthickb03cecd2016-07-27 10:27:55 -0700128 if devices:
129 dids = map(lambda d: d['id'], devices)
130 if len(dids) == 1:
131 did = dids[0]
132 else:
133 ###If we have more than 1, then check for env before using first one
134 did = os.getenv('OLT_DEVICE_ID', dids[0])
A R Karthickb03cecd2016-07-27 10:27:55 -0700135
136 return did
137
138 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800139 def get_device_ids(cls, controller = None):
A R Karthick0f6b6842016-12-06 17:17:44 -0800140 '''If running under olt, we get the first switch connected to onos'''
141 olt = OltConfig()
142 did = 'of:' + get_mac()
143 device_ids = []
144 if olt.on_olt():
A R Karthick946141b2017-01-24 16:37:47 -0800145 devices = cls.get_devices(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800146 if devices:
147 device_ids = map(lambda d: d['id'], devices)
148 else:
149 device_ids.append(did)
150
151 return device_ids
152
153 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700154 def get_flows(cls, device_id,controller=None):
155 if controller is None:
A R Karthick946141b2017-01-24 16:37:47 -0800156 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
ChetanGaonker689b3862016-10-17 16:25:01 -0700157 else:
A R Karthick946141b2017-01-24 16:37:47 -0800158 url = 'http://%s:8181/onos/v1/flows/' %(controller) + device_id
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700159 result = requests.get(url, auth = cls.auth)
160 if result.ok:
161 return result.json()['flows']
162 return None
163
164 @classmethod
A R Karthick0f6b6842016-12-06 17:17:44 -0800165 def get_ports_device(cls, device_id, controller = None):
166 if controller is None:
167 url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(cls.controller, device_id)
168 else:
169 url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(controller, device_id)
170
171 result = requests.get(url, auth = cls.auth)
172 if result.ok:
173 return result.json()['ports']
174 return None
175
176 @classmethod
177 def cord_olt_device_map(cls, olt_config, controller = None):
178 olt_device_list = []
179 olt_port_map, _ = olt_config.olt_port_map()
180 switches = olt_port_map['switches']
181 if len(switches) > 1:
A R Karthick946141b2017-01-24 16:37:47 -0800182 device_ids = cls.get_device_ids(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800183 else:
A R Karthick946141b2017-01-24 16:37:47 -0800184 did = cls.get_device_id(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800185 if did is None:
186 return olt_device_list
187 uplink_dict = {}
188 uplink_dict['did'] = did
189 uplink_dict['switch'] = switches[0]
190 uplink_dict['uplink'] = str(olt_config.olt_conf['uplink'])
191 uplink_dict['vlan'] = str(olt_config.olt_conf['vlan'])
192 olt_device_list.append(uplink_dict)
193 return olt_device_list
194
195 for did in device_ids:
196 ports = cls.get_ports_device(did, controller = controller)
197 if ports:
198 matched = False
199 for port in ports:
200 for switch in switches:
201 if port['annotations']['portName'] == switch:
202 uplink_dict = {}
203 uplink = olt_port_map[switch]['uplink']
204 uplink_dict['did'] = did
205 uplink_dict['switch'] = switch
206 uplink_dict['uplink'] = str(uplink)
207 uplink_dict['vlan'] = str(olt_config.olt_conf['vlan'])
208 olt_device_list.append(uplink_dict)
209 matched = True
210 break
211 if matched == True:
212 break
213
214 return olt_device_list
215
216 @classmethod
217 def cord_olt_config(cls, olt_config, controller=None):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700218 '''Configures OLT data for existing devices/switches'''
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700219 did_dict = {}
220 config = { 'devices' : did_dict }
A R Karthick0f6b6842016-12-06 17:17:44 -0800221 olt_device_list = cls.cord_olt_device_map(olt_config, controller = controller)
222 if not olt_device_list:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700223 return
A R Karthick0f6b6842016-12-06 17:17:44 -0800224 for olt_map in olt_device_list:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700225 access_device_dict = {}
A R Karthick0f6b6842016-12-06 17:17:44 -0800226 device_data = {'uplink': olt_map['uplink'], 'vlan': olt_map['vlan']}
227 access_device_dict['accessDevice'] = device_data
228 did_dict[olt_map['did']] = access_device_dict
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700229
230 ##configure the device list with access information
A R Karthick0f6b6842016-12-06 17:17:44 -0800231 return cls.config(config, controller=controller)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700232
233 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700234 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700235 params = {'activate':'true'}
236 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700237 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 -0700238 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700239 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700240 params = params, headers = headers,
241 data = payload)
ChetanGaonker689b3862016-10-17 16:25:01 -0700242 print('result.ok, result.status_code are %s and %s'%(result.ok, result.status_code))
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700243 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700244
245 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700246 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
247 params = {'activate':'true'}
248 headers = {'content-type':'application/json'}
249 if app_url is None:
250 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
251 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
252 params['url'] = app_url
253 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
254 result = requests.post(url, auth = cls.auth,
255 json = params, headers = headers)
256 return result.ok, result.status_code
257
258 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700259 def uninstall_app(cls, app_name, onos_ip = None):
260 params = {'activate':'true'}
261 headers = {'content-type':'application/octet-stream'}
262 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
263 app_url = '{}/{}'.format(url, app_name)
264 resp = requests.delete(app_url, auth = cls.auth)
265 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700266
267 @classmethod
A R Karthick36ae0252017-04-03 17:15:24 -0700268 def host_config(cls, config, onos_ip=None):
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700269 if config:
270 json_data = json.dumps(config)
A R Karthick36ae0252017-04-03 17:15:24 -0700271 url = cls.host_cfg_url if onos_ip is None else 'http://{}:8181/onos/v1/network/configuration/hosts/'.format(onos_ip)
272 resp = requests.post(url, auth = cls.auth, data = json_data)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700273 return resp.ok, resp.status_code
274 return False, 400
A R Karthick38d5df42017-07-10 13:33:26 -0700275
276 @classmethod
277 def config_device_driver(cls, controller = None, dids = None, driver = 'pmc-olt'):
278 driver_apps = ('org.onosproject.drivers', 'org.onosproject.openflow-base',)
279 if dids is None:
280 dids = cls.get_device_ids(controller = controller)
281 device_map = {}
282 for did in dids:
283 device_map[did] = { 'basic' : { 'driver' : driver } }
284 network_cfg = { 'devices' : device_map }
285 cls.config(network_cfg)
286 for driver in driver_apps:
287 cls(driver).deactivate()
288 time.sleep(2)
289 for driver in driver_apps:
290 cls(driver).activate()
291 time.sleep(5)