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 | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 7 | from CordContainer import Container |
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 | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 60 | time.sleep(5) |
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 |
| 65 | voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 66 | nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \ |
| 67 | --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \ |
| 68 | >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc, |
| 69 | self.service_map['kafka']['ip'], |
| 70 | self.interface, |
| 71 | self.service_map['fluentd']['ip']) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 72 | if not self.service_running('python voltha/main.py'): |
| 73 | ret = os.system(voltha_start_cmd) |
| 74 | if ret != 0: |
| 75 | raise Exception('Failed to start VOLTHA. Return code %d' %ret) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 76 | time.sleep(5) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 77 | else: |
| 78 | print('VOLTHA core is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 79 | |
| 80 | ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 81 | nohup python ofagent/main.py -v --consul=localhost:8500 \ |
| 82 | --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \ |
| 83 | >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc, |
| 84 | self.service_map['fluentd']['ip'], |
| 85 | self.controller) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 86 | if not self.service_running('python ofagent/main.py'): |
| 87 | ret = os.system(ofagent_start_cmd) |
| 88 | if ret != 0: |
| 89 | raise Exception('VOLTHA ofagent not started. Failed with return code %d' %ret) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 90 | time.sleep(3) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 91 | else: |
| 92 | print('VOLTHA ofagent is already running. Skipped start') |
| 93 | |
A.R Karthick | 12e08c3 | 2017-05-30 17:09:26 -0700 | [diff] [blame] | 94 | ponsim_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 95 | 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] | 96 | if not self.service_running('python ponsim/main.py'): |
| 97 | ret = os.system(ponsim_start_cmd) |
| 98 | if ret != 0: |
| 99 | raise Exception('PONSIM not started. Failed with return code %d' %ret) |
| 100 | time.sleep(3) |
| 101 | else: |
| 102 | print('PONSIM already running. Skipped start') |
| 103 | |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 104 | def service_running(self, pattern): |
| 105 | st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 106 | return True if st == 0 else False |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 107 | |
| 108 | def kill_service(self, pattern): |
| 109 | st, output = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 110 | if st == 0 and output: |
| 111 | pids = output.strip().splitlines() |
| 112 | for pid in pids: |
| 113 | try: |
| 114 | os.kill(int(pid), signal.SIGKILL) |
| 115 | except: |
| 116 | pass |
| 117 | |
| 118 | def stop(self): |
| 119 | self.kill_service('python voltha/main.py') |
| 120 | self.kill_service('python ofagent/main.py') |
| 121 | self.kill_service('python chameleon/main.py') |
A.R Karthick | 12e08c3 | 2017-05-30 17:09:26 -0700 | [diff] [blame] | 122 | self.kill_service('python ponsim/main.py') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 123 | service_stop_cmd = 'docker-compose -f {} down'.format(self.compose_file_loc) |
| 124 | os.system(service_stop_cmd) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 125 | |
| 126 | class VolthaCtrl(object): |
| 127 | |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 128 | UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' } |
| 129 | |
| 130 | def __init__(self, host, rest_port = 8881, uplink_vlan_map = UPLINK_VLAN_MAP): |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 131 | self.host = host |
| 132 | self.rest_port = rest_port |
| 133 | self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 134 | self.uplink_vlan_map = uplink_vlan_map |
| 135 | self.switches = [] |
| 136 | self.switch_map = {} |
| 137 | |
| 138 | def config(self, fake = False): |
| 139 | devices = OnosCtrl.get_devices() |
| 140 | if not devices: |
| 141 | return self.switch_map |
| 142 | voltha_devices = filter(lambda d: not d['mfr'].startswith('Nicira'), devices) |
| 143 | self.switches = voltha_devices |
| 144 | device_config = { 'devices' : { } } |
| 145 | device_id = None |
| 146 | for device in voltha_devices: |
| 147 | device_id = device['id'] |
| 148 | ports = OnosCtrl.get_ports_device(device_id) |
| 149 | nni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('nni'), ports) |
| 150 | uni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('uni'), ports) |
| 151 | if device_id not in self.uplink_vlan_map: |
| 152 | log.info('Skipping voltha device %s as uplink vlan does not exist' %device_id) |
| 153 | continue |
| 154 | if not nni_ports: |
| 155 | log.info('Voltha device %s has no NNI ports' %device_id) |
| 156 | if fake is True: |
| 157 | log.info('Faking NNI port 0') |
| 158 | nni_ports = [ {'port': '0'} ] |
| 159 | else: |
| 160 | log.info('Skip configuring device %s' %device_id) |
| 161 | continue |
| 162 | if not uni_ports: |
| 163 | log.info('Voltha device %s has no UNI ports' %device_id) |
| 164 | if fake is True: |
| 165 | log.info('Faking UNI port 252') |
| 166 | uni_ports = [ {'port': '252'} ] |
| 167 | else: |
| 168 | log.info('Skip configuring device %s' %device_id) |
| 169 | continue |
| 170 | uplink_vlan = self.uplink_vlan_map[device_id] |
| 171 | onu_ports = map(lambda uni: uni['port'], uni_ports) |
| 172 | self.switch_map[device_id] = dict(uplink_vlan = uplink_vlan, ports = onu_ports) |
| 173 | device_config['devices'][device_id] = {} |
| 174 | device_config['devices'][device_id]['basic'] = dict(driver='pmc-olt') |
| 175 | device_config['devices'][device_id]['accessDevice'] = dict(uplink=nni_ports[0]['port'], |
| 176 | vlan = uplink_vlan, |
| 177 | defaultVlan='0' |
| 178 | ) |
| 179 | if device_id: |
| 180 | #toggle drivers/openflow base before reconfiguring the driver and olt config data |
| 181 | OnosCtrl('org.onosproject.drivers').deactivate() |
| 182 | OnosCtrl('org.onosproject.openflow-base').deactivate() |
| 183 | OnosCtrl.config(device_config) |
| 184 | time.sleep(2) |
| 185 | OnosCtrl('org.onosproject.drivers').activate() |
| 186 | OnosCtrl('org.onosproject.openflow-base').activate() |
| 187 | time.sleep(5) |
| 188 | |
| 189 | return self.switch_map |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 190 | |
| 191 | def get_devices(self): |
| 192 | url = '{}/local/devices'.format(self.rest_url) |
| 193 | resp = requests.get(url) |
| 194 | if resp.ok is not True or resp.status_code != 200: |
| 195 | return None |
| 196 | return resp.json() |
| 197 | |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 198 | def enable_device(self, olt_type, olt_mac = None, address = None): |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 199 | url = '{}/local/devices'.format(self.rest_url) |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 200 | if olt_mac is None and address is None: |
| 201 | log.error('Either olt mac or address needs to be specified') |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 202 | return None, False |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 203 | if olt_mac is not None: |
| 204 | device_config = { 'type' : olt_type, 'mac_address' : olt_mac } |
| 205 | else: |
| 206 | device_config = { 'type' : olt_type, 'host_and_port' : address } |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 207 | #pre-provision |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 208 | if olt_mac is not None: |
| 209 | log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac)) |
| 210 | else: |
| 211 | log.info('Pre-provisioning %s with address %s' %(olt_type, address)) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 212 | resp = requests.post(url, data = json.dumps(device_config)) |
| 213 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 214 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 215 | device_id = resp.json()['id'] |
| 216 | log.info('Enabling device %s' %(device_id)) |
| 217 | enable_url = '{}/{}/enable'.format(url, device_id) |
| 218 | resp = requests.post(enable_url) |
| 219 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 220 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 221 | #get operational status |
| 222 | time.sleep(5) |
| 223 | log.info('Checking operational status for device %s' %(device_id)) |
| 224 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 225 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 226 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 227 | device_info = resp.json() |
| 228 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 229 | device_info['admin_state'] != 'ENABLED' or \ |
| 230 | device_info['connect_status'] != 'REACHABLE': |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 231 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 232 | |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 233 | return device_id, True |
| 234 | |
| 235 | def disable_device(self, device_id, delete = True): |
| 236 | log.info('Disabling device %s' %(device_id)) |
| 237 | disable_url = '{}/local/devices/{}/disable'.format(self.rest_url, device_id) |
| 238 | resp = requests.post(disable_url) |
| 239 | if resp.ok is not True or resp.status_code != 200: |
| 240 | return False |
| 241 | if delete is True: |
| 242 | log.info('Deleting device %s' %(device_id)) |
| 243 | delete_url = '{}/local/devices/{}/delete'.format(self.rest_url, device_id) |
| 244 | resp = requests.delete(delete_url) |
| 245 | if resp.status_code not in [204, 202, 200]: |
| 246 | return False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 247 | return True |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 248 | |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 249 | def restart_device(self, device_id): |
| 250 | log.info('Restarting olt or onu device %s' %(device_id)) |
| 251 | disable_url = '{}/local/devices/{}/restart'.format(self.rest_url, device_id) |
| 252 | resp = requests.post(disable_url) |
| 253 | if resp.ok is not True or resp.status_code != 200: |
| 254 | return False |
| 255 | return True |
| 256 | |
| 257 | def pause_device(self, device_id): |
| 258 | log.info('Restarting olt or onu device %s' %(device_id)) |
| 259 | disable_url = '{}/local/devices/{}/pause'.format(self.rest_url, device_id) |
| 260 | resp = requests.post(disable_url) |
| 261 | if resp.ok is not True or resp.status_code != 200: |
| 262 | return False |
| 263 | return True |
| 264 | |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 265 | def get_operational_status(self, device_id): |
| 266 | url = '{}/local/devices'.format(self.rest_url) |
| 267 | log.info('Checking operational status for device %s' %(device_id)) |
| 268 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 269 | if resp.ok is not True or resp.status_code != 200: |
| 270 | return False |
| 271 | device_info = resp.json() |
| 272 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 273 | device_info['admin_state'] != 'ENABLED' or \ |
| 274 | device_info['connect_status'] != 'REACHABLE': |
| 275 | return False |
| 276 | return True |
| 277 | |
| 278 | def check_preprovision_status(self, device_id): |
| 279 | url = '{}/local/devices'.format(self.rest_url) |
| 280 | log.info('Check if device %s is in Preprovisioning state'%(device_id)) |
| 281 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 282 | if resp.ok is not True or resp.status_code != 200: |
| 283 | return False |
| 284 | device_info = resp.json() |
| 285 | if device_info['admin_status'] == 'PREPROVISIONED': |
| 286 | return True |
| 287 | return False |