blob: bd931a7bf2f5877563f7035af19cc847bac20116 [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
20import fcntl, socket, struct
21
A R Karthick078e63a2016-07-28 13:59:31 -070022def get_mac(iface = None, pad = 4):
23 if iface is None:
24 iface = os.getenv('TEST_SWITCH', 'ovsbr0')
A R Karthickb03cecd2016-07-27 10:27:55 -070025 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
26 try:
27 info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15])))
28 except:
29 info = ['0'] * 24
30 s.close()
31 sep = ''
32 if pad == 0:
33 sep = ':'
34 return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]])
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080035
A R Karthick2b93d6a2016-09-06 15:19:09 -070036def get_controller():
37 controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
38 controller = controller.split(',')[0]
39 return controller
40
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080041class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070042
43 auth = ('karaf', 'karaf')
A R Karthick2b93d6a2016-09-06 15:19:09 -070044 controller = get_controller()
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070045 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A.R Karthick95d044e2016-06-10 18:44:36 -070046 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070047 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -070048 host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070049
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080050 def __init__(self, app, controller = None):
51 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070052 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080053 self.controller = controller
54 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
55 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
56 self.auth = ('karaf', 'karaf')
57
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070058 @classmethod
A R Karthickbd82f362016-11-10 15:08:52 -080059 def config(cls, config, controller=None):
A R Karthick4e0c0912016-08-17 16:57:42 -070060 if config is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080061 json_data = json.dumps(config)
ChetanGaonker689b3862016-10-17 16:25:01 -070062 if controller is None:
ChetanGaonker689b3862016-10-17 16:25:01 -070063 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
64 else:
65 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
ChetanGaonker689b3862016-10-17 16:25:01 -070066 resp = requests.post(cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080067 return resp.ok, resp.status_code
68 return False, 400
69
A R Karthickbd82f362016-11-10 15:08:52 -080070 @classmethod
A.R Karthickb17e2022017-01-27 11:29:26 -080071 def get_config(cls, controller=None):
72 if controller is None:
73 controller = cls.controller
74 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
75 resp = requests.get(cfg_url, auth = cls.auth)
76 if resp.ok:
77 return resp.json()
78 return None
79
80 @classmethod
A R Karthickbd82f362016-11-10 15:08:52 -080081 def delete(cls, config, controller=None):
82 if config:
83 json_data = json.dumps(config)
84 if controller is None:
85 print('default Onos config url is %s'%cls.cfg_url)
86 resp = requests.delete(cls.cfg_url, auth = cls.auth, data = json_data)
87 else:
88 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A R Karthickbd82f362016-11-10 15:08:52 -080089 resp = requests.delete(cfg_url, auth = cls.auth, data = json_data)
90 return resp.ok, resp.status_code
91 return False, 400
92
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080093 def activate(self):
94 resp = requests.post(self.app_url + '/active', auth = self.auth)
95 return resp.ok, resp.status_code
96
97 def deactivate(self):
98 resp = requests.delete(self.app_url + '/active', auth = self.auth)
99 return resp.ok, resp.status_code
100
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700101 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800102 def get_devices(cls, controller = None):
103 if controller is None:
104 controller = cls.controller
105 url = 'http://%s:8181/onos/v1/devices' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700106 result = requests.get(url, auth = cls.auth)
107 if result.ok:
108 devices = result.json()['devices']
109 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800110
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700111 return None
112
113 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800114 def get_device_id(cls, controller = None):
A R Karthickb03cecd2016-07-27 10:27:55 -0700115 '''If running under olt, we get the first switch connected to onos'''
116 olt = OltConfig()
A R Karthick078e63a2016-07-28 13:59:31 -0700117 did = 'of:' + get_mac()
A R Karthickb03cecd2016-07-27 10:27:55 -0700118 if olt.on_olt():
A R Karthick946141b2017-01-24 16:37:47 -0800119 devices = cls.get_devices(controller = controller)
A R Karthickb03cecd2016-07-27 10:27:55 -0700120 if devices:
121 dids = map(lambda d: d['id'], devices)
122 if len(dids) == 1:
123 did = dids[0]
124 else:
125 ###If we have more than 1, then check for env before using first one
126 did = os.getenv('OLT_DEVICE_ID', dids[0])
A R Karthickb03cecd2016-07-27 10:27:55 -0700127
128 return did
129
130 @classmethod
A R Karthick946141b2017-01-24 16:37:47 -0800131 def get_device_ids(cls, controller = None):
A R Karthick0f6b6842016-12-06 17:17:44 -0800132 '''If running under olt, we get the first switch connected to onos'''
133 olt = OltConfig()
134 did = 'of:' + get_mac()
135 device_ids = []
136 if olt.on_olt():
A R Karthick946141b2017-01-24 16:37:47 -0800137 devices = cls.get_devices(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800138 if devices:
139 device_ids = map(lambda d: d['id'], devices)
140 else:
141 device_ids.append(did)
142
143 return device_ids
144
145 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700146 def get_flows(cls, device_id,controller=None):
147 if controller is None:
A R Karthick946141b2017-01-24 16:37:47 -0800148 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
ChetanGaonker689b3862016-10-17 16:25:01 -0700149 else:
A R Karthick946141b2017-01-24 16:37:47 -0800150 url = 'http://%s:8181/onos/v1/flows/' %(controller) + device_id
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700151 result = requests.get(url, auth = cls.auth)
152 if result.ok:
153 return result.json()['flows']
154 return None
155
156 @classmethod
A R Karthick0f6b6842016-12-06 17:17:44 -0800157 def get_ports_device(cls, device_id, controller = None):
158 if controller is None:
159 url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(cls.controller, device_id)
160 else:
161 url = 'http://{}:8181/onos/v1/devices/{}/ports'.format(controller, device_id)
162
163 result = requests.get(url, auth = cls.auth)
164 if result.ok:
165 return result.json()['ports']
166 return None
167
168 @classmethod
169 def cord_olt_device_map(cls, olt_config, controller = None):
170 olt_device_list = []
171 olt_port_map, _ = olt_config.olt_port_map()
172 switches = olt_port_map['switches']
173 if len(switches) > 1:
A R Karthick946141b2017-01-24 16:37:47 -0800174 device_ids = cls.get_device_ids(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800175 else:
A R Karthick946141b2017-01-24 16:37:47 -0800176 did = cls.get_device_id(controller = controller)
A R Karthick0f6b6842016-12-06 17:17:44 -0800177 if did is None:
178 return olt_device_list
179 uplink_dict = {}
180 uplink_dict['did'] = did
181 uplink_dict['switch'] = switches[0]
182 uplink_dict['uplink'] = str(olt_config.olt_conf['uplink'])
183 uplink_dict['vlan'] = str(olt_config.olt_conf['vlan'])
184 olt_device_list.append(uplink_dict)
185 return olt_device_list
186
187 for did in device_ids:
188 ports = cls.get_ports_device(did, controller = controller)
189 if ports:
190 matched = False
191 for port in ports:
192 for switch in switches:
193 if port['annotations']['portName'] == switch:
194 uplink_dict = {}
195 uplink = olt_port_map[switch]['uplink']
196 uplink_dict['did'] = did
197 uplink_dict['switch'] = switch
198 uplink_dict['uplink'] = str(uplink)
199 uplink_dict['vlan'] = str(olt_config.olt_conf['vlan'])
200 olt_device_list.append(uplink_dict)
201 matched = True
202 break
203 if matched == True:
204 break
205
206 return olt_device_list
207
208 @classmethod
209 def cord_olt_config(cls, olt_config, controller=None):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700210 '''Configures OLT data for existing devices/switches'''
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700211 did_dict = {}
212 config = { 'devices' : did_dict }
A R Karthick0f6b6842016-12-06 17:17:44 -0800213 olt_device_list = cls.cord_olt_device_map(olt_config, controller = controller)
214 if not olt_device_list:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700215 return
A R Karthick0f6b6842016-12-06 17:17:44 -0800216 for olt_map in olt_device_list:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700217 access_device_dict = {}
A R Karthick0f6b6842016-12-06 17:17:44 -0800218 device_data = {'uplink': olt_map['uplink'], 'vlan': olt_map['vlan']}
219 access_device_dict['accessDevice'] = device_data
220 did_dict[olt_map['did']] = access_device_dict
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700221
222 ##configure the device list with access information
A R Karthick0f6b6842016-12-06 17:17:44 -0800223 return cls.config(config, controller=controller)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700224
225 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700226 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700227 params = {'activate':'true'}
228 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700229 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 -0700230 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700231 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700232 params = params, headers = headers,
233 data = payload)
ChetanGaonker689b3862016-10-17 16:25:01 -0700234 print('result.ok, result.status_code are %s and %s'%(result.ok, result.status_code))
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700235 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700236
237 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700238 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
239 params = {'activate':'true'}
240 headers = {'content-type':'application/json'}
241 if app_url is None:
242 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
243 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
244 params['url'] = app_url
245 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
246 result = requests.post(url, auth = cls.auth,
247 json = params, headers = headers)
248 return result.ok, result.status_code
249
250 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700251 def uninstall_app(cls, app_name, onos_ip = None):
252 params = {'activate':'true'}
253 headers = {'content-type':'application/octet-stream'}
254 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
255 app_url = '{}/{}'.format(url, app_name)
256 resp = requests.delete(app_url, auth = cls.auth)
257 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700258
259 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700260 def host_config(cls, config,onos_ip=None):
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700261 if config:
262 json_data = json.dumps(config)
ChetanGaonker689b3862016-10-17 16:25:01 -0700263 url = cls.host_cfg_url if onos_ip is None else 'http://%s:8181/onos/v1/hosts/'.format(onos_ip)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700264 resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data)
265 return resp.ok, resp.status_code
266 return False, 400