A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 1 | import requests |
| 2 | import json |
| 3 | import time |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 4 | import os |
| 5 | import signal |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 6 | from CordTestUtils import log_test as log, getstatusoutput, get_controller |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 7 | from CordContainer import Container, Onos |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 8 | from OnosCtrl import OnosCtrl |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 9 | from OltConfig import OltConfig |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 10 | |
| 11 | class VolthaService(object): |
| 12 | services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd') |
| 13 | compose_file = 'docker-compose-system-test.yml' |
| 14 | service_map = {} |
| 15 | |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 16 | def __init__(self, voltha_loc, controller, interface = 'eth0', olt_config = None): |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 17 | if not os.access(voltha_loc, os.F_OK): |
| 18 | raise Exception('Voltha location %s not found' %voltha_loc) |
| 19 | compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file) |
| 20 | if not os.access(compose_file_loc, os.F_OK): |
| 21 | raise Exception('Voltha compose file %s not found' %compose_file_loc) |
| 22 | self.voltha_loc = voltha_loc |
| 23 | self.controller = controller |
| 24 | self.interface = interface |
| 25 | self.compose_file_loc = compose_file_loc |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 26 | num_onus = 1 |
| 27 | if olt_config is not None: |
| 28 | port_map, _ = OltConfig(olt_config).olt_port_map() |
| 29 | if port_map['ponsim'] is True: |
| 30 | num_onus = max(1, len(port_map['ports'])) |
| 31 | self.num_onus = num_onus |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 32 | |
| 33 | def start(self): |
| 34 | start_cmd = 'docker-compose -f {} up -d {} {} {} {} {}'.format(self.compose_file_loc, |
| 35 | *self.services) |
| 36 | ret = os.system(start_cmd) |
| 37 | if ret != 0: |
| 38 | raise Exception('Failed to start voltha services. Failed with code %d' %ret) |
| 39 | |
| 40 | for service in self.services: |
| 41 | name = 'compose_{}_1'.format(service) |
| 42 | network = 'compose_default' |
| 43 | cnt = Container(name, name) |
| 44 | ip = cnt.ip(network = network) |
| 45 | if not ip: |
| 46 | raise Exception('IP not found for container %s' %name) |
| 47 | print('IP %s for service %s' %(ip, service)) |
| 48 | self.service_map[service] = dict(name = name, network = network, ip = ip) |
| 49 | |
| 50 | #first start chameleon |
| 51 | chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 52 | nohup python chameleon/main.py -v --consul=localhost:8500 \ |
| 53 | --fluentd={}:24224 --grpc-endpoint=localhost:50555 \ |
| 54 | >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc, |
| 55 | self.service_map['fluentd']['ip']) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 56 | if not self.service_running('python chameleon/main.py'): |
| 57 | ret = os.system(chameleon_start_cmd) |
| 58 | if ret != 0: |
| 59 | raise Exception('VOLTHA chameleon service not started. Failed with return code %d' %ret) |
A R Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 60 | time.sleep(10) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 61 | else: |
| 62 | print('Chameleon voltha sevice is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 63 | |
| 64 | #now start voltha and ofagent |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 65 | voltha_setup_cmd = "cd {} && sh -c '. ./env.sh && make rebuild-venv && make protos'".format(self.voltha_loc) |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 66 | voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 67 | nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \ |
| 68 | --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \ |
| 69 | >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc, |
| 70 | self.service_map['kafka']['ip'], |
| 71 | self.interface, |
| 72 | self.service_map['fluentd']['ip']) |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 73 | pki_dir = '{}/pki'.format(self.voltha_loc) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 74 | if not self.service_running('python voltha/main.py'): |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 75 | voltha_pki_dir = '/voltha' |
| 76 | if os.access(pki_dir, os.F_OK): |
| 77 | pki_xfer_cmd = 'mkdir -p {} && cp -rv {}/pki {}'.format(voltha_pki_dir, |
| 78 | self.voltha_loc, |
| 79 | voltha_pki_dir) |
| 80 | os.system(pki_xfer_cmd) |
| 81 | #os.system(voltha_setup_cmd) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 82 | ret = os.system(voltha_start_cmd) |
| 83 | if ret != 0: |
| 84 | raise Exception('Failed to start VOLTHA. Return code %d' %ret) |
A R Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 85 | time.sleep(10) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 86 | else: |
| 87 | print('VOLTHA core is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 88 | |
| 89 | ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 90 | nohup python ofagent/main.py -v --consul=localhost:8500 \ |
| 91 | --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \ |
| 92 | >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc, |
| 93 | self.service_map['fluentd']['ip'], |
| 94 | self.controller) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 95 | if not self.service_running('python ofagent/main.py'): |
A R Karthick | d52ca8a | 2017-07-24 17:38:55 -0700 | [diff] [blame] | 96 | ofagent_pki_dir = '/ofagent' |
| 97 | if os.access(pki_dir, os.F_OK): |
| 98 | pki_xfer_cmd = 'mkdir -p {} && cp -rv {}/pki {}'.format(ofagent_pki_dir, |
| 99 | self.voltha_loc, |
| 100 | ofagent_pki_dir) |
| 101 | os.system(pki_xfer_cmd) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 102 | ret = os.system(ofagent_start_cmd) |
| 103 | if ret != 0: |
| 104 | raise Exception('VOLTHA ofagent not started. Failed with return code %d' %ret) |
A R Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 105 | time.sleep(10) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 106 | else: |
| 107 | print('VOLTHA ofagent is already running. Skipped start') |
| 108 | |
A.R Karthick | 12e08c3 | 2017-05-30 17:09:26 -0700 | [diff] [blame] | 109 | ponsim_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 110 | nohup python ponsim/main.py -o {} -v >/tmp/ponsim.log 2>&1 &'".format(self.voltha_loc, self.num_onus) |
A.R Karthick | 12e08c3 | 2017-05-30 17:09:26 -0700 | [diff] [blame] | 111 | if not self.service_running('python ponsim/main.py'): |
| 112 | ret = os.system(ponsim_start_cmd) |
| 113 | if ret != 0: |
| 114 | raise Exception('PONSIM not started. Failed with return code %d' %ret) |
| 115 | time.sleep(3) |
| 116 | else: |
| 117 | print('PONSIM already running. Skipped start') |
| 118 | |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 119 | def service_running(self, pattern): |
| 120 | st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 121 | return True if st == 0 else False |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 122 | |
| 123 | def kill_service(self, pattern): |
| 124 | st, output = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 125 | if st == 0 and output: |
| 126 | pids = output.strip().splitlines() |
| 127 | for pid in pids: |
| 128 | try: |
| 129 | os.kill(int(pid), signal.SIGKILL) |
| 130 | except: |
| 131 | pass |
| 132 | |
| 133 | def stop(self): |
| 134 | self.kill_service('python voltha/main.py') |
| 135 | self.kill_service('python ofagent/main.py') |
| 136 | self.kill_service('python chameleon/main.py') |
A.R Karthick | 12e08c3 | 2017-05-30 17:09:26 -0700 | [diff] [blame] | 137 | self.kill_service('python ponsim/main.py') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 138 | service_stop_cmd = 'docker-compose -f {} down'.format(self.compose_file_loc) |
| 139 | os.system(service_stop_cmd) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 140 | |
| 141 | class VolthaCtrl(object): |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 142 | UPLINK_VLAN_START = 333 |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 143 | UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' } |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 144 | REST_PORT = 8881 |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 145 | HOST = '172.17.0.1' |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 146 | ONOS_APPS = ('org.onosproject.dhcp', 'org.onosproject.dhcp-relay', 'org.ciena.cordigmp') |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 147 | |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 148 | def __init__(self, host = HOST, rest_port = REST_PORT, uplink_vlan_map = UPLINK_VLAN_MAP, uplink_vlan_start = UPLINK_VLAN_START): |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 149 | self.host = host |
| 150 | self.rest_port = rest_port |
| 151 | self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 152 | self.uplink_vlan_map = uplink_vlan_map |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 153 | VolthaCtrl.UPLINK_VLAN_START = uplink_vlan_start |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 154 | self.switches = [] |
| 155 | self.switch_map = {} |
| 156 | |
| 157 | def config(self, fake = False): |
| 158 | devices = OnosCtrl.get_devices() |
| 159 | if not devices: |
| 160 | return self.switch_map |
| 161 | voltha_devices = filter(lambda d: not d['mfr'].startswith('Nicira'), devices) |
| 162 | self.switches = voltha_devices |
| 163 | device_config = { 'devices' : { } } |
| 164 | device_id = None |
| 165 | for device in voltha_devices: |
| 166 | device_id = device['id'] |
| 167 | ports = OnosCtrl.get_ports_device(device_id) |
| 168 | nni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('nni'), ports) |
| 169 | uni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('uni'), ports) |
| 170 | if device_id not in self.uplink_vlan_map: |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 171 | uplink_vlan = VolthaCtrl.UPLINK_VLAN_START |
| 172 | VolthaCtrl.UPLINK_VLAN_START += 1 |
| 173 | log.info('Voltha device %s not in map. Using uplink vlan %d' %(device_id, uplink_vlan)) |
| 174 | else: |
| 175 | uplink_vlan = self.uplink_vlan_map[device_id] |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 176 | if not nni_ports: |
| 177 | log.info('Voltha device %s has no NNI ports' %device_id) |
| 178 | if fake is True: |
| 179 | log.info('Faking NNI port 0') |
| 180 | nni_ports = [ {'port': '0'} ] |
| 181 | else: |
| 182 | log.info('Skip configuring device %s' %device_id) |
| 183 | continue |
| 184 | if not uni_ports: |
| 185 | log.info('Voltha device %s has no UNI ports' %device_id) |
| 186 | if fake is True: |
| 187 | log.info('Faking UNI port 252') |
| 188 | uni_ports = [ {'port': '252'} ] |
| 189 | else: |
| 190 | log.info('Skip configuring device %s' %device_id) |
| 191 | continue |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 192 | onu_ports = map(lambda uni: uni['port'], uni_ports) |
| 193 | self.switch_map[device_id] = dict(uplink_vlan = uplink_vlan, ports = onu_ports) |
| 194 | device_config['devices'][device_id] = {} |
| 195 | device_config['devices'][device_id]['basic'] = dict(driver='pmc-olt') |
| 196 | device_config['devices'][device_id]['accessDevice'] = dict(uplink=nni_ports[0]['port'], |
| 197 | vlan = uplink_vlan, |
| 198 | defaultVlan='0' |
| 199 | ) |
| 200 | if device_id: |
| 201 | #toggle drivers/openflow base before reconfiguring the driver and olt config data |
| 202 | OnosCtrl('org.onosproject.drivers').deactivate() |
| 203 | OnosCtrl('org.onosproject.openflow-base').deactivate() |
| 204 | OnosCtrl.config(device_config) |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 205 | time.sleep(10) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 206 | OnosCtrl('org.onosproject.drivers').activate() |
| 207 | OnosCtrl('org.onosproject.openflow-base').activate() |
| 208 | time.sleep(5) |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 209 | log.info('Reactivating CORD and ONOS apps') |
| 210 | Onos.activate_cord_apps(deactivate = True) |
| 211 | Onos.activate_apps(self.ONOS_APPS, deactivate = True) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 212 | |
| 213 | return self.switch_map |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 214 | |
| 215 | def get_devices(self): |
| 216 | url = '{}/local/devices'.format(self.rest_url) |
| 217 | resp = requests.get(url) |
| 218 | if resp.ok is not True or resp.status_code != 200: |
| 219 | return None |
| 220 | return resp.json() |
| 221 | |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 222 | def enable_device(self, olt_type, olt_mac = None, address = None): |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 223 | url = '{}/local/devices'.format(self.rest_url) |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 224 | if olt_mac is None and address is None: |
| 225 | log.error('Either olt mac or address needs to be specified') |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 226 | return None, False |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 227 | if olt_mac is not None: |
| 228 | device_config = { 'type' : olt_type, 'mac_address' : olt_mac } |
| 229 | else: |
| 230 | device_config = { 'type' : olt_type, 'host_and_port' : address } |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 231 | #pre-provision |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 232 | if olt_mac is not None: |
| 233 | log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac)) |
| 234 | else: |
| 235 | log.info('Pre-provisioning %s with address %s' %(olt_type, address)) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 236 | resp = requests.post(url, data = json.dumps(device_config)) |
| 237 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 238 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 239 | device_id = resp.json()['id'] |
| 240 | log.info('Enabling device %s' %(device_id)) |
| 241 | enable_url = '{}/{}/enable'.format(url, device_id) |
| 242 | resp = requests.post(enable_url) |
| 243 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 244 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 245 | #get operational status |
A R Karthick | 090631b | 2017-07-25 16:05:05 -0700 | [diff] [blame] | 246 | time.sleep(10) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 247 | log.info('Checking operational status for device %s' %(device_id)) |
| 248 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 249 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 250 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 251 | device_info = resp.json() |
| 252 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 253 | device_info['admin_state'] != 'ENABLED' or \ |
| 254 | device_info['connect_status'] != 'REACHABLE': |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 255 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 256 | |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 257 | return device_id, True |
| 258 | |
| 259 | def disable_device(self, device_id, delete = True): |
| 260 | log.info('Disabling device %s' %(device_id)) |
| 261 | disable_url = '{}/local/devices/{}/disable'.format(self.rest_url, device_id) |
| 262 | resp = requests.post(disable_url) |
| 263 | if resp.ok is not True or resp.status_code != 200: |
| 264 | return False |
| 265 | if delete is True: |
A R Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 266 | #rest for disable completion |
| 267 | time.sleep(10) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 268 | log.info('Deleting device %s' %(device_id)) |
| 269 | delete_url = '{}/local/devices/{}/delete'.format(self.rest_url, device_id) |
| 270 | resp = requests.delete(delete_url) |
| 271 | if resp.status_code not in [204, 202, 200]: |
| 272 | return False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 273 | return True |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 274 | |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 275 | def restart_device(self, device_id): |
| 276 | log.info('Restarting olt or onu device %s' %(device_id)) |
| 277 | disable_url = '{}/local/devices/{}/restart'.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 | return True |
| 282 | |
| 283 | def pause_device(self, device_id): |
| 284 | log.info('Restarting olt or onu device %s' %(device_id)) |
| 285 | disable_url = '{}/local/devices/{}/pause'.format(self.rest_url, device_id) |
| 286 | resp = requests.post(disable_url) |
| 287 | if resp.ok is not True or resp.status_code != 200: |
| 288 | return False |
| 289 | return True |
| 290 | |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 291 | def get_operational_status(self, device_id): |
| 292 | url = '{}/local/devices'.format(self.rest_url) |
| 293 | log.info('Checking operational status for device %s' %(device_id)) |
| 294 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 295 | if resp.ok is not True or resp.status_code != 200: |
| 296 | return False |
| 297 | device_info = resp.json() |
| 298 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 299 | device_info['admin_state'] != 'ENABLED' or \ |
| 300 | device_info['connect_status'] != 'REACHABLE': |
| 301 | return False |
| 302 | return True |
| 303 | |
| 304 | def check_preprovision_status(self, device_id): |
| 305 | url = '{}/local/devices'.format(self.rest_url) |
| 306 | log.info('Check if device %s is in Preprovisioning state'%(device_id)) |
| 307 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 308 | if resp.ok is not True or resp.status_code != 200: |
| 309 | return False |
| 310 | device_info = resp.json() |
| 311 | if device_info['admin_status'] == 'PREPROVISIONED': |
| 312 | return True |
| 313 | return False |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 314 | |
| 315 | def get_olt_app(): |
| 316 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 317 | version = Onos.getVersion() |
| 318 | major = int(version.split('.')[0]) |
| 319 | minor = int(version.split('.')[1]) |
| 320 | olt_app_version = '1.2-SNAPSHOT' |
| 321 | if major > 1: |
| 322 | olt_app_version = '2.0-SNAPSHOT' |
| 323 | elif major == 1: |
| 324 | if minor > 10: |
| 325 | olt_app_version = '2.0-SNAPSHOT' |
| 326 | elif minor <= 8: |
| 327 | olt_app_version = '1.1-SNAPSHOT' |
| 328 | olt_app_file = os.path.join(our_path, '..', 'apps/olt-app-{}.oar'.format(olt_app_version)) |
| 329 | return olt_app_file |
| 330 | |
| 331 | def voltha_setup(host = '172.17.0.1', rest_port = VolthaCtrl.REST_PORT, |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 332 | olt_type = 'ponsim_olt', olt_mac = '00:0c:e2:31:12:00', |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 333 | uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP, |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 334 | uplink_vlan_start = VolthaCtrl.UPLINK_VLAN_START, |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 335 | config_fake = False, olt_app = None): |
| 336 | |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 337 | voltha = VolthaCtrl(host, rest_port = rest_port, |
| 338 | uplink_vlan_map = uplink_vlan_map, |
| 339 | uplink_vlan_start = uplink_vlan_start) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 340 | if olt_type.startswith('ponsim'): |
| 341 | ponsim_address = '{}:50060'.format(host) |
| 342 | log.info('Enabling ponsim olt') |
| 343 | device_id, status = voltha.enable_device(olt_type, address = ponsim_address) |
| 344 | else: |
| 345 | log.info('Enabling OLT instance for %s with mac %s' %(olt_type, olt_mac)) |
| 346 | device_id, status = voltha.enable_device(olt_type, olt_mac) |
| 347 | |
| 348 | if device_id is None or status is False: |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 349 | if device_id: |
| 350 | voltha.disable_device(device_id) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 351 | return None |
| 352 | |
| 353 | switch_map = None |
| 354 | olt_installed = False |
| 355 | if olt_app is None: |
| 356 | olt_app = get_olt_app() |
| 357 | try: |
A R Karthick | 090631b | 2017-07-25 16:05:05 -0700 | [diff] [blame] | 358 | time.sleep(5) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 359 | switch_map = voltha.config(fake = config_fake) |
| 360 | if switch_map is None: |
| 361 | voltha.disable_device(device_id) |
| 362 | return None |
| 363 | log.info('Installing OLT app %s' %olt_app) |
| 364 | OnosCtrl.install_app(olt_app) |
| 365 | olt_installed = True |
| 366 | time.sleep(5) |
| 367 | return voltha, device_id, switch_map |
| 368 | except: |
| 369 | voltha.disable_device(device_id) |
| 370 | time.sleep(10) |
| 371 | if olt_installed is True: |
| 372 | log.info('Uninstalling OLT app %s' %olt_app) |
| 373 | OnosCtrl.uninstall_app(olt_app) |
| 374 | |
| 375 | return None |
| 376 | |
| 377 | def voltha_teardown(voltha_ctrl, device_id, switch_map, olt_app = None): |
| 378 | voltha_ctrl.disable_device(device_id) |
| 379 | time.sleep(10) |
| 380 | if olt_app is None: |
| 381 | olt_app = get_olt_app() |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 382 | log.info('Uninstalling OLT app %s' %olt_app) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 383 | OnosCtrl.uninstall_app(olt_app) |