blob: 319125e783a949f0f8d3f92c6c1e5ae20ad5ccbd [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
A R Karthickb03cecd2016-07-27 10:27:55 -070019from OltConfig import OltConfig
A.R Karthickbe7768c2017-03-17 11:39:41 -070020from CordTestUtils import get_mac, get_controller
A R Karthick2b93d6a2016-09-06 15:19:09 -070021
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080022class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070023
24 auth = ('karaf', 'karaf')
A R Karthick2b93d6a2016-09-06 15:19:09 -070025 controller = get_controller()
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070026 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A.R Karthick95d044e2016-06-10 18:44:36 -070027 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070028 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
A R Karthick36ae0252017-04-03 17:15:24 -070029 host_cfg_url = 'http://%s:8181/onos/v1/network/configuration/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070030
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080031 def __init__(self, app, controller = None):
32 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070033 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080034 self.controller = controller
35 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
36 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
37 self.auth = ('karaf', 'karaf')
38
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070039 @classmethod
A R Karthickbd82f362016-11-10 15:08:52 -080040 def config(cls, config, controller=None):
A R Karthick4e0c0912016-08-17 16:57:42 -070041 if config is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080042 json_data = json.dumps(config)
ChetanGaonker689b3862016-10-17 16:25:01 -070043 if controller is None:
ChetanGaonker689b3862016-10-17 16:25:01 -070044 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
45 else:
46 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
ChetanGaonker689b3862016-10-17 16:25:01 -070047 resp = requests.post(cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080048 return resp.ok, resp.status_code
49 return False, 400
50
A R Karthickbd82f362016-11-10 15:08:52 -080051 @classmethod
A.R Karthickb17e2022017-01-27 11:29:26 -080052 def get_config(cls, controller=None):
53 if controller is None:
54 controller = cls.controller
55 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
56 resp = requests.get(cfg_url, auth = cls.auth)
57 if resp.ok:
58 return resp.json()
59 return None
60
61 @classmethod
A R Karthickbd82f362016-11-10 15:08:52 -080062 def delete(cls, config, controller=None):
63 if config:
64 json_data = json.dumps(config)
65 if controller is None:
66 print('default Onos config url is %s'%cls.cfg_url)
67 resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data)
68 else:
69 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A R Karthickbd82f362016-11-10 15:08:52 -080070 resp = requests.delete(cfg_url, auth = cls.auth, data = json_data)
71 return resp.ok, resp.status_code
72 return False, 400
73
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080074 def activate(self):
75 resp = requests.post(self.app_url + '/active', auth = self.auth)
76 return resp.ok, resp.status_code
77
78 def deactivate(self):
79 resp = requests.delete(self.app_url + '/active', auth = self.auth)
80 return resp.ok, resp.status_code
81
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070082 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -080083 def get_devices(cls, controller = None):
84 if controller is None:
85 controller = cls.controller
86 url = 'http://%s:8181/onos/v1/devices' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070087 result = requests.get(url, auth = cls.auth)
88 if result.ok:
89 devices = result.json()['devices']
90 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080091
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070092 return None
93
94 @classmethod
A.R Karthickd4eed642017-03-09 14:40:52 -080095 def get_links(cls, controller = None):
96 if controller is None:
97 controller = cls.controller
98 url = 'http://%s:8181/onos/v1/links' %(controller)
99 result = requests.get(url, auth = cls.auth)
100 if result.ok:
101 links = result.json()['links']
102 return links
103 return None
104
105 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800106 def get_device_id(cls, controller = None):
A R Karthickb03cecd2016-07-27 10:27:55 -0700107 '''If running under olt, we get the first switch connected to onos'''
108 olt = OltConfig()
A R Karthick078e63a2016-07-28 13:59:31 -0700109 did = 'of:' + get_mac()
A R Karthickb03cecd2016-07-27 10:27:55 -0700110 if olt.on_olt():
A R Karthick946141b2017-01-24 16:37:47 -0800111 devices = cls.get_devices(controller = controller)
A R Karthickb03cecd2016-07-27 10:27:55 -0700112 if devices:
113 dids = map(lambda d: d['id'], devices)
114 if len(dids) == 1:
115 did = dids[0]
116 else:
117 ###If we have more than 1, then check for env before using first one
118 did = os.getenv('OLT_DEVICE_ID', dids[0])
A R Karthickb03cecd2016-07-27 10:27:55 -0700119
120 return did
121
122 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800123 def get_device_ids(cls, controller = None):
A R Karthick0f6b6842016-12-06 17:17:44 -0800124 '''If running under olt, we get the first switch connected to onos'''
125 olt = OltConfig()
126 did = 'of:' + get_mac()
127 device_ids = []
128 if olt.on_olt():
A R Karthick946141b2017-01-24 16:37:47 -0800129 devices = cls.get_devices(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800130 if devices:
131 device_ids = map(lambda d: d['id'], devices)
132 else:
133 device_ids.append(did)
134
135 return device_ids
136
137 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700138 def get_flows(cls, device_id,controller=None):
139 if controller is None:
A R Karthick946141b2017-01-24 16:37:47 -0800140 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
ChetanGaonker689b3862016-10-17 16:25:01 -0700141 else:
A R Karthick946141b2017-01-24 16:37:47 -0800142 url = 'http://%s:8181/onos/v1/flows/' %(controller) + device_id
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700143 result = requests.get(url, auth = cls.auth)
144 if result.ok:
145 return result.json()['flows']
146 return None
147
148 @classmethod
A R Karthick0f6b6842016-12-06 17:17:44 -0800149 def get_ports_device(cls, device_id, controller = None):
150 if controller is None:
151 url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(cls.controller, device_id)
152 else:
153 url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(controller, device_id)
154
155 result = requests.get(url, auth = cls.auth)
156 if result.ok:
157 return result.json()['ports']
158 return None
159
160 @classmethod
161 def cord_olt_device_map(cls, olt_config, controller = None):
162 olt_device_list = []
163 olt_port_map, _ = olt_config.olt_port_map()
164 switches = olt_port_map['switches']
165 if len(switches) > 1:
A R Karthick946141b2017-01-24 16:37:47 -0800166 device_ids = cls.get_device_ids(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800167 else:
A R Karthick946141b2017-01-24 16:37:47 -0800168 did = cls.get_device_id(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800169 if did is None:
170 return olt_device_list
171 uplink_dict = {}
172 uplink_dict['did'] = did
173 uplink_dict['switch'] = switches[0]
174 uplink_dict['uplink'] = str(olt_config.olt_conf['uplink'])
175 uplink_dict['vlan'] = str(olt_config.olt_conf['vlan'])
176 olt_device_list.append(uplink_dict)
177 return olt_device_list
178
179 for did in device_ids:
180 ports = cls.get_ports_device(did, controller = controller)
181 if ports:
182 matched = False
183 for port in ports:
184 for switch in switches:
185 if port['annotations']['portName'] == switch:
186 uplink_dict = {}
187 uplink = olt_port_map[switch]['uplink']
188 uplink_dict['did'] = did
189 uplink_dict['switch'] = switch
190 uplink_dict['uplink'] = str(uplink)
191 uplink_dict['vlan'] = str(olt_config.olt_conf['vlan'])
192 olt_device_list.append(uplink_dict)
193 matched = True
194 break
195 if matched == True:
196 break
197
198 return olt_device_list
199
200 @classmethod
201 def cord_olt_config(cls, olt_config, controller=None):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700202 '''Configures OLT data for existing devices/switches'''
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700203 did_dict = {}
204 config = { 'devices' : did_dict }
A R Karthick0f6b6842016-12-06 17:17:44 -0800205 olt_device_list = cls.cord_olt_device_map(olt_config, controller = controller)
206 if not olt_device_list:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700207 return
A R Karthick0f6b6842016-12-06 17:17:44 -0800208 for olt_map in olt_device_list:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700209 access_device_dict = {}
A R Karthick0f6b6842016-12-06 17:17:44 -0800210 device_data = {'uplink': olt_map['uplink'], 'vlan': olt_map['vlan']}
211 access_device_dict['accessDevice'] = device_data
212 did_dict[olt_map['did']] = access_device_dict
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700213
214 ##configure the device list with access information
A R Karthick0f6b6842016-12-06 17:17:44 -0800215 return cls.config(config, controller=controller)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700216
217 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700218 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700219 params = {'activate':'true'}
220 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700221 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 -0700222 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700223 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700224 params = params, headers = headers,
225 data = payload)
ChetanGaonker689b3862016-10-17 16:25:01 -0700226 print('result.ok, result.status_code are %s and %s'%(result.ok, result.status_code))
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700227 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700228
229 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700230 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
231 params = {'activate':'true'}
232 headers = {'content-type':'application/json'}
233 if app_url is None:
234 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
235 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
236 params['url'] = app_url
237 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
238 result = requests.post(url, auth = cls.auth,
239 json = params, headers = headers)
240 return result.ok, result.status_code
241
242 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700243 def uninstall_app(cls, app_name, onos_ip = None):
244 params = {'activate':'true'}
245 headers = {'content-type':'application/octet-stream'}
246 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
247 app_url = '{}/{}'.format(url, app_name)
248 resp = requests.delete(app_url, auth = cls.auth)
249 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700250
251 @classmethod
A R Karthick36ae0252017-04-03 17:15:24 -0700252 def host_config(cls, config, onos_ip=None):
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700253 if config:
254 json_data = json.dumps(config)
A R Karthick36ae0252017-04-03 17:15:24 -0700255 url = cls.host_cfg_url if onos_ip is None else 'http://{}:8181/onos/v1/network/configuration/hosts/'.format(onos_ip)
256 resp = requests.post(url, auth = cls.auth, data = json_data)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700257 return resp.ok, resp.status_code
258 return False, 400