blob: cc60ca38ef03280d7a344aad63710f5a90d46cbc [file] [log] [blame]
Matteo Scandolod44ca992018-05-17 15:02:10 -07001# 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
15from synchronizers.new_base.pullstep import PullStep
16from synchronizers.new_base.modelaccessor import model_accessor, ONUDevice, VOLTService, OLTDevice
17
18from xosconfig import Config
19from multistructlog import create_logger
20
21import requests
22from requests import ConnectionError
23from requests.models import InvalidURL
24
25import os, sys
26sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
27
28from helpers import Helpers
29
30log = create_logger(Config().get('logging'))
31
32class ONUDevicePullStep(PullStep):
33 def __init__(self):
34 super(ONUDevicePullStep, self).__init__(observed_model=ONUDevice)
35
36 def pull_records(self):
Matteo Scandolo7db85372018-05-22 15:26:55 -070037 return
38 # FIXME we need to pull PON Ports before
Matteo Scandolod44ca992018-05-17 15:02:10 -070039 log.info("pulling ONU devices from VOLTHA")
40
41 try:
42 self.volt_service = VOLTService.objects.all()[0]
43 except IndexError:
44 log.warn('VOLTService not found')
45 return
46
47 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
48 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
49
50 try:
51 r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port))
52
53 if r.status_code != 200:
54 log.info("It was not possible to fetch devices from VOLTHA")
55
56 # keeping only ONUs
57 devices = [d for d in r.json()["items"] if "onu" in d["type"]]
58
59 log.debug("received devices", onus=devices)
60
61 # TODO
62 # [ ] delete ONUS as ONUDevice.objects.all() - updated OLTs
63
64 if r.status_code != 200:
65 log.info("It was not possible to fetch devices from VOLTHA")
66
67 onus_in_voltha = self.create_or_update_onus(devices)
68
69 except ConnectionError, e:
70 log.warn("It was not possible to connect to VOLTHA", reason=e)
71 return
72 except InvalidURL, e:
73 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
74 return
75
76 def create_or_update_onus(self, onus):
77
78 updated_onus = []
79
80 for onu in onus:
81 try:
82
83 model = ONUDevice.objects.filter(serial_number=onu["serial_number"])[0]
84 log.debug("ONUDevice already exists, updating it", serial_number=onu["serial_number"])
85
86 if model.enacted < model.updated:
87 log.info("Skipping pull on ONUDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated)
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"]
104
Matteo Scandolo7db85372018-05-22 15:26:55 -0700105 # olt = OLTDevice.objects.get(device_id=onu["proxy_address"]["device_id"])
106 #
107 # model.olt_device = olt
108 # model.olt_device_id = olt.id
Matteo Scandolod44ca992018-05-17 15:02:10 -0700109
110 model.save()
111
112 updated_onus.append(model)
113
114 return updated_onus
115
116
117
118
119
120