Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 17 | import requests |
| 18 | import json |
| 19 | import time |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 20 | import os |
| 21 | import signal |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 22 | from CordTestUtils import log_test as log, getstatusoutput, get_controller |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 23 | from CordContainer import Container, Onos |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 24 | from OnosCtrl import OnosCtrl |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 25 | from OltConfig import OltConfig |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 26 | |
| 27 | class VolthaService(object): |
| 28 | services = ('consul', 'kafka', 'zookeeper', 'registrator', 'fluentd') |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 29 | standalone_services = ('voltha', 'ofagent', 'vcli',) |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 30 | compose_file = 'docker-compose-system-test.yml' |
| 31 | service_map = {} |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 32 | 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 Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 38 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 39 | def __init__(self, voltha_loc, controller, interface = 'eth0', olt_config = None, container_mode = False): |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 40 | 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 Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 49 | VolthaService.CONTAINER_MODE = container_mode |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 50 | 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 Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 56 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 57 | 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 Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 64 | 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 Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 68 | for service in services: |
| 69 | name = '{}_{}_1'.format(self.PROJECT, service) |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 70 | cnt = Container(name, name) |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 71 | ip = cnt.ip(network = self.NETWORK) |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 72 | if not ip: |
| 73 | raise Exception('IP not found for container %s' %name) |
| 74 | print('IP %s for service %s' %(ip, service)) |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 75 | self.service_map[service] = dict(name = name, network = self.NETWORK, ip = ip) |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 76 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 77 | 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 Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 93 | time.sleep(10) |
A R Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 94 | 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 Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 101 | else: |
A R Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 102 | #first start chameleon on the host as its only the reliable way for REST |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 103 | 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 Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 107 | 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 Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 115 | |
A R Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 116 | if self.CONTAINER_MODE is False: |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 117 | #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 Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 141 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 142 | 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 Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 161 | |
A.R Karthick | 12e08c3 | 2017-05-30 17:09:26 -0700 | [diff] [blame] | 162 | ponsim_start_cmd = "cd {} && sh -c '. ./env.sh && \ |
A.R Karthick | 4f58384 | 2017-06-09 17:15:47 -0700 | [diff] [blame] | 163 | 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] | 164 | 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 Karthick | c2697a1 | 2017-05-24 14:01:15 -0700 | [diff] [blame] | 172 | def service_running(self, pattern): |
| 173 | st, _ = getstatusoutput('pgrep -f "{}"'.format(pattern)) |
| 174 | return True if st == 0 else False |
A.R Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 175 | |
| 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 Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 187 | if self.CONTAINER_MODE is False: |
| 188 | self.kill_service('python voltha/main.py') |
| 189 | self.kill_service('python ofagent/main.py') |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 190 | self.kill_service('python ponsim/main.py') |
A R Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 191 | self.kill_service('python chameleon/main.py') |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 192 | 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 Karthick | 57fa937 | 2017-05-24 12:47:03 -0700 | [diff] [blame] | 195 | os.system(service_stop_cmd) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 196 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 197 | @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 Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 211 | class VolthaCtrl(object): |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 212 | UPLINK_VLAN_START = 333 |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 213 | UPLINK_VLAN_MAP = { 'of:0000000000000001' : '222' } |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 214 | REST_PORT = 8881 |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 215 | HOST = '172.17.0.1' |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 216 | ONOS_APPS = ('org.onosproject.dhcp', 'org.onosproject.dhcp-relay', 'org.ciena.cordigmp') |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 217 | ADMIN_STATE = 'admin_state' |
| 218 | OPER_STATUS = 'oper_status' |
| 219 | CONNECT_STATUS = 'connect_status' |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 220 | |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 221 | 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] | 222 | self.host = host |
| 223 | self.rest_port = rest_port |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 224 | 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 Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 230 | self.uplink_vlan_map = uplink_vlan_map |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 231 | VolthaCtrl.UPLINK_VLAN_START = uplink_vlan_start |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 232 | 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 Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 249 | 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 Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 254 | 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 Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 270 | 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 Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 283 | time.sleep(10) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 284 | OnosCtrl('org.onosproject.drivers').activate() |
| 285 | OnosCtrl('org.onosproject.openflow-base').activate() |
| 286 | time.sleep(5) |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 287 | 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 Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 290 | |
| 291 | return self.switch_map |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 292 | |
| 293 | def get_devices(self): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 294 | url = '{}/devices'.format(self.rest_url) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 295 | 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 Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 300 | def enable_device(self, olt_type, olt_mac = None, address = None): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 301 | url = '{}/devices'.format(self.rest_url) |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 302 | if olt_mac is None and address is None: |
| 303 | log.error('Either olt mac or address needs to be specified') |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 304 | return None, False |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 305 | if olt_mac is not None: |
| 306 | device_config = { 'type' : olt_type, 'mac_address' : olt_mac } |
| 307 | else: |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 308 | 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 Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 312 | #pre-provision |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 313 | if olt_mac is not None: |
| 314 | log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac)) |
| 315 | else: |
A R Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 316 | log.info('Pre-provisioning %s with address %s' %(olt_type, address)) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 317 | resp = requests.post(url, data = json.dumps(device_config)) |
| 318 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 319 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 320 | 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 Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 325 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 326 | #get operational status |
A R Karthick | 090631b | 2017-07-25 16:05:05 -0700 | [diff] [blame] | 327 | time.sleep(10) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 328 | 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 Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 331 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 332 | device_info = resp.json() |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 333 | 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 Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 336 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 337 | |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 338 | return device_id, True |
| 339 | |
| 340 | def disable_device(self, device_id, delete = True): |
| 341 | log.info('Disabling device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 342 | disable_url = '{}/devices/{}/disable'.format(self.rest_url, device_id) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 343 | 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 Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 347 | #rest for disable completion |
| 348 | time.sleep(10) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 349 | log.info('Deleting device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 350 | delete_url = '{}/devices/{}/delete'.format(self.rest_url, device_id) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 351 | resp = requests.delete(delete_url) |
| 352 | if resp.status_code not in [204, 202, 200]: |
| 353 | return False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 354 | return True |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 355 | |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 356 | def restart_device(self, device_id): |
| 357 | log.info('Restarting olt or onu device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 358 | disable_url = '{}/devices/{}/restart'.format(self.rest_url, device_id) |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 359 | 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 Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 366 | disable_url = '{}/devices/{}/pause'.format(self.rest_url, device_id) |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 367 | 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 Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 372 | def get_operational_status(self, device_id): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 373 | url = '{}/devices'.format(self.rest_url) |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 374 | 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 Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 379 | if device_info[self.OPER_STATUS] != 'ACTIVE' or \ |
| 380 | device_info[self.ADMIN_STATE] != 'ENABLED' or \ |
| 381 | device_info[self.CONNECT_STATUS] != 'REACHABLE': |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 382 | return False |
| 383 | return True |
| 384 | |
| 385 | def check_preprovision_status(self, device_id): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 386 | url = '{}/devices'.format(self.rest_url) |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 387 | 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 Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 392 | if device_info[self.ADMIN_STATE] == 'PREPROVISIONED': |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 393 | return True |
| 394 | return False |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 395 | |
| 396 | def 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 Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 412 | def voltha_setup(host = '172.17.0.1', ponsim_host = VolthaService.PONSIM_HOST, olt_ip = None, rest_port = VolthaCtrl.REST_PORT, |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 413 | 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] | 414 | uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP, |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 415 | uplink_vlan_start = VolthaCtrl.UPLINK_VLAN_START, |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 416 | config_fake = False, olt_app = None): |
| 417 | |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 418 | voltha = VolthaCtrl(host, rest_port = rest_port, |
| 419 | uplink_vlan_map = uplink_vlan_map, |
| 420 | uplink_vlan_start = uplink_vlan_start) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 421 | if olt_type.startswith('ponsim'): |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 422 | ponsim_address = '{}:50060'.format(ponsim_host) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 423 | log.info('Enabling ponsim olt') |
| 424 | device_id, status = voltha.enable_device(olt_type, address = ponsim_address) |
| 425 | else: |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 426 | 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 Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 435 | |
| 436 | if device_id is None or status is False: |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 437 | if device_id: |
| 438 | voltha.disable_device(device_id) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 439 | 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 Karthick | 090631b | 2017-07-25 16:05:05 -0700 | [diff] [blame] | 446 | time.sleep(5) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 447 | 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 | |
| 465 | def 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 Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 470 | log.info('Uninstalling OLT app %s' %olt_app) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 471 | OnosCtrl.uninstall_app(olt_app) |