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