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 | |
| 15 | from synchronizers.new_base.pullstep import PullStep |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.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): |
| 33 | def __init__(self): |
| 34 | super(OLTDevicePullStep, self).__init__(observed_model=OLTDevice) |
| 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 | return o |
| 53 | |
| 54 | 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] | 55 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 56 | def pull_records(self): |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 57 | log.debug("[OLT pull step] pulling OLT devices from VOLTHA") |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 58 | |
| 59 | try: |
| 60 | self.volt_service = VOLTService.objects.all()[0] |
| 61 | except IndexError: |
| 62 | log.warn('VOLTService not found') |
| 63 | return |
| 64 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 65 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 66 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Matteo Scandolo | 10928dd | 2018-10-16 17:49:03 -0700 | [diff] [blame] | 69 | 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] | 70 | |
| 71 | if r.status_code != 200: |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 72 | 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] | 73 | |
| 74 | # keeping only OLTs |
| 75 | devices = [d for d in r.json()["items"] if "olt" in d["type"]] |
| 76 | |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 77 | log.trace("[OLT pull step] received devices", olts=devices) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 78 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 79 | olts_in_voltha = self.create_or_update_olts(devices) |
| 80 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 81 | self.delete_olts(olts_in_voltha) |
| 82 | |
| 83 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 84 | except ConnectionError, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 85 | 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] | 86 | return |
| 87 | except InvalidURL, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 88 | 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] | 89 | return |
| 90 | |
| 91 | def create_or_update_olts(self, olts): |
| 92 | |
| 93 | updated_olts = [] |
| 94 | |
| 95 | for olt in olts: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 96 | if olt["type"] == "simulated_olt": |
| 97 | [host, port] = ["172.17.0.1", "50060"] |
| 98 | else: |
| 99 | [host, port] = olt["host_and_port"].split(":") |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 100 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 101 | olt_ports = self.fetch_olt_ports(olt["id"]) |
| 102 | |
| 103 | try: |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 104 | model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0] |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 105 | |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 106 | log.trace("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 107 | |
| 108 | if model.enacted < model.updated: |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 109 | 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] | 110 | # 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] | 111 | if olt_ports: |
| 112 | self.create_or_update_ports(olt_ports, model) |
Matteo Scandolo | a0291a1 | 2018-08-21 10:13:18 -0700 | [diff] [blame] | 113 | updated_olts.append(model) |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 114 | continue |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 115 | |
| 116 | except IndexError: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 117 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 118 | model = OLTDevice() |
| 119 | model.device_type = olt["type"] |
| 120 | |
| 121 | if olt["type"] == "simulated_olt": |
| 122 | model.host = "172.17.0.1" |
| 123 | model.port = 50060 |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 124 | else: |
| 125 | [host, port] = olt["host_and_port"].split(":") |
| 126 | model.host = host |
| 127 | model.port = int(port) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 128 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 129 | # there's no name in voltha, so make one up based on the id |
| 130 | model.name = "OLT-%s" % olt["id"] |
| 131 | |
| 132 | nni_ports = [p for p in olt_ports if "ETHERNET_NNI" in p["type"]] |
| 133 | if not nni_ports: |
| 134 | log.warning("[OLT pull step] No NNI ports, so no way to determine uplink. Skipping.", device_type=olt["type"], host=host, port=port) |
| 135 | continue |
| 136 | |
| 137 | # Exctract uplink from the first NNI port. This decision is arbitrary, we will worry about multiple |
| 138 | # NNI ports when that situation arises. |
| 139 | model.uplink = str(nni_ports[0]["port_no"]) |
| 140 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 141 | log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 142 | |
| 143 | # Adding feedback state to the device |
| 144 | model.device_id = olt["id"] |
| 145 | model.admin_state = olt["admin_state"] |
| 146 | model.oper_status = olt["oper_status"] |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 147 | model.serial_number = olt['serial_number'] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 148 | |
| 149 | model.volt_service = self.volt_service |
| 150 | model.volt_service_id = self.volt_service.id |
| 151 | |
| 152 | # get logical device |
| 153 | OLTDevicePullStep.get_ids_from_logical_device(model) |
| 154 | |
| 155 | model.save() |
| 156 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 157 | if olt_ports: |
| 158 | self.create_or_update_ports(olt_ports, model) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 159 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 160 | updated_olts.append(model) |
| 161 | |
| 162 | return updated_olts |
| 163 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 164 | def fetch_olt_ports(self, olt_device_id): |
| 165 | """ Given an olt device_id, query voltha for the set of ports associated with that OLT. |
| 166 | |
| 167 | Returns a list of port dictionaries, or None in case of error. |
| 168 | """ |
| 169 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 170 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 171 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
| 172 | |
| 173 | try: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 174 | 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] | 175 | |
| 176 | if r.status_code != 200: |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 177 | log.warn("[OLT pull step] It was not possible to fetch ports from VOLTHA for device %s" % olt_device_id, |
| 178 | status_code=r.status_code) |
| 179 | return None |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 180 | |
| 181 | ports = r.json()['items'] |
| 182 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 183 | log.trace("[OLT pull step] received ports", ports=ports, olt=olt_device_id) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 184 | |
Scott Baker | d01fae7 | 2018-12-11 12:27:27 -0800 | [diff] [blame] | 185 | return ports |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 186 | |
| 187 | except ConnectionError, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 188 | 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] | 189 | return None |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 190 | except InvalidURL, e: |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 191 | 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] | 192 | return None |
| 193 | |
| 194 | return None |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 195 | |
| 196 | def create_or_update_ports(self, ports, olt): |
| 197 | nni_ports = [p for p in ports if "ETHERNET_NNI" in p["type"]] |
| 198 | pon_ports = [p for p in ports if "PON_OLT" in p["type"]] |
| 199 | |
| 200 | self.create_or_update_nni_port(nni_ports, olt) |
| 201 | self.create_or_update_pon_port(pon_ports, olt) |
| 202 | |
| 203 | def create_or_update_pon_port(self, pon_ports, olt): |
| 204 | |
| 205 | update_ports = [] |
| 206 | |
| 207 | for port in pon_ports: |
| 208 | try: |
| 209 | model = PONPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0] |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 210 | log.trace("[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] | 211 | except IndexError: |
| 212 | model = PONPort() |
| 213 | model.port_no = port["port_no"] |
| 214 | model.olt_device_id = olt.id |
| 215 | model.name = port["label"] |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 216 | 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] | 217 | |
| 218 | model.admin_state = port["admin_state"] |
| 219 | model.oper_status = port["oper_status"] |
| 220 | model.save() |
| 221 | update_ports.append(model) |
| 222 | return update_ports |
| 223 | |
| 224 | def create_or_update_nni_port(self, nni_ports, olt): |
| 225 | update_ports = [] |
| 226 | |
| 227 | for port in nni_ports: |
| 228 | try: |
| 229 | model = NNIPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0] |
| 230 | model.xos_managed = False |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 231 | log.trace("[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] | 232 | except IndexError: |
| 233 | model = NNIPort() |
| 234 | model.port_no = port["port_no"] |
| 235 | model.olt_device_id = olt.id |
| 236 | model.name = port["label"] |
| 237 | model.xos_managed = False |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 238 | 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] | 239 | |
| 240 | model.admin_state = port["admin_state"] |
| 241 | model.oper_status = port["oper_status"] |
| 242 | model.save() |
| 243 | update_ports.append(model) |
| 244 | return update_ports |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 245 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 246 | def delete_olts(self, olts_in_voltha): |
Matteo Scandolo | a0291a1 | 2018-08-21 10:13:18 -0700 | [diff] [blame] | 247 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 248 | 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] | 249 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 250 | xos_olts = OLTDevice.objects.all() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 251 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 252 | 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] | 253 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 254 | for model in deleted_in_voltha: |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 255 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 256 | if model.enacted < model.updated: |
| 257 | # DO NOT delete a model that is being processed |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 258 | log.debug("[OLT pull step] device is not present in VOLTHA, skipping deletion as sync is in progress", device_id=o.device_id, |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 259 | name=o.name) |
| 260 | continue |
| 261 | |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 262 | log.debug("[OLT pull step] deleting device as it's not present in VOLTHA", device_id=o.device_id, name=o.name) |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 263 | model.delete() |