blob: 812467becd68c07bb93ea268576fb30d91729199 [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')
A R Karthick32e711a2017-08-22 16:27:22 -070029 standalone_services = ('voltha', 'ofagent', 'vcli',)
A.R Karthick57fa9372017-05-24 12:47:03 -070030 compose_file = 'docker-compose-system-test.yml'
31 service_map = {}
A R Karthick168e2342017-08-15 16:13:10 -070032 PROJECT = 'cordtester'
33 NETWORK = '{}_default'.format(PROJECT)
34 CONTAINER_MODE = False
35 REST_SERVICE = 'chameleon'
36 DOCKER_HOST_IP = '172.17.0.1'
37 PONSIM_HOST = '172.17.0.1'
A.R Karthick57fa9372017-05-24 12:47:03 -070038
A R Karthick168e2342017-08-15 16:13:10 -070039 def __init__(self, voltha_loc, controller, interface = 'eth0', olt_config = None, container_mode = False):
A.R Karthick57fa9372017-05-24 12:47:03 -070040 if not os.access(voltha_loc, os.F_OK):
41 raise Exception('Voltha location %s not found' %voltha_loc)
42 compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file)
43 if not os.access(compose_file_loc, os.F_OK):
44 raise Exception('Voltha compose file %s not found' %compose_file_loc)
45 self.voltha_loc = voltha_loc
46 self.controller = controller
47 self.interface = interface
48 self.compose_file_loc = compose_file_loc
A R Karthick168e2342017-08-15 16:13:10 -070049 VolthaService.CONTAINER_MODE = container_mode
A.R Karthick4f583842017-06-09 17:15:47 -070050 num_onus = 1
51 if olt_config is not None:
52 port_map, _ = OltConfig(olt_config).olt_port_map()
53 if port_map['ponsim'] is True:
54 num_onus = max(1, len(port_map['ports']))
55 self.num_onus = num_onus
A.R Karthick57fa9372017-05-24 12:47:03 -070056
A R Karthick168e2342017-08-15 16:13:10 -070057 def start_services(self, *services):
58 services_fmt = ' {}' * len(services)
59 services_cmd_fmt = 'DOCKER_HOST_IP={} docker-compose -p {} -f {} up -d {}'.format(self.DOCKER_HOST_IP,
60 self.PROJECT,
61 self.compose_file_loc,
62 services_fmt)
63 start_cmd = services_cmd_fmt.format(*services)
A.R Karthick57fa9372017-05-24 12:47:03 -070064 ret = os.system(start_cmd)
65 if ret != 0:
66 raise Exception('Failed to start voltha services. Failed with code %d' %ret)
67
A R Karthick168e2342017-08-15 16:13:10 -070068 for service in services:
69 name = '{}_{}_1'.format(self.PROJECT, service)
A.R Karthick57fa9372017-05-24 12:47:03 -070070 cnt = Container(name, name)
A R Karthick168e2342017-08-15 16:13:10 -070071 ip = cnt.ip(network = self.NETWORK)
A.R Karthick57fa9372017-05-24 12:47:03 -070072 if not ip:
73 raise Exception('IP not found for container %s' %name)
74 print('IP %s for service %s' %(ip, service))
A R Karthick168e2342017-08-15 16:13:10 -070075 self.service_map[service] = dict(name = name, network = self.NETWORK, ip = ip)
A.R Karthick57fa9372017-05-24 12:47:03 -070076
A R Karthick168e2342017-08-15 16:13:10 -070077 def ponmgmt_enable(self):
78 cmds = ('echo 8 | tee /sys/class/net/ponmgmt/bridge/group_fwd_mask',
79 'brctl addif ponmgmt {} >/dev/null 2>&1'.format(self.interface),
80 )
81 for cmd in cmds:
82 try:
83 os.system(cmd)
84 except:
85 pass
86
87 def start(self):
88 self.start_services(*self.services)
89 if self.CONTAINER_MODE is True:
90 self.start_services(*self.standalone_services)
91 #enable multicast mac forwarding:
92 self.ponmgmt_enable()
A R Karthickd69d5702017-07-25 11:31:47 -070093 time.sleep(10)
A R Karthickaa6475d2017-08-15 17:24:36 -070094 chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \
95 nohup python chameleon/main.py -v --consul={}:8500 \
96 --fluentd={}:24224 --grpc-endpoint={}:50555 \
97 >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc,
98 self.get_ip('consul'),
99 self.get_ip('fluentd'),
100 self.get_ip('voltha'))
A.R Karthickc2697a12017-05-24 14:01:15 -0700101 else:
A R Karthickaa6475d2017-08-15 17:24:36 -0700102 #first start chameleon on the host as its only the reliable way for REST
A R Karthick168e2342017-08-15 16:13:10 -0700103 chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \
104 nohup python chameleon/main.py -v --consul=localhost:8500 \
105 --fluentd={}:24224 --grpc-endpoint=localhost:50555 \
106 >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc,
A R Karthickaa6475d2017-08-15 17:24:36 -0700107 self.get_ip('fluentd'))
108 if not self.service_running('python chameleon/main.py'):
109 ret = os.system(chameleon_start_cmd)
110 if ret != 0:
111 raise Exception('VOLTHA chameleon service not started. Failed with return code %d' %ret)
112 time.sleep(10)
113 else:
114 print('Chameleon voltha sevice is already running. Skipped start')
A.R Karthick57fa9372017-05-24 12:47:03 -0700115
A R Karthickaa6475d2017-08-15 17:24:36 -0700116 if self.CONTAINER_MODE is False:
A R Karthick168e2342017-08-15 16:13:10 -0700117 #now start voltha and ofagent
118 voltha_setup_cmd = "cd {} && sh -c '. ./env.sh && make rebuild-venv && make protos'".format(self.voltha_loc)
119 voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \
120 nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \
121 --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \
122 >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc,
123 self.service_map['kafka']['ip'],
124 self.interface,
125 self.service_map['fluentd']['ip'])
126 pki_dir = '{}/pki'.format(self.voltha_loc)
127 if not self.service_running('python voltha/main.py'):
128 voltha_pki_dir = '/voltha'
129 if os.access(pki_dir, os.F_OK):
130 pki_xfer_cmd = 'mkdir -p {} && cp -rv {}/pki {}'.format(voltha_pki_dir,
131 self.voltha_loc,
132 voltha_pki_dir)
133 os.system(pki_xfer_cmd)
134 #os.system(voltha_setup_cmd)
135 ret = os.system(voltha_start_cmd)
136 if ret != 0:
137 raise Exception('Failed to start VOLTHA. Return code %d' %ret)
138 time.sleep(10)
139 else:
140 print('VOLTHA core is already running. Skipped start')
A.R Karthick57fa9372017-05-24 12:47:03 -0700141
A R Karthick168e2342017-08-15 16:13:10 -0700142 ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \
143 nohup python ofagent/main.py -v --consul=localhost:8500 \
144 --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \
145 >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc,
146 self.service_map['fluentd']['ip'],
147 self.controller)
148 if not self.service_running('python ofagent/main.py'):
149 ofagent_pki_dir = '/ofagent'
150 if os.access(pki_dir, os.F_OK):
151 pki_xfer_cmd = 'mkdir -p {} && cp -rv {}/pki {}'.format(ofagent_pki_dir,
152 self.voltha_loc,
153 ofagent_pki_dir)
154 os.system(pki_xfer_cmd)
155 ret = os.system(ofagent_start_cmd)
156 if ret != 0:
157 raise Exception('VOLTHA ofagent not started. Failed with return code %d' %ret)
158 time.sleep(10)
159 else:
160 print('VOLTHA ofagent is already running. Skipped start')
A.R Karthickc2697a12017-05-24 14:01:15 -0700161
A.R Karthick12e08c32017-05-30 17:09:26 -0700162 ponsim_start_cmd = "cd {} && sh -c '. ./env.sh && \
A.R Karthick4f583842017-06-09 17:15:47 -0700163 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 -0700164 if not self.service_running('python ponsim/main.py'):
165 ret = os.system(ponsim_start_cmd)
166 if ret != 0:
167 raise Exception('PONSIM not started. Failed with return code %d' %ret)
168 time.sleep(3)
169 else:
170 print('PONSIM already running. Skipped start')
171
A.R Karthickc2697a12017-05-24 14:01:15 -0700172 def service_running(self, pattern):
173 st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern))
174 return True if st == 0 else False
A.R Karthick57fa9372017-05-24 12:47:03 -0700175
176 def kill_service(self, pattern):
177 st, output = getstatusoutput('pgrep -f "{}"'.format(pattern))
178 if st == 0 and output:
179 pids = output.strip().splitlines()
180 for pid in pids:
181 try:
182 os.kill(int(pid), signal.SIGKILL)
183 except:
184 pass
185
186 def stop(self):
A R Karthick168e2342017-08-15 16:13:10 -0700187 if self.CONTAINER_MODE is False:
188 self.kill_service('python voltha/main.py')
189 self.kill_service('python ofagent/main.py')
A R Karthick168e2342017-08-15 16:13:10 -0700190 self.kill_service('python ponsim/main.py')
A R Karthickaa6475d2017-08-15 17:24:36 -0700191 self.kill_service('python chameleon/main.py')
A R Karthick168e2342017-08-15 16:13:10 -0700192 service_stop_cmd = 'DOCKER_HOST_IP={} docker-compose -p {} -f {} down'.format(self.DOCKER_HOST_IP,
193 self.PROJECT,
194 self.compose_file_loc)
A.R Karthick57fa9372017-05-24 12:47:03 -0700195 os.system(service_stop_cmd)
A R Karthick35495c32017-05-11 14:58:32 -0700196
A R Karthick168e2342017-08-15 16:13:10 -0700197 @classmethod
198 def get_ip(cls, service):
199 if service in cls.service_map:
200 return cls.service_map[service]['ip']
201 if service == cls.REST_SERVICE:
202 return os.getenv('VOLTHA_HOST', None)
203 return None
204
205 @classmethod
206 def get_network(cls, service):
207 if service in cls.service_map:
208 return cls.service_map[service]['network']
209 return None
210
A R Karthick35495c32017-05-11 14:58:32 -0700211class VolthaCtrl(object):
A R Karthick53442712017-07-27 12:23:30 -0700212 UPLINK_VLAN_START = 333
A.R Karthick4c4d0492017-05-26 19:23:05 -0700213 UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' }
A R Karthickbf1e4b02017-07-11 20:17:14 -0700214 REST_PORT = 8881
A R Karthick53442712017-07-27 12:23:30 -0700215 HOST = '172.17.0.1'
A R Karthick6e70e142017-07-28 15:25:38 -0700216 ONOS_APPS = ('org.onosproject.dhcp', 'org.onosproject.dhcp-relay', 'org.ciena.cordigmp')
A R Karthick32e711a2017-08-22 16:27:22 -0700217 ADMIN_STATE = 'admin_state'
218 OPER_STATUS = 'oper_status'
219 CONNECT_STATUS = 'connect_status'
A.R Karthick4c4d0492017-05-26 19:23:05 -0700220
A R Karthick53442712017-07-27 12:23:30 -0700221 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 -0700222 self.host = host
223 self.rest_port = rest_port
A R Karthick32e711a2017-08-22 16:27:22 -0700224 self.rest_url = 'http://{}:{}/api/v1/local'.format(host, rest_port)
225 if rest_port == 8882:
226 self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port)
227 self.ADMIN_STATE = 'adminState'
228 self.OPER_STATUS = 'operStatus'
229 self.CONNECT_STATUS = 'connectStatus'
A.R Karthick4c4d0492017-05-26 19:23:05 -0700230 self.uplink_vlan_map = uplink_vlan_map
A R Karthick53442712017-07-27 12:23:30 -0700231 VolthaCtrl.UPLINK_VLAN_START = uplink_vlan_start
A.R Karthick4c4d0492017-05-26 19:23:05 -0700232 self.switches = []
233 self.switch_map = {}
234
235 def config(self, fake = False):
236 devices = OnosCtrl.get_devices()
237 if not devices:
238 return self.switch_map
239 voltha_devices = filter(lambda d: not d['mfr'].startswith('Nicira'), devices)
240 self.switches = voltha_devices
241 device_config = { 'devices' : { } }
242 device_id = None
243 for device in voltha_devices:
244 device_id = device['id']
245 ports = OnosCtrl.get_ports_device(device_id)
246 nni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('nni'), ports)
247 uni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('uni'), ports)
248 if device_id not in self.uplink_vlan_map:
A R Karthick53442712017-07-27 12:23:30 -0700249 uplink_vlan = VolthaCtrl.UPLINK_VLAN_START
250 VolthaCtrl.UPLINK_VLAN_START += 1
251 log.info('Voltha device %s not in map. Using uplink vlan %d' %(device_id, uplink_vlan))
252 else:
253 uplink_vlan = self.uplink_vlan_map[device_id]
A.R Karthick4c4d0492017-05-26 19:23:05 -0700254 if not nni_ports:
255 log.info('Voltha device %s has no NNI ports' %device_id)
256 if fake is True:
257 log.info('Faking NNI port 0')
258 nni_ports = [ {'port': '0'} ]
259 else:
260 log.info('Skip configuring device %s' %device_id)
261 continue
262 if not uni_ports:
263 log.info('Voltha device %s has no UNI ports' %device_id)
264 if fake is True:
265 log.info('Faking UNI port 252')
266 uni_ports = [ {'port': '252'} ]
267 else:
268 log.info('Skip configuring device %s' %device_id)
269 continue
A.R Karthick4c4d0492017-05-26 19:23:05 -0700270 onu_ports = map(lambda uni: uni['port'], uni_ports)
271 self.switch_map[device_id] = dict(uplink_vlan = uplink_vlan, ports = onu_ports)
272 device_config['devices'][device_id] = {}
273 device_config['devices'][device_id]['basic'] = dict(driver='pmc-olt')
274 device_config['devices'][device_id]['accessDevice'] = dict(uplink=nni_ports[0]['port'],
275 vlan = uplink_vlan,
276 defaultVlan='0'
277 )
278 if device_id:
279 #toggle drivers/openflow base before reconfiguring the driver and olt config data
280 OnosCtrl('org.onosproject.drivers').deactivate()
281 OnosCtrl('org.onosproject.openflow-base').deactivate()
282 OnosCtrl.config(device_config)
A R Karthick6e70e142017-07-28 15:25:38 -0700283 time.sleep(10)
A.R Karthick4c4d0492017-05-26 19:23:05 -0700284 OnosCtrl('org.onosproject.drivers').activate()
285 OnosCtrl('org.onosproject.openflow-base').activate()
286 time.sleep(5)
A R Karthick6e70e142017-07-28 15:25:38 -0700287 log.info('Reactivating CORD and ONOS apps')
288 Onos.activate_cord_apps(deactivate = True)
289 Onos.activate_apps(self.ONOS_APPS, deactivate = True)
A.R Karthick4c4d0492017-05-26 19:23:05 -0700290
291 return self.switch_map
A R Karthick35495c32017-05-11 14:58:32 -0700292
293 def get_devices(self):
A R Karthick32e711a2017-08-22 16:27:22 -0700294 url = '{}/devices'.format(self.rest_url)
A R Karthick35495c32017-05-11 14:58:32 -0700295 resp = requests.get(url)
296 if resp.ok is not True or resp.status_code != 200:
297 return None
298 return resp.json()
299
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700300 def enable_device(self, olt_type, olt_mac = None, address = None):
A R Karthick32e711a2017-08-22 16:27:22 -0700301 url = '{}/devices'.format(self.rest_url)
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700302 if olt_mac is None and address is None:
303 log.error('Either olt mac or address needs to be specified')
A.R Karthick8a507cf2017-06-02 18:44:49 -0700304 return None, False
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700305 if olt_mac is not None:
306 device_config = { 'type' : olt_type, 'mac_address' : olt_mac }
307 else:
A R Karthick31a40172017-08-14 12:06:09 -0700308 if len(address.split(':')) > 1:
309 device_config = { 'type' : olt_type, 'host_and_port' : address }
310 else:
311 device_config = { 'type' : olt_type, 'ipv4_address' : address }
A R Karthick35495c32017-05-11 14:58:32 -0700312 #pre-provision
A.R Karthick8b9c5f12017-05-30 17:47:08 -0700313 if olt_mac is not None:
314 log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac))
315 else:
A R Karthickaa6475d2017-08-15 17:24:36 -0700316 log.info('Pre-provisioning %s with address %s' %(olt_type, address))
A R Karthick35495c32017-05-11 14:58:32 -0700317 resp = requests.post(url, data = json.dumps(device_config))
318 if resp.ok is not True or resp.status_code != 200:
A.R Karthick8a507cf2017-06-02 18:44:49 -0700319 return None, False
A R Karthick35495c32017-05-11 14:58:32 -0700320 device_id = resp.json()['id']
321 log.info('Enabling device %s' %(device_id))
322 enable_url = '{}/{}/enable'.format(url, device_id)
323 resp = requests.post(enable_url)
324 if resp.ok is not True or resp.status_code != 200:
A.R Karthick8a507cf2017-06-02 18:44:49 -0700325 return None, False
A R Karthick35495c32017-05-11 14:58:32 -0700326 #get operational status
A R Karthick090631b2017-07-25 16:05:05 -0700327 time.sleep(10)
A R Karthick35495c32017-05-11 14:58:32 -0700328 log.info('Checking operational status for device %s' %(device_id))
329 resp = requests.get('{}/{}'.format(url, device_id))
330 if resp.ok is not True or resp.status_code != 200:
A.R Karthick8a507cf2017-06-02 18:44:49 -0700331 return device_id, False
A R Karthick35495c32017-05-11 14:58:32 -0700332 device_info = resp.json()
A R Karthick32e711a2017-08-22 16:27:22 -0700333 if device_info[self.OPER_STATUS] != 'ACTIVE' or \
334 device_info[self.ADMIN_STATE] != 'ENABLED' or \
335 device_info[self.CONNECT_STATUS] != 'REACHABLE':
A.R Karthick8a507cf2017-06-02 18:44:49 -0700336 return device_id, False
A R Karthick35495c32017-05-11 14:58:32 -0700337
A.R Karthick8a507cf2017-06-02 18:44:49 -0700338 return device_id, True
339
340 def disable_device(self, device_id, delete = True):
341 log.info('Disabling device %s' %(device_id))
A R Karthick32e711a2017-08-22 16:27:22 -0700342 disable_url = '{}/devices/{}/disable'.format(self.rest_url, device_id)
A.R Karthick8a507cf2017-06-02 18:44:49 -0700343 resp = requests.post(disable_url)
344 if resp.ok is not True or resp.status_code != 200:
345 return False
346 if delete is True:
A R Karthickd69d5702017-07-25 11:31:47 -0700347 #rest for disable completion
348 time.sleep(10)
A.R Karthick8a507cf2017-06-02 18:44:49 -0700349 log.info('Deleting device %s' %(device_id))
A R Karthick32e711a2017-08-22 16:27:22 -0700350 delete_url = '{}/devices/{}/delete'.format(self.rest_url, device_id)
A.R Karthick8a507cf2017-06-02 18:44:49 -0700351 resp = requests.delete(delete_url)
352 if resp.status_code not in [204, 202, 200]:
353 return False
A R Karthick35495c32017-05-11 14:58:32 -0700354 return True
Chetan Gaonker3620a112017-05-23 06:10:15 +0000355
Thangavelu K Se6c77c72017-06-23 21:28:48 +0000356 def restart_device(self, device_id):
357 log.info('Restarting olt or onu device %s' %(device_id))
A R Karthick32e711a2017-08-22 16:27:22 -0700358 disable_url = '{}/devices/{}/restart'.format(self.rest_url, device_id)
Thangavelu K Se6c77c72017-06-23 21:28:48 +0000359 resp = requests.post(disable_url)
360 if resp.ok is not True or resp.status_code != 200:
361 return False
362 return True
363
364 def pause_device(self, device_id):
365 log.info('Restarting olt or onu device %s' %(device_id))
A R Karthick32e711a2017-08-22 16:27:22 -0700366 disable_url = '{}/devices/{}/pause'.format(self.rest_url, device_id)
Thangavelu K Se6c77c72017-06-23 21:28:48 +0000367 resp = requests.post(disable_url)
368 if resp.ok is not True or resp.status_code != 200:
369 return False
370 return True
371
Chetan Gaonker3620a112017-05-23 06:10:15 +0000372 def get_operational_status(self, device_id):
A R Karthick32e711a2017-08-22 16:27:22 -0700373 url = '{}/devices'.format(self.rest_url)
Chetan Gaonker3620a112017-05-23 06:10:15 +0000374 log.info('Checking operational status for device %s' %(device_id))
375 resp = requests.get('{}/{}'.format(url, device_id))
376 if resp.ok is not True or resp.status_code != 200:
377 return False
378 device_info = resp.json()
A R Karthick32e711a2017-08-22 16:27:22 -0700379 if device_info[self.OPER_STATUS] != 'ACTIVE' or \
380 device_info[self.ADMIN_STATE] != 'ENABLED' or \
381 device_info[self.CONNECT_STATUS] != 'REACHABLE':
Chetan Gaonker3620a112017-05-23 06:10:15 +0000382 return False
383 return True
384
385 def check_preprovision_status(self, device_id):
A R Karthick32e711a2017-08-22 16:27:22 -0700386 url = '{}/devices'.format(self.rest_url)
Chetan Gaonker3620a112017-05-23 06:10:15 +0000387 log.info('Check if device %s is in Preprovisioning state'%(device_id))
388 resp = requests.get('{}/{}'.format(url, device_id))
389 if resp.ok is not True or resp.status_code != 200:
390 return False
391 device_info = resp.json()
A R Karthick32e711a2017-08-22 16:27:22 -0700392 if device_info[self.ADMIN_STATE] == 'PREPROVISIONED':
Chetan Gaonker3620a112017-05-23 06:10:15 +0000393 return True
394 return False
A R Karthickbf1e4b02017-07-11 20:17:14 -0700395
396def get_olt_app():
397 our_path = os.path.dirname(os.path.realpath(__file__))
398 version = Onos.getVersion()
399 major = int(version.split('.')[0])
400 minor = int(version.split('.')[1])
401 olt_app_version = '1.2-SNAPSHOT'
402 if major > 1:
403 olt_app_version = '2.0-SNAPSHOT'
404 elif major == 1:
405 if minor > 10:
406 olt_app_version = '2.0-SNAPSHOT'
407 elif minor <= 8:
408 olt_app_version = '1.1-SNAPSHOT'
409 olt_app_file = os.path.join(our_path, '..', 'apps/olt-app-{}.oar'.format(olt_app_version))
410 return olt_app_file
411
A R Karthick168e2342017-08-15 16:13:10 -0700412def voltha_setup(host = '172.17.0.1', ponsim_host = VolthaService.PONSIM_HOST, olt_ip = None, rest_port = VolthaCtrl.REST_PORT,
A R Karthick9dc6e922017-07-12 14:40:16 -0700413 olt_type = 'ponsim_olt', olt_mac = '00:0c:e2:31:12:00',
A R Karthickbf1e4b02017-07-11 20:17:14 -0700414 uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP,
A R Karthick53442712017-07-27 12:23:30 -0700415 uplink_vlan_start = VolthaCtrl.UPLINK_VLAN_START,
A R Karthickbf1e4b02017-07-11 20:17:14 -0700416 config_fake = False, olt_app = None):
417
A R Karthick53442712017-07-27 12:23:30 -0700418 voltha = VolthaCtrl(host, rest_port = rest_port,
419 uplink_vlan_map = uplink_vlan_map,
420 uplink_vlan_start = uplink_vlan_start)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700421 if olt_type.startswith('ponsim'):
A R Karthick168e2342017-08-15 16:13:10 -0700422 ponsim_address = '{}:50060'.format(ponsim_host)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700423 log.info('Enabling ponsim olt')
424 device_id, status = voltha.enable_device(olt_type, address = ponsim_address)
425 else:
A R Karthick31a40172017-08-14 12:06:09 -0700426 if olt_type.startswith('maple'):
427 if olt_ip:
428 log.info('Enabling %s' %olt_type)
429 device_id, status = voltha.enable_device(olt_type, address = olt_ip)
430 else:
431 log.info('OLT IP needs to be specified for maple olt')
432 else:
433 log.info('Enabling OLT instance for %s with mac %s' %(olt_type, olt_mac))
434 device_id, status = voltha.enable_device(olt_type, olt_mac)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700435
436 if device_id is None or status is False:
A R Karthick9dc6e922017-07-12 14:40:16 -0700437 if device_id:
438 voltha.disable_device(device_id)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700439 return None
440
441 switch_map = None
442 olt_installed = False
443 if olt_app is None:
444 olt_app = get_olt_app()
445 try:
A R Karthick090631b2017-07-25 16:05:05 -0700446 time.sleep(5)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700447 switch_map = voltha.config(fake = config_fake)
448 if switch_map is None:
449 voltha.disable_device(device_id)
450 return None
451 log.info('Installing OLT app %s' %olt_app)
452 OnosCtrl.install_app(olt_app)
453 olt_installed = True
454 time.sleep(5)
455 return voltha, device_id, switch_map
456 except:
457 voltha.disable_device(device_id)
458 time.sleep(10)
459 if olt_installed is True:
460 log.info('Uninstalling OLT app %s' %olt_app)
461 OnosCtrl.uninstall_app(olt_app)
462
463 return None
464
465def voltha_teardown(voltha_ctrl, device_id, switch_map, olt_app = None):
466 voltha_ctrl.disable_device(device_id)
467 time.sleep(10)
468 if olt_app is None:
469 olt_app = get_olt_app()
A R Karthickdd064632017-07-12 13:02:17 -0700470 log.info('Uninstalling OLT app %s' %olt_app)
A R Karthickbf1e4b02017-07-11 20:17:14 -0700471 OnosCtrl.uninstall_app(olt_app)