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 | e723209 | 2017-09-07 18:01:33 -0700 | [diff] [blame] | 29 | standalone_services = ('envoy', '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 | e723209 | 2017-09-07 18:01:33 -0700 | [diff] [blame] | 214 | REST_PORT = 8882 |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 215 | HOST = '172.17.0.1' |
A R Karthick | cf1a5d3 | 2017-10-05 16:04:43 -0700 | [diff] [blame] | 216 | ONOS_APPS = ('org.onosproject.hostprovider', '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 | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 221 | def __init__(self, host = HOST, rest_port = REST_PORT, uplink_vlan_map = UPLINK_VLAN_MAP, |
| 222 | uplink_vlan_start = UPLINK_VLAN_START): |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 223 | self.host = host |
| 224 | self.rest_port = rest_port |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 225 | self.rest_url = 'http://{}:{}/api/v1/local'.format(host, rest_port) |
| 226 | if rest_port == 8882: |
| 227 | self.rest_url = 'http://{}:{}/api/v1'.format(host, rest_port) |
A R Karthick | e723209 | 2017-09-07 18:01:33 -0700 | [diff] [blame] | 228 | #self.ADMIN_STATE = 'adminState' |
| 229 | #self.OPER_STATUS = 'operStatus' |
| 230 | #self.CONNECT_STATUS = 'connectStatus' |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 231 | self.uplink_vlan_map = uplink_vlan_map |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 232 | VolthaCtrl.UPLINK_VLAN_START = uplink_vlan_start |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 233 | self.switches = [] |
| 234 | self.switch_map = {} |
| 235 | |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 236 | def config(self, fake = False, driver_configured = False): |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 237 | devices = OnosCtrl.get_devices() |
| 238 | if not devices: |
| 239 | return self.switch_map |
| 240 | voltha_devices = filter(lambda d: not d['mfr'].startswith('Nicira'), devices) |
| 241 | self.switches = voltha_devices |
| 242 | device_config = { 'devices' : { } } |
| 243 | device_id = None |
| 244 | for device in voltha_devices: |
| 245 | device_id = device['id'] |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 246 | serial = device['serial'] |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 247 | ports = OnosCtrl.get_ports_device(device_id) |
| 248 | nni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('nni'), ports) |
| 249 | uni_ports = filter(lambda p: p['isEnabled'] and 'annotations' in p and p['annotations']['portName'].startswith('uni'), ports) |
| 250 | if device_id not in self.uplink_vlan_map: |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 251 | uplink_vlan = VolthaCtrl.UPLINK_VLAN_START |
| 252 | VolthaCtrl.UPLINK_VLAN_START += 1 |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 253 | self.uplink_vlan_map[device_id] = uplink_vlan |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 254 | log.info('Voltha device %s not in map. Using uplink vlan %d' %(device_id, uplink_vlan)) |
| 255 | else: |
| 256 | uplink_vlan = self.uplink_vlan_map[device_id] |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 257 | if not nni_ports: |
| 258 | log.info('Voltha device %s has no NNI ports' %device_id) |
| 259 | if fake is True: |
| 260 | log.info('Faking NNI port 0') |
| 261 | nni_ports = [ {'port': '0'} ] |
| 262 | else: |
| 263 | log.info('Skip configuring device %s' %device_id) |
| 264 | continue |
| 265 | if not uni_ports: |
| 266 | log.info('Voltha device %s has no UNI ports' %device_id) |
| 267 | if fake is True: |
| 268 | log.info('Faking UNI port 252') |
| 269 | uni_ports = [ {'port': '252'} ] |
| 270 | else: |
| 271 | log.info('Skip configuring device %s' %device_id) |
| 272 | continue |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 273 | onu_ports = map(lambda uni: uni['port'], uni_ports) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 274 | onu_names = map(lambda uni: uni['annotations']['portName'], uni_ports) |
| 275 | onu_macs = map(lambda uni: uni['annotations']['portMac'], uni_ports) |
| 276 | self.switch_map[device_id] = dict(uplink_vlan = uplink_vlan, |
| 277 | serial = serial, |
| 278 | ports = onu_ports, |
| 279 | names = onu_names, |
| 280 | macs = onu_macs) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 281 | device_config['devices'][device_id] = {} |
| 282 | device_config['devices'][device_id]['basic'] = dict(driver='pmc-olt') |
| 283 | device_config['devices'][device_id]['accessDevice'] = dict(uplink=nni_ports[0]['port'], |
| 284 | vlan = uplink_vlan, |
A R Karthick | 4eb90dd | 2017-09-01 13:46:30 -0700 | [diff] [blame] | 285 | defaultVlan=str(onu_ports[0]) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 286 | ) |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 287 | if device_id and driver_configured is False: |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 288 | #toggle drivers/openflow base before reconfiguring the driver and olt config data |
| 289 | OnosCtrl('org.onosproject.drivers').deactivate() |
| 290 | OnosCtrl('org.onosproject.openflow-base').deactivate() |
| 291 | OnosCtrl.config(device_config) |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 292 | time.sleep(10) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 293 | OnosCtrl('org.onosproject.drivers').activate() |
| 294 | OnosCtrl('org.onosproject.openflow-base').activate() |
A R Karthick | 4eb90dd | 2017-09-01 13:46:30 -0700 | [diff] [blame] | 295 | time.sleep(10) |
A R Karthick | 6e70e14 | 2017-07-28 15:25:38 -0700 | [diff] [blame] | 296 | log.info('Reactivating CORD and ONOS apps') |
| 297 | Onos.activate_cord_apps(deactivate = True) |
| 298 | Onos.activate_apps(self.ONOS_APPS, deactivate = True) |
A.R Karthick | 4c4d049 | 2017-05-26 19:23:05 -0700 | [diff] [blame] | 299 | |
| 300 | return self.switch_map |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 301 | |
| 302 | def get_devices(self): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 303 | url = '{}/devices'.format(self.rest_url) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 304 | resp = requests.get(url) |
| 305 | if resp.ok is not True or resp.status_code != 200: |
| 306 | return None |
| 307 | return resp.json() |
| 308 | |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 309 | def enable_device(self, olt_type, olt_mac = None, address = None): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 310 | url = '{}/devices'.format(self.rest_url) |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 311 | if olt_mac is None and address is None: |
| 312 | log.error('Either olt mac or address needs to be specified') |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 313 | return None, False |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 314 | if olt_mac is not None: |
| 315 | device_config = { 'type' : olt_type, 'mac_address' : olt_mac } |
| 316 | else: |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 317 | if len(address.split(':')) > 1: |
| 318 | device_config = { 'type' : olt_type, 'host_and_port' : address } |
| 319 | else: |
| 320 | device_config = { 'type' : olt_type, 'ipv4_address' : address } |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 321 | #pre-provision |
A.R Karthick | 8b9c5f1 | 2017-05-30 17:47:08 -0700 | [diff] [blame] | 322 | if olt_mac is not None: |
| 323 | log.info('Pre-provisioning %s with mac %s' %(olt_type, olt_mac)) |
| 324 | else: |
A R Karthick | aa6475d | 2017-08-15 17:24:36 -0700 | [diff] [blame] | 325 | log.info('Pre-provisioning %s with address %s' %(olt_type, address)) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 326 | resp = requests.post(url, data = json.dumps(device_config)) |
| 327 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 328 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 329 | device_id = resp.json()['id'] |
| 330 | log.info('Enabling device %s' %(device_id)) |
| 331 | enable_url = '{}/{}/enable'.format(url, device_id) |
| 332 | resp = requests.post(enable_url) |
| 333 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 334 | return None, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 335 | #get operational status |
A R Karthick | 090631b | 2017-07-25 16:05:05 -0700 | [diff] [blame] | 336 | time.sleep(10) |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 337 | log.info('Checking operational status for device %s' %(device_id)) |
| 338 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 339 | if resp.ok is not True or resp.status_code != 200: |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 340 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 341 | device_info = resp.json() |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 342 | if device_info[self.OPER_STATUS] != 'ACTIVE' or \ |
| 343 | device_info[self.ADMIN_STATE] != 'ENABLED' or \ |
| 344 | device_info[self.CONNECT_STATUS] != 'REACHABLE': |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 345 | return device_id, False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 346 | |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 347 | return device_id, True |
| 348 | |
| 349 | def disable_device(self, device_id, delete = True): |
| 350 | log.info('Disabling device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 351 | disable_url = '{}/devices/{}/disable'.format(self.rest_url, device_id) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 352 | resp = requests.post(disable_url) |
| 353 | if resp.ok is not True or resp.status_code != 200: |
| 354 | return False |
| 355 | if delete is True: |
A R Karthick | d69d570 | 2017-07-25 11:31:47 -0700 | [diff] [blame] | 356 | #rest for disable completion |
| 357 | time.sleep(10) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 358 | log.info('Deleting device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 359 | delete_url = '{}/devices/{}/delete'.format(self.rest_url, device_id) |
A.R Karthick | 8a507cf | 2017-06-02 18:44:49 -0700 | [diff] [blame] | 360 | resp = requests.delete(delete_url) |
| 361 | if resp.status_code not in [204, 202, 200]: |
| 362 | return False |
A R Karthick | 35495c3 | 2017-05-11 14:58:32 -0700 | [diff] [blame] | 363 | return True |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 364 | |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 365 | def restart_device(self, device_id): |
| 366 | log.info('Restarting olt or onu device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 367 | disable_url = '{}/devices/{}/restart'.format(self.rest_url, device_id) |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 368 | resp = requests.post(disable_url) |
| 369 | if resp.ok is not True or resp.status_code != 200: |
| 370 | return False |
| 371 | return True |
| 372 | |
| 373 | def pause_device(self, device_id): |
| 374 | log.info('Restarting olt or onu device %s' %(device_id)) |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 375 | disable_url = '{}/devices/{}/pause'.format(self.rest_url, device_id) |
Thangavelu K S | e6c77c7 | 2017-06-23 21:28:48 +0000 | [diff] [blame] | 376 | resp = requests.post(disable_url) |
| 377 | if resp.ok is not True or resp.status_code != 200: |
| 378 | return False |
| 379 | return True |
| 380 | |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 381 | def get_operational_status(self, device_id): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 382 | url = '{}/devices'.format(self.rest_url) |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 383 | log.info('Checking operational status for device %s' %(device_id)) |
| 384 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 385 | if resp.ok is not True or resp.status_code != 200: |
| 386 | return False |
| 387 | device_info = resp.json() |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 388 | if device_info[self.OPER_STATUS] != 'ACTIVE' or \ |
| 389 | device_info[self.ADMIN_STATE] != 'ENABLED' or \ |
| 390 | device_info[self.CONNECT_STATUS] != 'REACHABLE': |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 391 | return False |
| 392 | return True |
| 393 | |
| 394 | def check_preprovision_status(self, device_id): |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 395 | url = '{}/devices'.format(self.rest_url) |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 396 | log.info('Check if device %s is in Preprovisioning state'%(device_id)) |
| 397 | resp = requests.get('{}/{}'.format(url, device_id)) |
| 398 | if resp.ok is not True or resp.status_code != 200: |
| 399 | return False |
| 400 | device_info = resp.json() |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 401 | if device_info[self.ADMIN_STATE] == 'PREPROVISIONED': |
Chetan Gaonker | 3620a11 | 2017-05-23 06:10:15 +0000 | [diff] [blame] | 402 | return True |
| 403 | return False |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 404 | |
| 405 | def get_olt_app(): |
| 406 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 407 | version = Onos.getVersion() |
| 408 | major = int(version.split('.')[0]) |
| 409 | minor = int(version.split('.')[1]) |
| 410 | olt_app_version = '1.2-SNAPSHOT' |
| 411 | if major > 1: |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 412 | olt_app_version = '3.0-SNAPSHOT' |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 413 | elif major == 1: |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 414 | if minor >= 10: |
| 415 | olt_app_version = '3.0-SNAPSHOT' |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 416 | elif minor <= 8: |
| 417 | olt_app_version = '1.1-SNAPSHOT' |
| 418 | olt_app_file = os.path.join(our_path, '..', 'apps/olt-app-{}.oar'.format(olt_app_version)) |
| 419 | return olt_app_file |
| 420 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 421 | 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] | 422 | 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] | 423 | uplink_vlan_map = VolthaCtrl.UPLINK_VLAN_MAP, |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 424 | uplink_vlan_start = VolthaCtrl.UPLINK_VLAN_START, |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 425 | config_fake = False, olt_app = None, teardown = True): |
| 426 | devices = OnosCtrl.get_devices() |
| 427 | olt_devices = filter(lambda d: not d['mfr'].startswith('Nicira') and d['driver'] == 'pmc-olt', devices) |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 428 | voltha = VolthaCtrl(host, rest_port = rest_port, |
| 429 | uplink_vlan_map = uplink_vlan_map, |
| 430 | uplink_vlan_start = uplink_vlan_start) |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 431 | voltha_devices = voltha.get_devices() |
| 432 | if voltha_devices: |
| 433 | voltha_device_ids = filter(lambda d: d[voltha.OPER_STATUS] == 'ACTIVE' and d[voltha.ADMIN_STATE] == 'ENABLED', |
| 434 | voltha_devices['items']) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 435 | else: |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 436 | voltha_device_ids = [] |
| 437 | |
| 438 | driver_configured = len(olt_devices) > 0 and len(voltha_device_ids) > 0 |
| 439 | if olt_type.startswith('ponsim'): |
| 440 | if driver_configured: |
| 441 | device_id, status = voltha_device_ids[0], True |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 442 | else: |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 443 | ponsim_address = '{}:50060'.format(ponsim_host) |
| 444 | log.info('Enabling ponsim olt') |
| 445 | device_id, status = voltha.enable_device(olt_type, address = ponsim_address) |
| 446 | else: |
| 447 | if driver_configured: |
| 448 | device_id, status = voltha_device_ids[0], True |
| 449 | else: |
A R Karthick | cafe4d8 | 2017-09-14 12:29:21 -0700 | [diff] [blame] | 450 | if olt_type.startswith('maple') or olt_ip: |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 451 | if olt_ip: |
| 452 | log.info('Enabling %s' %olt_type) |
| 453 | device_id, status = voltha.enable_device(olt_type, address = olt_ip) |
| 454 | else: |
A R Karthick | cafe4d8 | 2017-09-14 12:29:21 -0700 | [diff] [blame] | 455 | log.info('OLT IP needs to be specified for %s' %olt_type) |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 456 | else: |
| 457 | log.info('Enabling OLT instance for %s with mac %s' %(olt_type, olt_mac)) |
| 458 | device_id, status = voltha.enable_device(olt_type, olt_mac) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 459 | |
| 460 | if device_id is None or status is False: |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 461 | if device_id: |
| 462 | voltha.disable_device(device_id) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 463 | return None |
| 464 | |
| 465 | switch_map = None |
| 466 | olt_installed = False |
| 467 | if olt_app is None: |
| 468 | olt_app = get_olt_app() |
| 469 | try: |
A R Karthick | 090631b | 2017-07-25 16:05:05 -0700 | [diff] [blame] | 470 | time.sleep(5) |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 471 | switch_map = voltha.config(fake = config_fake, driver_configured = driver_configured) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 472 | if switch_map is None: |
| 473 | voltha.disable_device(device_id) |
| 474 | return None |
| 475 | log.info('Installing OLT app %s' %olt_app) |
| 476 | OnosCtrl.install_app(olt_app) |
| 477 | olt_installed = True |
| 478 | time.sleep(5) |
A R Karthick | 2178298 | 2017-10-02 10:49:22 -0700 | [diff] [blame] | 479 | OnosCtrl.config_olt_component() |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 480 | return voltha, device_id, switch_map, driver_configured |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 481 | except: |
| 482 | voltha.disable_device(device_id) |
| 483 | time.sleep(10) |
| 484 | if olt_installed is True: |
| 485 | log.info('Uninstalling OLT app %s' %olt_app) |
| 486 | OnosCtrl.uninstall_app(olt_app) |
| 487 | |
| 488 | return None |
| 489 | |
| 490 | def voltha_teardown(voltha_ctrl, device_id, switch_map, olt_app = None): |
A R Karthick | 6708f56 | 2017-09-05 12:15:31 -0700 | [diff] [blame] | 491 | if voltha_ctrl: |
| 492 | voltha_ctrl.disable_device(device_id) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 493 | time.sleep(10) |
| 494 | if olt_app is None: |
| 495 | olt_app = get_olt_app() |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 496 | log.info('Uninstalling OLT app %s' %olt_app) |
A R Karthick | bf1e4b0 | 2017-07-11 20:17:14 -0700 | [diff] [blame] | 497 | OnosCtrl.uninstall_app(olt_app) |