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 | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 9 | |
| 10 | class VolthaService(object): |
| 11 | services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd') |
| 12 | compose_file = 'docker-compose-system-test.yml' |
| 13 | service_map = {} |
| 14 | |
| 15 | def __init__(self, voltha_loc, controller, interface = 'eth0'): |
| 16 | if not os.access(voltha_loc, os.F_OK): |
| 17 | raise Exception('Voltha location %s not found' %voltha_loc) |
| 18 | compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file) |
| 19 | if not os.access(compose_file_loc, os.F_OK): |
| 20 | raise Exception('Voltha compose file %s not found' %compose_file_loc) |
| 21 | self.voltha_loc = voltha_loc |
| 22 | self.controller = controller |
| 23 | self.interface = interface |
| 24 | self.compose_file_loc = compose_file_loc |
| 25 | |
| 26 | def start(self): |
| 27 | start_cmd = 'docker-compose -f {} up -d {} {} {} {} {}'.format(self.compose_file_loc, |
| 28 | *self.services) |
| 29 | ret = os.system(start_cmd) |
| 30 | if ret != 0: |
| 31 | raise Exception('Failed to start voltha services. Failed with code %d' %ret) |
| 32 | |
| 33 | for service in self.services: |
| 34 | name = 'compose_{}_1'.format(service) |
| 35 | network = 'compose_default' |
| 36 | cnt = Container(name, name) |
| 37 | ip = cnt.ip(network = network) |
| 38 | if not ip: |
| 39 | raise Exception('IP not found for container %s' %name) |
| 40 | print('IP %s for service %s' %(ip, service)) |
| 41 | self.service_map[service] = dict(name = name, network = network, ip = ip) |
| 42 | |
| 43 | #first start chameleon |
| 44 | chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 45 | nohup python chameleon/main.py -v --consul=localhost:8500 \ |
| 46 | --fluentd={}:24224 --grpc-endpoint=localhost:50555 \ |
| 47 | >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc, |
| 48 | self.service_map['fluentd']['ip']) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 49 | if not self.service_running('python chameleon/main.py'): |
| 50 | ret = os.system(chameleon_start_cmd) |
| 51 | if ret != 0: |
| 52 | 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] | 53 | time.sleep(5) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 54 | else: |
| 55 | print('Chameleon voltha sevice is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 56 | |
| 57 | #now start voltha and ofagent |
| 58 | voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 59 | nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \ |
| 60 | --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \ |
| 61 | >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc, |
| 62 | self.service_map['kafka']['ip'], |
| 63 | self.interface, |
| 64 | self.service_map['fluentd']['ip']) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 65 | if not self.service_running('python voltha/main.py'): |
| 66 | ret = os.system(voltha_start_cmd) |
| 67 | if ret != 0: |
| 68 | raise Exception('Failed to start VOLTHA. Return code %d' %ret) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 69 | time.sleep(5) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 70 | else: |
| 71 | print('VOLTHA core is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 72 | |
| 73 | ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 74 | nohup python ofagent/main.py -v --consul=localhost:8500 \ |
| 75 | --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \ |
| 76 | >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc, |
| 77 | self.service_map['fluentd']['ip'], |
| 78 | self.controller) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 79 | if not self.service_running('python ofagent/main.py'): |
| 80 | ret = os.system(ofagent_start_cmd) |
| 81 | if ret != 0: |
| 82 | 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] | 83 | time.sleep(3) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 84 | else: |
| 85 | print('VOLTHA ofagent is already running. Skipped start') |
| 86 | |
| 87 | def service_running(self, pattern): |
| 88 | st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 89 | return True if st == 0 else False |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 90 | |
| 91 | def kill_service(self, pattern): |
| 92 | st, output = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 93 | if st == 0 and output: |
| 94 | pids = output.strip().splitlines() |
| 95 | for pid in pids: |
| 96 | try: |
| 97 | os.kill(int(pid), signal.SIGKILL) |
| 98 | except: |
| 99 | pass |
| 100 | |
| 101 | def stop(self): |
| 102 | self.kill_service('python voltha/main.py') |
| 103 | self.kill_service('python ofagent/main.py') |
| 104 | self.kill_service('python chameleon/main.py') |
| 105 | service_stop_cmd = 'docker-compose -f {} down'.format(self.compose_file_loc) |
| 106 | os.system(service_stop_cmd) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 107 | |
| 108 | class VolthaCtrl(object): |
| 109 | |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 110 | UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' } |
| 111 | |
| 112 | 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] | 113 | self.host = host |
| 114 | self.rest_port = rest_port |
| 115 | self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 116 | self.uplink_vlan_map = uplink_vlan_map |
| 117 | self.switches = [] |
| 118 | self.switch_map = {} |
| 119 | |
| 120 | def config(self, fake = False): |
| 121 | devices = OnosCtrl.get_devices() |
| 122 | if not devices: |
| 123 | return self.switch_map |
| 124 | voltha_devices = filter(lambda d: not d['mfr'].startswith('Nicira'), devices) |
| 125 | self.switches = voltha_devices |
| 126 | device_config = { 'devices' : { } } |
| 127 | device_id = None |
| 128 | for device in voltha_devices: |
| 129 | device_id = device['id'] |
| 130 | ports = OnosCtrl.get_ports_device(device_id) |
| 131 | nni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('nni'), ports) |
| 132 | uni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('uni'), ports) |
| 133 | if device_id not in self.uplink_vlan_map: |
| 134 | log.info('Skipping voltha device %s as uplink vlan does not exist' %device_id) |
| 135 | continue |
| 136 | if not nni_ports: |
| 137 | log.info('Voltha device %s has no NNI ports' %device_id) |
| 138 | if fake is True: |
| 139 | log.info('Faking NNI port 0') |
| 140 | nni_ports = [ {'port': '0'} ] |
| 141 | else: |
| 142 | log.info('Skip configuring device %s' %device_id) |
| 143 | continue |
| 144 | if not uni_ports: |
| 145 | log.info('Voltha device %s has no UNI ports' %device_id) |
| 146 | if fake is True: |
| 147 | log.info('Faking UNI port 252') |
| 148 | uni_ports = [ {'port': '252'} ] |
| 149 | else: |
| 150 | log.info('Skip configuring device %s' %device_id) |
| 151 | continue |
| 152 | uplink_vlan = self.uplink_vlan_map[device_id] |
| 153 | onu_ports = map(lambda uni: uni['port'], uni_ports) |
| 154 | self.switch_map[device_id] = dict(uplink_vlan = uplink_vlan, ports = onu_ports) |
| 155 | device_config['devices'][device_id] = {} |
| 156 | device_config['devices'][device_id]['basic'] = dict(driver='pmc-olt') |
| 157 | device_config['devices'][device_id]['accessDevice'] = dict(uplink=nni_ports[0]['port'], |
| 158 | vlan = uplink_vlan, |
| 159 | defaultVlan='0' |
| 160 | ) |
| 161 | if device_id: |
| 162 | #toggle drivers/openflow base before reconfiguring the driver and olt config data |
| 163 | OnosCtrl('org.onosproject.drivers').deactivate() |
| 164 | OnosCtrl('org.onosproject.openflow-base').deactivate() |
| 165 | OnosCtrl.config(device_config) |
| 166 | time.sleep(2) |
| 167 | OnosCtrl('org.onosproject.drivers').activate() |
| 168 | OnosCtrl('org.onosproject.openflow-base').activate() |
| 169 | time.sleep(5) |
| 170 | |
| 171 | return self.switch_map |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 172 | |
| 173 | def get_devices(self): |
| 174 | url = '{}/local/devices'.format(self.rest_url) |
| 175 | resp = requests.get(url) |
| 176 | if resp.ok is not True or resp.status_code != 200: |
| 177 | return None |
| 178 | return resp.json() |
| 179 | |
| 180 | def enable_device(self, olt_type, olt_mac): |
| 181 | url = '{}/local/devices'.format(self.rest_url) |
| 182 | device_config = { 'type' : olt_type, 'mac_address' : olt_mac } |
| 183 | #pre-provision |
| 184 | log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac)) |
| 185 | resp = requests.post(url, data = json.dumps(device_config)) |
| 186 | if resp.ok is not True or resp.status_code != 200: |
| 187 | return False |
| 188 | device_id = resp.json()['id'] |
| 189 | log.info('Enabling device %s' %(device_id)) |
| 190 | enable_url = '{}/{}/enable'.format(url, device_id) |
| 191 | resp = requests.post(enable_url) |
| 192 | if resp.ok is not True or resp.status_code != 200: |
| 193 | return False |
| 194 | #get operational status |
| 195 | time.sleep(5) |
| 196 | log.info('Checking operational status for device %s' %(device_id)) |
| 197 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 198 | if resp.ok is not True or resp.status_code != 200: |
| 199 | return False |
| 200 | device_info = resp.json() |
| 201 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 202 | device_info['admin_state'] != 'ENABLED' or \ |
| 203 | device_info['connect_status'] != 'REACHABLE': |
| 204 | return False |
| 205 | |
| 206 | return True |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 207 | |
| 208 | def get_operational_status(self, device_id): |
| 209 | url = '{}/local/devices'.format(self.rest_url) |
| 210 | log.info('Checking operational status for device %s' %(device_id)) |
| 211 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 212 | if resp.ok is not True or resp.status_code != 200: |
| 213 | return False |
| 214 | device_info = resp.json() |
| 215 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 216 | device_info['admin_state'] != 'ENABLED' or \ |
| 217 | device_info['connect_status'] != 'REACHABLE': |
| 218 | return False |
| 219 | return True |
| 220 | |
| 221 | def check_preprovision_status(self, device_id): |
| 222 | url = '{}/local/devices'.format(self.rest_url) |
| 223 | log.info('Check if device %s is in Preprovisioning state'%(device_id)) |
| 224 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 225 | if resp.ok is not True or resp.status_code != 200: |
| 226 | return False |
| 227 | device_info = resp.json() |
| 228 | if device_info['admin_status'] == 'PREPROVISIONED': |
| 229 | return True |
| 230 | return False |