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 |
| 6 | from CordTestUtils import log_test as log, getstatusoutput |
| 7 | from CordContainer import Container |
| 8 | |
| 9 | class VolthaService(object): |
| 10 | services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd') |
| 11 | compose_file = 'docker-compose-system-test.yml' |
| 12 | service_map = {} |
| 13 | |
| 14 | def __init__(self, voltha_loc, controller, interface = 'eth0'): |
| 15 | if not os.access(voltha_loc, os.F_OK): |
| 16 | raise Exception('Voltha location %s not found' %voltha_loc) |
| 17 | compose_file_loc = os.path.join(voltha_loc, 'compose', self.compose_file) |
| 18 | if not os.access(compose_file_loc, os.F_OK): |
| 19 | raise Exception('Voltha compose file %s not found' %compose_file_loc) |
| 20 | self.voltha_loc = voltha_loc |
| 21 | self.controller = controller |
| 22 | self.interface = interface |
| 23 | self.compose_file_loc = compose_file_loc |
| 24 | |
| 25 | def start(self): |
| 26 | start_cmd = 'docker-compose -f {} up -d {} {} {} {} {}'.format(self.compose_file_loc, |
| 27 | *self.services) |
| 28 | ret = os.system(start_cmd) |
| 29 | if ret != 0: |
| 30 | raise Exception('Failed to start voltha services. Failed with code %d' %ret) |
| 31 | |
| 32 | for service in self.services: |
| 33 | name = 'compose_{}_1'.format(service) |
| 34 | network = 'compose_default' |
| 35 | cnt = Container(name, name) |
| 36 | ip = cnt.ip(network = network) |
| 37 | if not ip: |
| 38 | raise Exception('IP not found for container %s' %name) |
| 39 | print('IP %s for service %s' %(ip, service)) |
| 40 | self.service_map[service] = dict(name = name, network = network, ip = ip) |
| 41 | |
| 42 | #first start chameleon |
| 43 | chameleon_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 44 | nohup python chameleon/main.py -v --consul=localhost:8500 \ |
| 45 | --fluentd={}:24224 --grpc-endpoint=localhost:50555 \ |
| 46 | >/tmp/chameleon.log 2>&1 &'".format(self.voltha_loc, |
| 47 | self.service_map['fluentd']['ip']) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 48 | if not self.service_running('python chameleon/main.py'): |
| 49 | ret = os.system(chameleon_start_cmd) |
| 50 | if ret != 0: |
| 51 | raise Exception('VOLTHA chameleon service not started. Failed with return code %d' %ret) |
| 52 | else: |
| 53 | print('Chameleon voltha sevice is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 54 | |
| 55 | #now start voltha and ofagent |
| 56 | voltha_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 57 | nohup python voltha/main.py -v --consul=localhost:8500 --kafka={}:9092 -I {} \ |
| 58 | --fluentd={}:24224 --rest-port=8880 --grpc-port=50555 \ |
| 59 | >/tmp/voltha.log 2>&1 &'".format(self.voltha_loc, |
| 60 | self.service_map['kafka']['ip'], |
| 61 | self.interface, |
| 62 | self.service_map['fluentd']['ip']) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 63 | if not self.service_running('python voltha/main.py'): |
| 64 | ret = os.system(voltha_start_cmd) |
| 65 | if ret != 0: |
| 66 | raise Exception('Failed to start VOLTHA. Return code %d' %ret) |
| 67 | else: |
| 68 | print('VOLTHA core is already running. Skipped start') |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 69 | |
| 70 | ofagent_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
| 71 | nohup python ofagent/main.py -v --consul=localhost:8500 \ |
| 72 | --fluentd={}:24224 --controller={}:6653 --grpc-endpoint=localhost:50555 \ |
| 73 | >/tmp/ofagent.log 2>&1 &'".format(self.voltha_loc, |
| 74 | self.service_map['fluentd']['ip'], |
| 75 | self.controller) |
A.R Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 76 | if not self.service_running('python ofagent/main.py'): |
| 77 | ret = os.system(ofagent_start_cmd) |
| 78 | if ret != 0: |
| 79 | raise Exception('VOLTHA ofagent not started. Failed with return code %d' %ret) |
| 80 | else: |
| 81 | print('VOLTHA ofagent is already running. Skipped start') |
| 82 | |
| 83 | def service_running(self, pattern): |
| 84 | st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 85 | return True if st == 0 else False |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 86 | |
| 87 | def kill_service(self, pattern): |
| 88 | st, output = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 89 | if st == 0 and output: |
| 90 | pids = output.strip().splitlines() |
| 91 | for pid in pids: |
| 92 | try: |
| 93 | os.kill(int(pid), signal.SIGKILL) |
| 94 | except: |
| 95 | pass |
| 96 | |
| 97 | def stop(self): |
| 98 | self.kill_service('python voltha/main.py') |
| 99 | self.kill_service('python ofagent/main.py') |
| 100 | self.kill_service('python chameleon/main.py') |
| 101 | service_stop_cmd = 'docker-compose -f {} down'.format(self.compose_file_loc) |
| 102 | os.system(service_stop_cmd) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 103 | |
| 104 | class VolthaCtrl(object): |
| 105 | |
| 106 | def __init__(self, host, rest_port = 8881): |
| 107 | self.host = host |
| 108 | self.rest_port = rest_port |
| 109 | self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port) |
| 110 | |
| 111 | def get_devices(self): |
| 112 | url = '{}/local/devices'.format(self.rest_url) |
| 113 | resp = requests.get(url) |
| 114 | if resp.ok is not True or resp.status_code != 200: |
| 115 | return None |
| 116 | return resp.json() |
| 117 | |
| 118 | def enable_device(self, olt_type, olt_mac): |
| 119 | url = '{}/local/devices'.format(self.rest_url) |
| 120 | device_config = { 'type' : olt_type, 'mac_address' : olt_mac } |
| 121 | #pre-provision |
| 122 | log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac)) |
| 123 | resp = requests.post(url, data = json.dumps(device_config)) |
| 124 | if resp.ok is not True or resp.status_code != 200: |
| 125 | return False |
| 126 | device_id = resp.json()['id'] |
| 127 | log.info('Enabling device %s' %(device_id)) |
| 128 | enable_url = '{}/{}/enable'.format(url, device_id) |
| 129 | resp = requests.post(enable_url) |
| 130 | if resp.ok is not True or resp.status_code != 200: |
| 131 | return False |
| 132 | #get operational status |
| 133 | time.sleep(5) |
| 134 | log.info('Checking operational status for device %s' %(device_id)) |
| 135 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 136 | if resp.ok is not True or resp.status_code != 200: |
| 137 | return False |
| 138 | device_info = resp.json() |
| 139 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 140 | device_info['admin_state'] != 'ENABLED' or \ |
| 141 | device_info['connect_status'] != 'REACHABLE': |
| 142 | return False |
| 143 | |
| 144 | return True |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 145 | |
| 146 | def get_operational_status(self, device_id): |
| 147 | url = '{}/local/devices'.format(self.rest_url) |
| 148 | log.info('Checking operational status for device %s' %(device_id)) |
| 149 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 150 | if resp.ok is not True or resp.status_code != 200: |
| 151 | return False |
| 152 | device_info = resp.json() |
| 153 | if device_info['oper_status'] != 'ACTIVE' or \ |
| 154 | device_info['admin_state'] != 'ENABLED' or \ |
| 155 | device_info['connect_status'] != 'REACHABLE': |
| 156 | return False |
| 157 | return True |
| 158 | |
| 159 | def check_preprovision_status(self, device_id): |
| 160 | url = '{}/local/devices'.format(self.rest_url) |
| 161 | log.info('Check if device %s is in Preprovisioning state'%(device_id)) |
| 162 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 163 | if resp.ok is not True or resp.status_code != 200: |
| 164 | return False |
| 165 | device_info = resp.json() |
| 166 | if device_info['admin_status'] == 'PREPROVISIONED': |
| 167 | return True |
| 168 | return False |