Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 15 | from time import sleep |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 17 | import requests |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 18 | from multistructlog import create_logger |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 19 | from requests.auth import HTTPBasicAuth |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 20 | from xossynchronizer.steps.syncstep import SyncStep, DeferredException |
Matteo Scandolo | 2b4c847 | 2019-06-26 18:06:47 -0700 | [diff] [blame] | 21 | from xossynchronizer.modelaccessor import OLTDevice, TechnologyProfile, model_accessor |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 22 | from xosconfig import Config |
| 23 | |
| 24 | import os, sys |
| 25 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 26 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 27 | from helpers import Helpers |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 28 | |
| 29 | log = create_logger(Config().get('logging')) |
| 30 | |
| 31 | class SyncOLTDevice(SyncStep): |
| 32 | provides = [OLTDevice] |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 33 | observes = OLTDevice |
| 34 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 35 | max_attempt = 120 # we give 10 minutes to the OLT to activate |
| 36 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 37 | @staticmethod |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 38 | def get_ids_from_logical_device(o): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 39 | voltha = Helpers.get_voltha_info(o.volt_service) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 40 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 41 | request = requests.get("%s:%d/api/v1/logical_devices" % (voltha['url'], voltha['port'])) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 42 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 43 | if request.status_code != 200: |
| 44 | raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 45 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 46 | response = request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 47 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 48 | for ld in response["items"]: |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 49 | if ld["root_device_id"] == o.device_id: |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 50 | o.of_id = ld["id"] |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 51 | o.dp_id = "of:%s" % (Helpers.datapath_id_to_hex(ld["datapath_id"])) # Convert to hex |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 52 | return o |
| 53 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 54 | raise Exception("Can't find a logical_device for OLT device id: %s" % o.device_id) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 56 | def pre_provision_olt_device(self, model): |
| 57 | log.info("Pre-provisioning OLT device in VOLTHA", object=str(model), **model.tologdict()) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 59 | voltha = Helpers.get_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 61 | data = { |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 62 | "type": model.device_type |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 65 | if hasattr(model, "host") and hasattr(model, "port"): |
| 66 | data["host_and_port"] = "%s:%s" % (model.host, model.port) |
| 67 | elif hasattr(model, "mac_address"): |
| 68 | data["mac_address"] = model.mac_address |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 69 | |
| 70 | log.info("Pushing OLT to Voltha", data=data) |
| 71 | |
| 72 | request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data) |
| 73 | |
| 74 | if request.status_code != 200: |
| 75 | raise Exception("Failed to add OLT device: %s" % request.text) |
| 76 | |
| 77 | log.info("Add device response", text=request.text) |
| 78 | |
| 79 | res = request.json() |
| 80 | |
| 81 | log.info("Add device json res", res=res) |
| 82 | |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 83 | # TODO(smbaker): Potential partial failure. If device is created in Voltha but synchronizer crashes before the |
| 84 | # model is saved, then synchronizer will continue to try to preprovision and fail due to preexisting |
| 85 | # device. |
| 86 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 87 | if not res['id']: |
| 88 | raise Exception( |
| 89 | 'VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA') |
| 90 | else: |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 91 | model.device_id = res['id'] |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame] | 92 | |
| 93 | # Only update the serial number if it is not already populated. See comments in similar code in the |
| 94 | # pull step. Let the pull step handle emitting any error message if the serial numbers differ. |
| 95 | if res['serial_number'] and (not model.serial_number): |
| 96 | log.info("Sync step learned olt serial number from voltha", |
| 97 | model_serial_number=model.serial_number, |
| 98 | voltha_serial_number=res['serial_number'], |
| 99 | olt_id=model.id) |
| 100 | model.serial_number = res['serial_number'] |
| 101 | |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 102 | model.save_changed_fields() |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 103 | |
| 104 | def activate_olt(self, model): |
| 105 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 106 | attempted = 0 |
| 107 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 108 | voltha = Helpers.get_voltha_info(model.volt_service) |
| 109 | |
| 110 | # Enable device |
| 111 | request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], model.device_id)) |
| 112 | |
| 113 | if request.status_code != 200: |
| 114 | raise Exception("Failed to enable OLT device: %s" % request.text) |
| 115 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 116 | model.backend_status = "Waiting for device to be activated" |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 117 | model.save_changed_fields(always_update_timestamp=False) # we don't want to kickoff a new loop |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 118 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 119 | # Read state |
| 120 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json() |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 121 | while request['oper_status'] == "ACTIVATING" and attempted < self.max_attempt: |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 122 | log.info("Waiting for OLT device %s (%s) to activate" % (model.name, model.device_id)) |
| 123 | sleep(5) |
| 124 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json() |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 125 | attempted = attempted + 1 |
| 126 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 127 | model.oper_status = request['oper_status'] |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame] | 128 | |
| 129 | # Only update the serial number if it is not already populated. See comments in similar code in the |
| 130 | # pull step. Let the pull step handle emitting any error message if the serial numbers differ. |
| 131 | if request['serial_number'] and (not model.serial_number): |
| 132 | log.info("Sync step learned olt serial number from voltha", |
| 133 | model_serial_number=model.serial_number, |
| 134 | voltha_serial_number=request['serial_number'], |
| 135 | olt_id=model.id) |
| 136 | model.serial_number = request['serial_number'] |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 137 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 138 | if model.oper_status != "ACTIVE": |
| 139 | raise Exception("It was not possible to activate OLTDevice with id %s" % model.id) |
| 140 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 141 | # Find the of_id of the device |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 142 | self.get_ids_from_logical_device(model) |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 143 | model.save_changed_fields() |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 144 | |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 145 | def deactivate_olt(self, model): |
| 146 | voltha = Helpers.get_voltha_info(model.volt_service) |
| 147 | |
| 148 | # Disable device |
| 149 | request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], model.device_id)) |
| 150 | |
| 151 | if request.status_code != 200: |
| 152 | raise Exception("Failed to disable OLT device: %s" % request.text) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 153 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 154 | def configure_onos(self, model): |
| 155 | |
| 156 | log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict()) |
| 157 | |
| 158 | onos_voltha = Helpers.get_onos_voltha_info(model.volt_service) |
| 159 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
| 160 | |
| 161 | # Add device info to onos-voltha |
| 162 | data = { |
| 163 | "devices": { |
| 164 | model.dp_id: { |
| 165 | "basic": { |
| 166 | "name": model.name |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | log.info("Calling ONOS", data=data) |
| 173 | |
| 174 | url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port']) |
| 175 | request = requests.post(url, json=data, auth=onos_voltha_basic_auth) |
| 176 | |
| 177 | if request.status_code != 200: |
| 178 | log.error(request.text) |
| 179 | raise Exception("Failed to add OLT device %s into ONOS" % model.name) |
| 180 | else: |
| 181 | try: |
| 182 | print request.json() |
| 183 | except Exception: |
| 184 | print request.text |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 185 | |
Matteo Scandolo | 2b4c847 | 2019-06-26 18:06:47 -0700 | [diff] [blame] | 186 | def wait_for_tp(self, technology): |
| 187 | """ |
| 188 | Check if a technology profile for this technology has been already pushed to ETCD, |
| 189 | if not defer the OLT Provisioning. |
| 190 | :param technology: string - the technology to check for a tech profile |
| 191 | :return: True (or raises DeferredException) |
| 192 | """ |
| 193 | try: |
| 194 | tps = TechnologyProfile.objects.get(technology=technology, backend_code=1) |
| 195 | except IndexError: |
| 196 | raise DeferredException("Waiting for a TechnologyProfile (technology=%s) to be synchronized" % technology) |
| 197 | |
| 198 | return True |
| 199 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 200 | def sync_record(self, model): |
| 201 | log.info("Synching device", object=str(model), **model.tologdict()) |
| 202 | |
Matteo Scandolo | 2b4c847 | 2019-06-26 18:06:47 -0700 | [diff] [blame] | 203 | self.wait_for_tp(model.technology) |
| 204 | |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 205 | if model.admin_state not in ["ENABLED", "DISABLED"]: |
| 206 | raise Exception("OLT Device %s admin_state has invalid value %s" % (model.id, model.admin_state)) |
| 207 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 208 | # If the device has feedback_state is already present in voltha |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 209 | if not model.device_id and not model.oper_status and not model.of_id: |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 210 | log.info("Pushing OLT device to VOLTHA", object=str(model), **model.tologdict()) |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 211 | self.pre_provision_olt_device(model) |
| 212 | model.oper_status = "UNKNOWN" # fall-though to activate OLT |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 213 | else: |
| 214 | log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict()) |
| 215 | |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 216 | # Reconcile admin_state and oper_status, activating or deactivating the OLT as necessary. |
| 217 | |
| 218 | if model.oper_status != "ACTIVE" and model.admin_state == "ENABLED": |
| 219 | self.activate_olt(model) |
| 220 | elif model.oper_status == "ACTIVE" and model.admin_state == "DISABLED": |
| 221 | self.deactivate_olt(model) |
| 222 | |
| 223 | if model.admin_state == "ENABLED": |
| 224 | # If we were not able to reconcile ENABLE/ACTIVE, then throw an exception and do not proceed to onos |
| 225 | # configuration. |
| 226 | if model.oper_status != "ACTIVE": |
| 227 | raise Exception("It was not possible to activate OLTDevice with id %s" % model.id) |
| 228 | |
| 229 | # At this point OLT is enabled and active. Configure ONOS. |
| 230 | self.configure_onos(model) |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 231 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 232 | def delete_record(self, model): |
| 233 | log.info("Deleting OLT device", object=str(model), **model.tologdict()) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 234 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 235 | voltha = Helpers.get_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 236 | |
Matteo Scandolo | 563891c | 2018-08-21 11:56:32 -0700 | [diff] [blame] | 237 | if not model.device_id or model.backend_code == 2: |
| 238 | # NOTE if the device was not synchronized, just remove it from the data model |
| 239 | log.warning("OLTDevice %s has no device_id, it was never saved in VOLTHA" % model.name) |
| 240 | return |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 241 | else: |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 242 | try: |
| 243 | # Disable the OLT device |
| 244 | request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], model.device_id)) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 245 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 246 | if request.status_code != 200: |
| 247 | log.error("Failed to disable OLT device in VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code) |
| 248 | raise Exception("Failed to disable OLT device in VOLTHA") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 249 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 250 | # NOTE [teo] wait some time after the disable to let VOLTHA doing its things |
| 251 | i = 0 |
| 252 | for i in list(reversed(range(10))): |
| 253 | sleep(1) |
| 254 | log.info("Deleting the OLT in %s seconds" % i) |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 255 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 256 | # Delete the OLT device |
| 257 | request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], model.device_id)) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 258 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 259 | if request.status_code != 200: |
| 260 | log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code) |
| 261 | raise Exception("Failed to delete OLT device from VOLTHA") |
| 262 | except requests.ConnectionError: |
| 263 | log.warning("ConnectionError when contacting Voltha in OLT delete step", name=model.name, device_id=model.device_id) |