Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [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, ONUDevice, VOLTService, OLTDevice, PONPort, PONONUPort, UNIPort |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 17 | |
| 18 | from xosconfig import Config |
| 19 | from multistructlog import create_logger |
| 20 | |
| 21 | import requests |
| 22 | from requests import ConnectionError |
| 23 | from requests.models import InvalidURL |
| 24 | |
| 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 | |
| 30 | log = create_logger(Config().get('logging')) |
| 31 | |
| 32 | class ONUDevicePullStep(PullStep): |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 33 | def __init__(self, model_accessor): |
| 34 | super(ONUDevicePullStep, self).__init__(model_accessor=model_accessor, observed_model=ONUDevice) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 35 | |
| 36 | def pull_records(self): |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 37 | log.debug("pulling ONU devices from VOLTHA") |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 38 | |
| 39 | try: |
| 40 | self.volt_service = VOLTService.objects.all()[0] |
| 41 | except IndexError: |
| 42 | log.warn('VOLTService not found') |
| 43 | return |
| 44 | |
| 45 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 46 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
| 47 | |
| 48 | try: |
Matteo Scandolo | 10928dd | 2018-10-16 17:49:03 -0700 | [diff] [blame] | 49 | r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port), timeout=1) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 50 | |
| 51 | if r.status_code != 200: |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 52 | log.warn("It was not possible to fetch devices from VOLTHA") |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 53 | |
| 54 | # keeping only ONUs |
| 55 | devices = [d for d in r.json()["items"] if "onu" in d["type"]] |
| 56 | |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 57 | log.trace("received devices", onus=devices) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 58 | |
| 59 | # TODO |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 60 | # [ ] delete ONUS as ONUDevice.objects.all() - updated ONUs |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 61 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 62 | onus_in_voltha = self.create_or_update_onus(devices) |
| 63 | |
| 64 | except ConnectionError, e: |
| 65 | log.warn("It was not possible to connect to VOLTHA", reason=e) |
| 66 | return |
| 67 | except InvalidURL, e: |
| 68 | log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e) |
| 69 | return |
| 70 | |
| 71 | def create_or_update_onus(self, onus): |
| 72 | |
| 73 | updated_onus = [] |
| 74 | |
| 75 | for onu in onus: |
| 76 | try: |
| 77 | |
| 78 | model = ONUDevice.objects.filter(serial_number=onu["serial_number"])[0] |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 79 | log.trace("ONUDevice already exists, updating it", serial_number=onu["serial_number"]) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 80 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 81 | except IndexError: |
| 82 | model = ONUDevice() |
| 83 | model.serial_number = onu["serial_number"] |
Andy Bavier | ae33e16 | 2019-01-28 11:47:00 -0700 | [diff] [blame] | 84 | model.admin_state = onu["admin_state"] |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 85 | |
Andy Bavier | ae33e16 | 2019-01-28 11:47:00 -0700 | [diff] [blame] | 86 | log.debug("ONUDevice is new, creating it", serial_number=onu["serial_number"], admin_state=onu["admin_state"]) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 87 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 88 | try: |
| 89 | olt = OLTDevice.objects.get(device_id=onu["parent_id"]) |
| 90 | except IndexError: |
| 91 | log.warning("Unable to find olt for ONUDevice", serial_number=onu["serial_number"], olt_device_id=onu["parent_id"]) |
| 92 | continue |
| 93 | |
| 94 | try: |
| 95 | pon_port = PONPort.objects.get(port_no=onu["parent_port_no"], olt_device_id=olt.id) |
| 96 | except IndexError: |
| 97 | log.warning("Unable to find pon_port for ONUDevice", serial_number=onu["serial_number"], olt_device_id=onu["parent_id"], port_no=onu["parent_port_no"]) |
| 98 | continue |
| 99 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 100 | # Adding feedback state to the device |
| 101 | model.vendor = onu["vendor"] |
| 102 | model.device_type = onu["type"] |
| 103 | model.device_id = onu["id"] |
| 104 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 105 | model.oper_status = onu["oper_status"] |
| 106 | model.connect_status = onu["connect_status"] |
Scott Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 107 | model.reason = onu["reason"] |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 108 | model.xos_managed = False |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 109 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 110 | model.pon_port = pon_port |
| 111 | model.pon_port_id = pon_port.id |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 112 | |
Andy Bavier | ae33e16 | 2019-01-28 11:47:00 -0700 | [diff] [blame] | 113 | model.save_changed_fields() |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 114 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 115 | self.fetch_onu_ports(model) |
| 116 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 117 | updated_onus.append(model) |
| 118 | |
| 119 | return updated_onus |
| 120 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 121 | def fetch_onu_ports(self, onu): |
| 122 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 123 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 125 | try: |
Matteo Scandolo | 10928dd | 2018-10-16 17:49:03 -0700 | [diff] [blame] | 126 | r = requests.get("%s:%s/api/v1/devices/%s/ports" % (voltha_url, voltha_port, onu.device_id), timeout=1) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 127 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 128 | if r.status_code != 200: |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 129 | log.warn("It was not possible to fetch ports from VOLTHA for ONUDevice %s" % onu.device_id) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 130 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 131 | ports = r.json()['items'] |
| 132 | |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 133 | log.trace("received ports", ports=ports, onu=onu.device_id) |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 134 | |
| 135 | self.create_or_update_ports(ports, onu) |
| 136 | |
| 137 | except ConnectionError, e: |
| 138 | log.warn("It was not possible to connect to VOLTHA", reason=e) |
| 139 | return |
| 140 | except InvalidURL, e: |
| 141 | log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e) |
| 142 | return |
| 143 | return |
| 144 | |
| 145 | def create_or_update_ports(self, ports, onu): |
| 146 | uni_ports = [p for p in ports if "ETHERNET_UNI" in p["type"]] |
| 147 | pon_onu_ports = [p for p in ports if "PON_ONU" in p["type"]] |
| 148 | |
| 149 | self.create_or_update_uni_port(uni_ports, onu) |
| 150 | self.create_or_update_pon_onu_port(pon_onu_ports, onu) |
| 151 | |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 152 | def get_onu_port_id(self, port, onu): |
| 153 | # find the correct port id as represented in the logical_device |
| 154 | logical_device_id = onu.pon_port.olt_device.of_id |
| 155 | |
| 156 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 157 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
| 158 | |
| 159 | try: |
Matteo Scandolo | 10928dd | 2018-10-16 17:49:03 -0700 | [diff] [blame] | 160 | r = requests.get("%s:%s/api/v1/logical_devices/%s/ports" % (voltha_url, voltha_port, logical_device_id), timeout=1) |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 161 | |
| 162 | if r.status_code != 200: |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 163 | log.warn("It was not possible to fetch ports from VOLTHA for logical_device %s" % logical_device_id) |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 164 | |
| 165 | logical_ports = r.json()['items'] |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 166 | log.trace("logical device ports for ONUDevice %s" % onu.device_id, logical_ports=logical_ports) |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 167 | |
Jonathan Hart | d6de1b9 | 2018-07-18 18:52:28 -0700 | [diff] [blame] | 168 | ports = [p['ofp_port']['port_no'] for p in logical_ports if p['device_id'] == onu.device_id] |
Matteo Scandolo | 27d2fbb | 2018-09-07 14:02:53 -0700 | [diff] [blame] | 169 | # log.debug("Port_id for port %s on ONUDevice %s: %s" % (port['label'], onu.device_id, ports), logical_ports=logical_ports) |
Matteo Scandolo | f140ded | 2018-10-17 15:37:15 -0700 | [diff] [blame] | 170 | # FIXME if this throws an error ONUs from other OTLs are not sync'ed |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 171 | return int(ports[0]) |
| 172 | |
| 173 | except ConnectionError, e: |
| 174 | log.warn("It was not possible to connect to VOLTHA", reason=e) |
| 175 | return |
| 176 | except InvalidURL, e: |
| 177 | log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e) |
| 178 | return |
| 179 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 180 | def create_or_update_uni_port(self, uni_ports, onu): |
| 181 | update_ports = [] |
| 182 | |
| 183 | for port in uni_ports: |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 184 | port_no = self.get_onu_port_id(port, onu) |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 185 | try: |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 186 | model = UNIPort.objects.filter(port_no=port_no, onu_device_id=onu.id)[0] |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 187 | log.trace("UNIPort already exists, updating it", port_no=port_no, onu_device_id=onu.id) |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 188 | except IndexError: |
| 189 | model = UNIPort() |
Matteo Scandolo | 72f43d0 | 2018-07-16 10:54:01 -0400 | [diff] [blame] | 190 | model.port_no = port_no |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 191 | model.onu_device_id = onu.id |
| 192 | model.name = port["label"] |
| 193 | log.debug("UNIPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id) |
| 194 | |
| 195 | model.admin_state = port["admin_state"] |
| 196 | model.oper_status = port["oper_status"] |
Andy Bavier | ae33e16 | 2019-01-28 11:47:00 -0700 | [diff] [blame] | 197 | model.save_changed_fields() |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 198 | update_ports.append(model) |
| 199 | return update_ports |
| 200 | |
| 201 | def create_or_update_pon_onu_port(self, pon_onu_ports, onu): |
| 202 | update_ports = [] |
| 203 | |
| 204 | for port in pon_onu_ports: |
| 205 | try: |
| 206 | model = PONONUPort.objects.filter(port_no=port["port_no"], onu_device_id=onu.id)[0] |
| 207 | model.xos_managed = False |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 208 | log.trace("PONONUPort already exists, updating it", port_no=port["port_no"], onu_device_id=onu.id) |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 209 | except IndexError: |
| 210 | model = PONONUPort() |
| 211 | model.port_no = port["port_no"] |
| 212 | model.onu_device_id = onu.id |
| 213 | model.name = port["label"] |
| 214 | model.xos_managed = False |
| 215 | log.debug("PONONUPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id) |
| 216 | |
| 217 | model.admin_state = port["admin_state"] |
| 218 | model.oper_status = port["oper_status"] |
Andy Bavier | ae33e16 | 2019-01-28 11:47:00 -0700 | [diff] [blame] | 219 | model.save_changed_fields() |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 220 | update_ports.append(model) |
| 221 | return update_ports |