blob: bbe303e785ab8865bca1f613fbb69ce062711a33 [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 Karthick35495c32017-05-11 14:58:32 -070017import requests
18import json
19import time
A.R Karthick57fa9372017-05-24 12:47:03 -070020import os
21import signal
A.R Karthick4c4d0492017-05-26 19:23:05 -070022from CordTestUtils import log_test as log, getstatusoutput, get_controller
A R Karthickbf1e4b02017-07-11 20:17:14 -070023from CordContainer import Container, Onos
A.R Karthick4c4d0492017-05-26 19:23:05 -070024from OnosCtrl import OnosCtrl
A.R Karthick4f583842017-06-09 17:15:47 -070025from OltConfig import OltConfig
A.R Karthick57fa9372017-05-24 12:47:03 -070026
27class VolthaService(object):
28 services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd')
29 compose_file = 'docker-compose-system-test.yml'
30 service_map = {}
31
A.R Karthick4f583842017-06-09 17:15:47 -070032 def __init__(self, voltha_loc, controller, interface = 'eth0', olt_config = None):
A.R Karthick57fa9372017-05-24 12:47:03 -070033 if not os.access(voltha_loc, os.F_OK):
34 raise Exception('Voltha location %s not found' %voltha_loc)
35 compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file)
36 if not os.access(compose_file_loc, os.F_OK):
37 raise Exception('Voltha compose file %s not found' %compose_file_loc)
38 self.voltha_loc = voltha_loc
39 self.controller = controller
40 self.interface = interface
41 self.compose_file_loc = compose_file_loc
A.R Karthick4f583842017-06-09 17:15:47 -070042 num_onus = 1
43 if olt_config is not None:
44 port_map, _ = OltConfig(olt_config).olt_port_map()
45 if port_map['ponsim'] is True:
46 num_onus = max(1, len(port_map['ports']))
47 self.num_onus = num_onus
A.R Karthick57fa9372017-05-24 12:47:03 -070048
49 def start(self):
50 start_cmd = 'docker-compose -f {} up -d {} {} {} {} {}'.format(self.compose_file_loc,
51 *self.services)
52 ret = os.system(start_cmd)
53 if ret != 0:
54 raise Exception('Failed to start voltha services. Failed with code %d' %ret)
55
56 for service in self.services:
57 name = 'compose_{}_1'.format(service)
58 network = 'compose_default'
59 cnt = Container(name, name)
60 ip = cnt.ip(network = network)
61 if not ip:
62 raise Exception('IP not found for container %s' %name)
63 print('IP %s for service %s' %(ip, service))
64 self.service_map[service] = dict(name = name, network = network, ip = ip)
65
66 #first start chameleon
67 chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \
68 nohup python chameleon/main.py -v --consul=localhost:8500 \
69 --fluentd={}:24224 --grpc-endpoint=localhost:50555 \
70 >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc,
71 self.service_map['fluentd']['ip'])
A.R Karthickc2697a12017-05-24 14:01:15 -070072 if not self.service_running('python chameleon/main.py'):
73 ret = os.system(chameleon_start_cmd)
74 if ret != 0:
75 raise Exception('VOLTHA chameleon service not started. Failed with return code %d' %ret)
A R Karthickd69d5702017-07-25 11:31:47 -070076 time.sleep(10)
A.R Karthickc2697a12017-05-24 14:01:15 -070077 else:
78 print('Chameleon voltha sevice is already running. Skipped start')
A.R Karthick57fa9372017-05-24 12:47:03 -070079
80 #now start voltha and ofagent
A R Karthickd52ca8a2017-07-24 17:38:55 -070081 voltha_setup_cmd = "cd {} && sh -c '. ./env.sh && make rebuild-venv && make protos'".format(self.voltha_loc)
A.R Karthick57fa9372017-05-24 12:47:03 -070082 voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \
83 nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \
84 --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \
85 >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc,
86 self.service_map['kafka']['ip'],
87 self.interface,
88 self.service_map['fluentd']['ip'])
A R Karthickd52ca8a2017-07-24 17:38:55 -070089 pki_dir = '{}/pki'.format(self.voltha_loc)
A.R Karthickc2697a12017-05-24 14:01:15 -070090 if not self.service_running('python voltha/main.py'):
A R Karthickd52ca8a2017-07-24 17:38:55 -070091 voltha_pki_dir = '/voltha'
92 if os.access(pki_dir, os.F_OK):
93 pki_xfer_cmd = 'mkdir -p {} && cp -rv {}/pki {}'.format(voltha_pki_dir,
94 self.voltha_loc,
95 voltha_pki_dir)
96 os.system(pki_xfer_cmd)
97 #os.system(voltha_setup_cmd)
A.R Karthickc2697a12017-05-24 14:01:15 -070098 ret = os.system(voltha_start_cmd)
99 if ret != 0:
100 raise Exception('Failed to start VOLTHA. Return code %d' %ret)
A R Karthickd69d5702017-07-25 11:31:47 -0700101 time.sleep(10)
A.R Karthickc2697a12017-05-24 14:01:15 -0700102 else:
103 print('VOLTHA core is already running. Skipped start')
A.R Karthick57fa9372017-05-24 12:47:03 -0700104
105 ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \
106 nohup python ofagent/main.py -v --consul=localhost:8500 \
107 --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \
108 >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc,
109 self.service_map['fluentd']['ip'],
110 self.controller)
A.R Karthickc2697a12017-05-24 14:01:15 -0700111 if not self.service_running('python ofagent/main.py'):
A R Karthickd52ca8a2017-07-24 17:38:55 -0700112 ofagent_pki_dir = '/ofagent'
113 if os.access(pki_dir, os.F_OK):
114 pki_xfer_cmd = 'mkdir -p {} && cp -rv {}/pki {}'.format(ofagent_pki_dir,
115 self.voltha_loc,
116 ofagent_pki_dir)
117 os.system(pki_xfer_cmd)
A.R Karthickc2697a12017-05-24 14:01:15 -0700118 ret = os.system(ofagent_start_cmd)
119 if ret != 0:
120 raise Exception('VOLTHA ofagent not started. Failed with return code %d' %ret)
A R Karthickd69d5702017-07-25 11:31:47 -0700121 time.sleep(10)
A.R Karthickc2697a12017-05-24 14:01:15 -0700122 else:
123 print('VOLTHA ofagent is already running. Skipped start')
124
A.R Karthick12e08c32017-05-30 17:09:26 -0700125 ponsim_start_cmd = "cd {} && sh -c '. ./env.sh && \
A.R Karthick4f583842017-06-09 17:15:47 -0700126 nohup python ponsim/main.py -o {} -v >/tmp/ponsim.log 2>&1 &'".format(self.voltha_loc, self.num_onus)
A.R Karthick12e08c32017-05-30 17:09:26 -0700127 if not self.service_running('python ponsim/main.py'):
128 ret = os.system(ponsim_start_cmd)
129 if ret != 0:
130 raise Exception('PONSIM not started. Failed with return code %d' %ret)
131 time.sleep(3)
132 else:
133 print('PONSIM already running. Skipped start')
134
A.R Karthickc2697a12017-05-24 14:01:15 -0700135 def service_running(self, pattern):
136 st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern))
137 return True if st == 0 else False
A.R Karthick57fa9372017-05-24 12:47:03 -0700138
139 def kill_service(self, pattern):
140 st, output = getstatusoutput('pgrep -f "{}"'.format(pattern))
141 if st == 0 and output:
142 pids = output.strip().splitlines()
143 for pid in pids:
144 try:
145 os.kill(int(pid), signal.SIGKILL)
146 except:
147 pass
148
149 def stop(self):
150 self.kill_service('python voltha/main.py')
151 self.kill_service('python ofagent/main.py')
152 self.kill_service('python chameleon/main.py')
A.R Karthick12e08c32017-05-30 17:09:26 -0700153 self.kill_service('python ponsim/main.py')
A.R Karthick57fa9372017-05-24 12:47:03 -0700154 service_stop_cmd = 'docker-compose -f {} down'.format(self.compose_file_loc)
155 os.system(service_stop_cmd)
A R Karthick35495c32017-05-11 14:58:32 -0700156
157class VolthaCtrl(object):
A R Karthick53442712017-07-27 12:23:30 -0700158 UPLINK_VLAN_START = 333
A.R Karthick4c4d0492017-05-26 19:23:05 -0700159 UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' }
A R Karthickbf1e4b02017-07-11 20:17:14 -0700160 REST_PORT = 8881
A R Karthick53442712017-07-27 12:23:30 -0700161 HOST = '172.17.0.1'
A R Karthick6e70e142017-07-28 15:25:38 -0700162 ONOS_APPS = ('org.onosproject.dhcp', 'org.onosproject.dhcp-relay', 'org.ciena.cordigmp')
A.R Karthick4c4d0492017-05-26 19:23:05 -0700163
A R Karthick53442712017-07-27 12:23:30 -0700164 def __init__(self, host = HOST, rest_port = REST_PORT, uplink_vlan_map = UPLINK_VLAN_MAP, uplink_vlan_start = UPLINK_VLAN_START):
A R Karthick35495c32017-05-11 14:58:32 -0700165 self.host = host
166 self.rest_port = rest_port
167 self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port)
A.R Karthick4c4d0492017-05-26 19:23:05 -0700168 self.uplink_vlan_map = uplink_vlan_map
A R Karthick53442712017-07-27 12:23:30 -0700169 VolthaCtrl.UPLINK_VLAN_START = uplink_vlan_start
A.R Karthick4c4d0492017-05-26 19:23:05 -0700170 self.switches = []
171 self.switch_map = {}
172
173 def config(self, fake = False):
174 devices = OnosCtrl.get_devices()
175 if not devices:
176 return self.switch_map
177 voltha_devices = filter(lambda d: not d['mfr'].startswith('Nicira'), devices)
178 self.switches = voltha_devices
179 device_config = { 'devices' : { } }
180 device_id = None
181 for device in voltha_devices:
182 device_id = device['id']
183 ports = OnosCtrl.get_ports_device(device_id)
184 nni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('nni'), ports)
185 uni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('uni'), ports)
186 if device_id not in self.uplink_vlan_map:
A R Karthick53442712017-07-27 12:23:30 -0700187 uplink_vlan = VolthaCtrl.UPLINK_VLAN_START
188 VolthaCtrl.UPLINK_VLAN_START += 1
189 log.info('Voltha device %s not in map. Using uplink vlan %d' %(device_id, uplink_vlan))
190 else:
191 uplink_vlan = self.uplink_vlan_map[device_id]
A.R Karthick4c4d0492017-05-26 19:23:05 -0700192 if not nni_ports:
193 log.info('Voltha device %s has no NNI ports' %device_id)
194 if fake is True:
195 log.info('Faking NNI port 0')
196 nni_ports = [ {'port': '0'} ]
197 else:
198 log.info('Skip configuring device %s' %device_id)
199 continue
200 if not uni_ports:
201 log.info('Voltha device %s has no UNI ports' %device_id)
202 if fake is True:
203 log.info('Faking UNI port 252')
204 uni_ports = [ {'port': '252'} ]
205 else:
206 log.info('Skip configuring device %s' %device_id)
207 continue
A.R Karthick4c4d0492017-05-26 19:23:05 -0700208 onu_ports = map(lambda uni: uni['port'], uni_ports)
209 self.switch_map[device_id] = dict(uplink_vlan = uplink_vlan, ports = onu_ports)
210 device_config['devices'][device_id] = {}
211 device_config['devices'][device_id]['basic'] = dict(driver='pmc-olt')
212 device_config['devices'][device_id]['accessDevice'] = dict(uplink=nni_ports[0]['port'],
213 vlan = uplink_vlan,
214 defaultVlan='0'
215 )
216 if device_id:
217 #toggle drivers/openflow base before reconfiguring the driver and olt config data
218 OnosCtrl('org.onosproject.drivers').deactivate()
219 OnosCtrl('org.onosproject.openflow-base').deactivate()
220 OnosCtrl.config(device_config)
A R Karthick6e70e142017-07-28 15:25:38 -0700221 time.sleep(10)
A.R Karthick4c4d0492017-05-26 19:23:05 -0700222 OnosCtrl('org.onosproject.drivers').activate()
223 OnosCtrl('org.onosproject.openflow-base').activate()
224 time.sleep(5)
A R Karthick6e70e142017-07-28 15:25:38 -0700225 log.info('Reactivating CORD and ONOS apps')
226 Onos.activate_cord_apps(deactivate = True)
227 Onos.activate_apps(self.ONOS_APPS, deactivate = True)
A.R Karthick4c4d0492017-05-26 19:23:05 -0700228
229 return self.switch_map
A R Karthick35495c32017-05-11 14:58:32 -0700230
231 def get_devices(self):
232 url = '{}/local/devices'.format(self.rest_url)
233 resp = requests.get(url)
234 if resp.ok is not True or resp.status_code != 200:
235 return None
236 return resp.json()
237
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700238 def enable_device(self, olt_type, olt_mac = None, address = None):
A R Karthick35495c32017-05-11 14:58:32 -0700239 url = '{}/local/devices'.format(self.rest_url)
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700240 if olt_mac is None and address is None:
241 log.error('Either olt mac or address needs to be specified')
A.R Karthick8a507cf2017-06-02 18:44:49 -0700242 return None, False
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700243 if olt_mac is not None:
244 device_config = { 'type' : olt_type, 'mac_address' : olt_mac }
245 else:
246 device_config = { 'type' : olt_type, 'host_and_port' : address }
A R Karthick35495c32017-05-11 14:58:32 -0700247 #pre-provision
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700248 if olt_mac is not None:
249 log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac))
250 else:
251 log.info('Pre-provisioning %s with address %s' %(olt_type, address))
A R Karthick35495c32017-05-11 14:58:32 -0700252 resp = requests.post(url, data = json.dumps(device_config))
253 if resp.ok is not True or resp.status_code != 200:
A.R Karthick8a507cf2017-06-02 18:44:49 -0700254 return None, False
A R Karthick35495c32017-05-11 14:58:32 -0700255 device_id = resp.json()['id']
256 log.info('Enabling device %s' %(device_id))
257 enable_url = '{}/{}/enable'.format(url, device_id)
258 resp = requests.post(enable_url)
259 if resp.ok is not True or resp.status_code != 200:
A.R Karthick8a507cf2017-06-02 18:44:49 -0700260 return None, False
A R Karthick35495c32017-05-11 14:58:32 -0700261 #get operational status
A R Karthick090631b2017-07-25 16:05:05 -0700262 time.sleep(10)
A R Karthick35495c32017-05-11 14:58:32 -0700263 log.info('Checking operational status for device %s' %(device_id))
264 resp = requests.get('{}/{}'.format(url, device_id))
265 if resp.ok is not True or resp.status_code != 200:
A.R Karthick8a507cf2017-06-02 18:44:49 -0700266 return device_id, False
A R Karthick35495c32017-05-11 14:58:32 -0700267 device_info = resp.json()
268 if device_info['oper_status'] != 'ACTIVE' or \
269 device_info['admin_state'] != 'ENABLED' or \
270 device_info['connect_status'] != 'REACHABLE':
A.R Karthick8a507cf2017-06-02 18:44:49 -0700271 return device_id, False
A R Karthick35495c32017-05-11 14:58:32 -0700272
A.R Karthick8a507cf2017-06-02 18:44:49 -0700273 return device_id, True
274
275 def disable_device(self, device_id, delete = True):
276 log.info('Disabling device %s' %(device_id))
277 disable_url = '{}/local/devices/{}/disable'.format(self.rest_url, device_id)
278 resp = requests.post(disable_url)
279 if resp.ok is not True or resp.status_code != 200:
280 return False
281 if delete is True:
A R Karthickd69d5702017-07-25 11:31:47 -0700282 #rest for disable completion
283 time.sleep(10)
A.R Karthick8a507cf2017-06-02 18:44:49 -0700284 log.info('Deleting device %s' %(device_id))
285 delete_url = '{}/local/devices/{}/delete'.format(self.rest_url, device_id)
286 resp = requests.delete(delete_url)
287 if resp.status_code not in [204, 202, 200]:
288 return False
A R Karthick35495c32017-05-11 14:58:32 -0700289 return True
Chetan Gaonker3620a112017-05-23 06:10:15 +0000290
Thangavelu K Se6c77c72017-06-23 21:28:48 +0000291 def restart_device(self, device_id):
292 log.info('Restarting olt or onu device %s' %(device_id))
293 disable_url = '{}/local/devices/{}/restart'.format(self.rest_url, device_id)
294 resp = requests.post(disable_url)
295 if resp.ok is not True or resp.status_code != 200:
296 return False
297 return True
298
299 def pause_device(self, device_id):
300 log.info('Restarting olt or onu device %s' %(device_id))
301 disable_url = '{}/local/devices/{}/pause'.format(self.rest_url, device_id)
302 resp = requests.post(disable_url)
303 if resp.ok is not True or resp.status_code != 200:
304 return False
305 return True
306
Chetan Gaonker3620a112017-05-23 06:10:15 +0000307 def get_operational_status(self, device_id):
308 url = '{}/local/devices'.format(self.rest_url)
309 log.info('Checking operational status for device %s' %(device_id))
310 resp = requests.get('{}/{}'.format(url, device_id))
311 if resp.ok is not True or resp.status_code != 200:
312 return False
313 device_info = resp.json()
314 if device_info['oper_status'] != 'ACTIVE' or \
315 device_info['admin_state'] != 'ENABLED' or \
316 device_info['connect_status'] != 'REACHABLE':
317 return False
318 return True
319
320 def check_preprovision_status(self, device_id):
321 url = '{}/local/devices'.format(self.rest_url)
322 log.info('Check if device %s is in Preprovisioning state'%(device_id))
323 resp = requests.get('{}/{}'.format(url, device_id))
324 if resp.ok is not True or resp.status_code != 200:
325 return False
326 device_info = resp.json()
327 if device_info['admin_status'] == 'PREPROVISIONED':
328 return True
329 return False
A R Karthickbf1e4b02017-07-11 20:17:14 -0700330
331def get_olt_app():
332 our_path = os.path.dirname(os.path.realpath(__file__))
333 version = Onos.getVersion()
334 major = int(version.split('.')[0])
335 minor = int(version.split('.')[1])
336 olt_app_version = '1.2-SNAPSHOT'
337 if major > 1:
338 olt_app_version = '2.0-SNAPSHOT'
339 elif major == 1:
340 if minor > 10:
341 olt_app_version = '2.0-SNAPSHOT'
342 elif minor <= 8:
343 olt_app_version = '1.1-SNAPSHOT'
344 olt_app_file = os.path.join(our_path, '..', 'apps/olt-app-{}.oar'.format(olt_app_version))
345 return olt_app_file
346
347def voltha_setup(host = '172.17.0.1', rest_port = VolthaCtrl.REST_PORT,
A R Karthick9dc6e922017-07-12 14:40:16 -0700348 olt_type = 'ponsim_olt', olt_mac = '00:0c:e2:31:12:00',
A R Karthickbf1e4b02017-07-11 20:17:14 -0700349 uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP,
A R Karthick53442712017-07-27 12:23:30 -0700350 uplink_vlan_start = VolthaCtrl.UPLINK_VLAN_START,
A R Karthickbf1e4b02017-07-11 20:17:14 -0700351 config_fake = False, olt_app = None):
352
A R Karthick53442712017-07-27 12:23:30 -0700353 voltha = VolthaCtrl(host, rest_port = rest_port,
354 uplink_vlan_map = uplink_vlan_map,
355 uplink_vlan_start = uplink_vlan_start)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700356 if olt_type.startswith('ponsim'):
357 ponsim_address = '{}:50060'.format(host)
358 log.info('Enabling ponsim olt')
359 device_id, status = voltha.enable_device(olt_type, address = ponsim_address)
360 else:
361 log.info('Enabling OLT instance for %s with mac %s' %(olt_type, olt_mac))
362 device_id, status = voltha.enable_device(olt_type, olt_mac)
363
364 if device_id is None or status is False:
A R Karthick9dc6e922017-07-12 14:40:16 -0700365 if device_id:
366 voltha.disable_device(device_id)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700367 return None
368
369 switch_map = None
370 olt_installed = False
371 if olt_app is None:
372 olt_app = get_olt_app()
373 try:
A R Karthick090631b2017-07-25 16:05:05 -0700374 time.sleep(5)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700375 switch_map = voltha.config(fake = config_fake)
376 if switch_map is None:
377 voltha.disable_device(device_id)
378 return None
379 log.info('Installing OLT app %s' %olt_app)
380 OnosCtrl.install_app(olt_app)
381 olt_installed = True
382 time.sleep(5)
383 return voltha, device_id, switch_map
384 except:
385 voltha.disable_device(device_id)
386 time.sleep(10)
387 if olt_installed is True:
388 log.info('Uninstalling OLT app %s' %olt_app)
389 OnosCtrl.uninstall_app(olt_app)
390
391 return None
392
393def voltha_teardown(voltha_ctrl, device_id, switch_map, olt_app = None):
394 voltha_ctrl.disable_device(device_id)
395 time.sleep(10)
396 if olt_app is None:
397 olt_app = get_olt_app()
A R Karthickdd064632017-07-12 13:02:17 -0700398 log.info('Uninstalling OLT app %s' %olt_app)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700399 OnosCtrl.uninstall_app(olt_app)