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 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 15 | from xossynchronizer.pull_steps.pullstep import PullStep |
| 16 | from xossynchronizer.modelaccessor import model_accessor, OLTDevice, VOLTService, PONPort, NNIPort |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 17 | |
| 18 | from xosconfig import Config |
| 19 | from multistructlog import create_logger |
| 20 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 21 | import requests |
| 22 | from requests import ConnectionError |
| 23 | from requests.models import InvalidURL |
| 24 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 25 | import os, sys |
| 26 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 27 | |
| 28 | from helpers import Helpers |
| 29 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 30 | log = create_logger(Config().get('logging')) |
| 31 | |
| 32 | class OLTDevicePullStep(PullStep): |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 33 | def __init__(self, model_accessor): |
| 34 | super(OLTDevicePullStep, self).__init__(model_accessor=model_accessor, observed_model=OLTDevice) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 35 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 36 | @staticmethod |
| 37 | def get_ids_from_logical_device(o): |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 38 | voltha_url = Helpers.get_voltha_info(o.volt_service)['url'] |
| 39 | voltha_port = Helpers.get_voltha_info(o.volt_service)['port'] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 40 | |
Matteo Scandolo | 10928dd | 2018-10-16 17:49:03 -0700 | [diff] [blame] | 41 | r = requests.get("%s:%s/api/v1/logical_devices" % (voltha_url, voltha_port), timeout=1) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 42 | |
| 43 | if r.status_code != 200: |
| 44 | raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text) |
| 45 | |
| 46 | res = r.json() |
| 47 | |
| 48 | for ld in res["items"]: |
| 49 | if ld["root_device_id"] == o.device_id: |
| 50 | o.of_id = ld["id"] |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 51 | o.dp_id = "of:" + Helpers.datapath_id_to_hex(ld["datapath_id"]) # convert to hex |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 52 | |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 53 | # Note: If the device is administratively disabled, then it's likely we won't find a logical device for |
| 54 | # it. Only throw the exception for OLTs that are enabled. |
| 55 | |
| 56 | if not o.of_id and not o.dp_id and o.admin_state == "ENABLED": |
| 57 | raise Exception("Can't find a logical device for device id: %s" % o.device_id) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 58 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 59 | def pull_records(self): |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 60 | log.debug("[OLT pull step] pulling OLT devices from VOLTHA") |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 61 | |
| 62 | try: |
| 63 | self.volt_service = VOLTService.objects.all()[0] |
| 64 | except IndexError: |
| 65 | log.warn('VOLTService not found') |
| 66 | return |
| 67 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 68 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 69 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 70 | |
| 71 | try: |
Matteo Scandolo | 10928dd | 2018-10-16 17:49:03 -0700 | [diff] [blame] | 72 | r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port), timeout=1) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 73 | |
| 74 | if r.status_code != 200: |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 75 | log.debug("[OLT pull step] It was not possible to fetch devices from VOLTHA") |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 76 | |
Vijaykumar Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 77 | # [SEBA-367] Handling blank response received from Voltha, Scenario occurs when voltha api is called while vcore service is re-starting |
| 78 | elif r.text: |
| 79 | # keeping only OLTs |
| 80 | devices = [d for d in r.json()["items"] if "olt" in d["type"]] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 81 | |
Vijaykumar Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 82 | log.debug("[OLT pull step] received devices", olts=devices) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 83 | |
Vijaykumar Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 84 | olts_in_voltha = self.create_or_update_olts(devices) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 85 | |
Vijaykumar Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 86 | self.delete_olts(olts_in_voltha) |
| 87 | else: |
| 88 | log.debug("[OLT pull step] Blank response received") |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 89 | |
Vijaykumar Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 90 | except (ValueError, TypeError), e: |
| 91 | log.warn("[OLT pull step] Invalid Json received in response from VOLTHA", reason=e) |
| 92 | return |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 93 | except ConnectionError, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 94 | log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 95 | return |
| 96 | except InvalidURL, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 97 | log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 98 | return |
| 99 | |
| 100 | def create_or_update_olts(self, olts): |
| 101 | |
| 102 | updated_olts = [] |
| 103 | |
| 104 | for olt in olts: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 105 | if olt["type"] == "simulated_olt": |
| 106 | [host, port] = ["172.17.0.1", "50060"] |
Matteo Scandolo | 778fef7 | 2019-04-22 13:32:36 -0700 | [diff] [blame] | 107 | elif "host_and_port" in olt: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 108 | [host, port] = olt["host_and_port"].split(":") |
Matteo Scandolo | 778fef7 | 2019-04-22 13:32:36 -0700 | [diff] [blame] | 109 | elif "mac_address" in olt: |
| 110 | mac_address = olt["mac_address"] |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 111 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 112 | olt_ports = self.fetch_olt_ports(olt["id"]) |
| 113 | |
| 114 | try: |
Matteo Scandolo | 778fef7 | 2019-04-22 13:32:36 -0700 | [diff] [blame] | 115 | if "host_and_port" in olt: |
| 116 | model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0] |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 117 | log.debug("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], id=model.id, host=host, port=port) |
Matteo Scandolo | 778fef7 | 2019-04-22 13:32:36 -0700 | [diff] [blame] | 118 | elif "mac_address" in olt: |
| 119 | model = OLTDevice.objects.filter(device_type=olt["type"], mac_address=mac_address)[0] |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 120 | log.debug("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], id=model.id, mac_address=mac_address) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 121 | |
| 122 | if model.enacted < model.updated: |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 123 | log.debug("[OLT pull step] Skipping pull on OLTDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 124 | # if we are not updating the device we still need to pull ports |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 125 | if olt_ports: |
| 126 | self.create_or_update_ports(olt_ports, model) |
Matteo Scandolo | a0291a1 | 2018-08-21 10:13:18 -0700 | [diff] [blame] | 127 | updated_olts.append(model) |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 128 | continue |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 129 | |
| 130 | except IndexError: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 131 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 132 | model = OLTDevice() |
| 133 | model.device_type = olt["type"] |
| 134 | |
| 135 | if olt["type"] == "simulated_olt": |
| 136 | model.host = "172.17.0.1" |
| 137 | model.port = 50060 |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 138 | log.debug("[OLT pull step] OLTDevice is new, creating it. Simulated") |
Matteo Scandolo | 778fef7 | 2019-04-22 13:32:36 -0700 | [diff] [blame] | 139 | elif "host_and_port" in olt: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 140 | [host, port] = olt["host_and_port"].split(":") |
| 141 | model.host = host |
| 142 | model.port = int(port) |
Matteo Scandolo | 778fef7 | 2019-04-22 13:32:36 -0700 | [diff] [blame] | 143 | log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port) |
| 144 | elif "mac_address" in olt: |
| 145 | model.mac_address = olt["mac_address"] |
| 146 | log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], mac_address=mac_address) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 147 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 148 | # there's no name in voltha, so make one up based on the id |
| 149 | model.name = "OLT-%s" % olt["id"] |
| 150 | |
| 151 | nni_ports = [p for p in olt_ports if "ETHERNET_NNI" in p["type"]] |
| 152 | if not nni_ports: |
| 153 | log.warning("[OLT pull step] No NNI ports, so no way to determine uplink. Skipping.", device_type=olt["type"], host=host, port=port) |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 154 | model.device_id = olt["id"] # device_id must be populated for delete_olts |
| 155 | updated_olts.append(model) |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 156 | continue |
| 157 | |
| 158 | # Exctract uplink from the first NNI port. This decision is arbitrary, we will worry about multiple |
| 159 | # NNI ports when that situation arises. |
| 160 | model.uplink = str(nni_ports[0]["port_no"]) |
| 161 | |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 162 | # Initial admin_state |
| 163 | model.admin_state = olt["admin_state"] |
| 164 | |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 165 | # Check to see if Voltha's serial_number field is populated. During Activation it's possible that |
| 166 | # Voltha's serial_number field may be blank. We want to avoid overwriting a populated data model |
| 167 | # serial number with an unpopulated Voltha serial number. IF this happened, then there would be |
| 168 | # a window of vulnerability where sadis requests will fail. |
| 169 | |
| 170 | if olt['serial_number'] and model.serial_number != olt['serial_number']: |
| 171 | # Check to see if data model serial number is already populated. If the serial number was |
| 172 | # already populated, and what we just learned from Voltha differs, then this is an error |
| 173 | # that should be made visible to the operator, so the operator may correct it. |
| 174 | if model.serial_number: |
| 175 | log.error("Serial number mismatch when pulling olt. Aborting OLT Update.", |
| 176 | model_serial_number=model.serial_number, |
| 177 | voltha_serial_number=olt['serial_number'], |
| 178 | olt_id=model.id) |
| 179 | model.backend_status = "Incorrect serial number" |
| 180 | model.backend_code = 2 |
| 181 | model.save_changed_fields() |
| 182 | # Have to include this in the result, or delete_olts() will delete it |
| 183 | updated_olts.append(model) |
| 184 | # Stop processing this OLT |
| 185 | continue |
| 186 | |
| 187 | # Preserve existing behavior. |
| 188 | # Data model serial number is unpopulated, so populate it. |
| 189 | |
| 190 | # TODO(smbaker): Consider making serial_number a required field, then do away with this. |
| 191 | # Deferred until after SEBA-2.0 release. |
| 192 | |
| 193 | log.info("Pull step learned olt serial number from voltha", |
| 194 | model_serial_number=model.serial_number, |
| 195 | voltha_serial_number=olt['serial_number'], |
| 196 | olt_id=model.id) |
| 197 | |
| 198 | model.serial_number = olt['serial_number'] |
| 199 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 200 | # Adding feedback state to the device |
| 201 | model.device_id = olt["id"] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 202 | model.oper_status = olt["oper_status"] |
| 203 | |
| 204 | model.volt_service = self.volt_service |
| 205 | model.volt_service_id = self.volt_service.id |
| 206 | |
| 207 | # get logical device |
| 208 | OLTDevicePullStep.get_ids_from_logical_device(model) |
| 209 | |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 210 | model.save_changed_fields() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 211 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 212 | if olt_ports: |
| 213 | self.create_or_update_ports(olt_ports, model) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 214 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 215 | updated_olts.append(model) |
| 216 | |
| 217 | return updated_olts |
| 218 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 219 | def fetch_olt_ports(self, olt_device_id): |
| 220 | """ Given an olt device_id, query voltha for the set of ports associated with that OLT. |
| 221 | |
| 222 | Returns a list of port dictionaries, or None in case of error. |
| 223 | """ |
| 224 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 225 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 226 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
| 227 | |
| 228 | try: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 229 | r = requests.get("%s:%s/api/v1/devices/%s/ports" % (voltha_url, voltha_port, olt_device_id), timeout=1) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 230 | |
| 231 | if r.status_code != 200: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 232 | log.warn("[OLT pull step] It was not possible to fetch ports from VOLTHA for device %s" % olt_device_id, |
| 233 | status_code=r.status_code) |
| 234 | return None |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 235 | |
| 236 | ports = r.json()['items'] |
| 237 | |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 238 | log.debug("[OLT pull step] received ports", ports=ports, olt=olt_device_id) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 239 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 240 | return ports |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 241 | |
| 242 | except ConnectionError, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 243 | log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e) |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 244 | return None |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 245 | except InvalidURL, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 246 | log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e) |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 247 | return None |
| 248 | |
| 249 | return None |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 250 | |
| 251 | def create_or_update_ports(self, ports, olt): |
| 252 | nni_ports = [p for p in ports if "ETHERNET_NNI" in p["type"]] |
| 253 | pon_ports = [p for p in ports if "PON_OLT" in p["type"]] |
| 254 | |
| 255 | self.create_or_update_nni_port(nni_ports, olt) |
| 256 | self.create_or_update_pon_port(pon_ports, olt) |
| 257 | |
| 258 | def create_or_update_pon_port(self, pon_ports, olt): |
| 259 | |
| 260 | update_ports = [] |
| 261 | |
| 262 | for port in pon_ports: |
| 263 | try: |
| 264 | model = PONPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0] |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 265 | log.debug("[OLT pull step] PONPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 266 | except IndexError: |
| 267 | model = PONPort() |
| 268 | model.port_no = port["port_no"] |
| 269 | model.olt_device_id = olt.id |
| 270 | model.name = port["label"] |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 271 | log.debug("[OLT pull step] PONPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 272 | |
| 273 | model.admin_state = port["admin_state"] |
| 274 | model.oper_status = port["oper_status"] |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 275 | model.save_changed_fields() |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 276 | update_ports.append(model) |
| 277 | return update_ports |
| 278 | |
| 279 | def create_or_update_nni_port(self, nni_ports, olt): |
| 280 | update_ports = [] |
| 281 | |
| 282 | for port in nni_ports: |
| 283 | try: |
| 284 | model = NNIPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0] |
| 285 | model.xos_managed = False |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 286 | log.debug("[OLT pull step] NNIPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 287 | except IndexError: |
| 288 | model = NNIPort() |
| 289 | model.port_no = port["port_no"] |
| 290 | model.olt_device_id = olt.id |
| 291 | model.name = port["label"] |
| 292 | model.xos_managed = False |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 293 | log.debug("[OLT pull step] NNIPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 294 | |
| 295 | model.admin_state = port["admin_state"] |
| 296 | model.oper_status = port["oper_status"] |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 297 | model.save_changed_fields() |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 298 | update_ports.append(model) |
| 299 | return update_ports |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 300 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 301 | def delete_olts(self, olts_in_voltha): |
Matteo Scandolo | a0291a1 | 2018-08-21 10:13:18 -0700 | [diff] [blame] | 302 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 303 | olts_id_in_voltha = [m.device_id for m in olts_in_voltha] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 304 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 305 | xos_olts = OLTDevice.objects.all() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 306 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 307 | deleted_in_voltha = [o for o in xos_olts if o.device_id not in olts_id_in_voltha] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 308 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 309 | for model in deleted_in_voltha: |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 310 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 311 | if model.enacted < model.updated: |
| 312 | # DO NOT delete a model that is being processed |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 313 | log.debug("[OLT pull step] device is not present in VOLTHA, skipping deletion as sync is in progress", device_id=o.device_id, |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 314 | name=o.name) |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 315 | continue |
| 316 | |
Scott Baker | 3581f64 | 2019-06-26 14:24:20 -0700 | [diff] [blame^] | 317 | log.debug("[OLT pull step] deleting device as it's not present in VOLTHA", device_id=o.device_id, name=o.name, id=o.id) |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 318 | model.delete() |