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 | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.modelaccessor import model_accessor, ONUDevice, VOLTService, OLTDevice, PONPort |
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 |
| 60 | # [ ] delete ONUS as ONUDevice.objects.all() - updated OLTs |
| 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 | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 86 | return |
| 87 | |
| 88 | except IndexError: |
| 89 | model = ONUDevice() |
| 90 | model.serial_number = onu["serial_number"] |
| 91 | |
| 92 | log.debug("ONUDevice is new, creating it", serial_number=onu["serial_number"]) |
| 93 | |
| 94 | # Adding feedback state to the device |
| 95 | model.vendor = onu["vendor"] |
| 96 | model.device_type = onu["type"] |
| 97 | model.device_id = onu["id"] |
| 98 | |
| 99 | model.admin_state = onu["admin_state"] |
| 100 | model.oper_status = onu["oper_status"] |
| 101 | model.connect_status = onu["connect_status"] |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 102 | model.xos_managed = False |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 103 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 104 | olt = OLTDevice.objects.get(device_id=onu["parent_id"]) |
| 105 | pon_port = PONPort.objects.get(port_no=onu["parent_port_no"], olt_device_id=olt.id) |
| 106 | |
| 107 | model.pon_port = pon_port |
| 108 | model.pon_port_id = pon_port.id |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 109 | |
| 110 | model.save() |
| 111 | |
| 112 | updated_onus.append(model) |
| 113 | |
| 114 | return updated_onus |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |