blob: be04ca9a464b62dc601a3a6f12c9e7ad8d5a5076 [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
Scott Baker47b47302019-01-30 16:55:07 -080015from xossynchronizer.pull_steps.pullstep import PullStep
Matteo Scandolo915c7b82019-05-09 16:26:12 -070016from xossynchronizer.modelaccessor import model_accessor, ONUDevice, VOLTService, OLTDevice, PONPort, ANIPort, UNIPort
Matteo Scandolod44ca992018-05-17 15:02:10 -070017
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):
Scott Baker47b47302019-01-30 16:55:07 -080033 def __init__(self, model_accessor):
34 super(ONUDevicePullStep, self).__init__(model_accessor=model_accessor, observed_model=ONUDevice)
Matteo Scandolod44ca992018-05-17 15:02:10 -070035
36 def pull_records(self):
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -070037 log.debug("pulling ONU devices from VOLTHA")
Matteo Scandolod44ca992018-05-17 15:02:10 -070038
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 Scandolo10928dd2018-10-16 17:49:03 -070049 r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port), timeout=1)
Matteo Scandolod44ca992018-05-17 15:02:10 -070050
51 if r.status_code != 200:
Matteo Scandolof7ebb112018-09-18 16:17:22 -070052 log.warn("It was not possible to fetch devices from VOLTHA")
Matteo Scandolod44ca992018-05-17 15:02:10 -070053
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053054 # [SEBA-367] Handling blank response received from Voltha, Scenario occurs when voltha api is called while vcore service is re-starting
Matteo Scandolod44ca992018-05-17 15:02:10 -070055
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053056 elif r.text:
57 # keeping only ONUs
58 devices = [d for d in r.json()["items"] if "onu" in d["type"]]
Matteo Scandolod44ca992018-05-17 15:02:10 -070059
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053060 log.debug("received devices", onus=devices)
Matteo Scandolod44ca992018-05-17 15:02:10 -070061
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053062 # TODO
63 # [ ] delete ONUS as ONUDevice.objects.all() - updated ONUs
Matteo Scandolod44ca992018-05-17 15:02:10 -070064
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053065 onus_in_voltha = self.create_or_update_onus(devices)
66 else:
67 log.debug("[ONU pull step] Blank response received")
68
69 except (ValueError, TypeError), e:
70 log.warn("[ONU pull step] Invalid Json received in response from VOLTHA", reason=e)
71 return
Matteo Scandolod44ca992018-05-17 15:02:10 -070072 except ConnectionError, e:
73 log.warn("It was not possible to connect to VOLTHA", reason=e)
74 return
75 except InvalidURL, e:
76 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
77 return
78
79 def create_or_update_onus(self, onus):
80
81 updated_onus = []
82
83 for onu in onus:
84 try:
85
86 model = ONUDevice.objects.filter(serial_number=onu["serial_number"])[0]
Andy Bavier00c573c2019-02-08 16:19:11 -070087 log.debug("ONUDevice already exists, updating it", serial_number=onu["serial_number"])
Matteo Scandolod44ca992018-05-17 15:02:10 -070088
Matteo Scandolod44ca992018-05-17 15:02:10 -070089 except IndexError:
90 model = ONUDevice()
91 model.serial_number = onu["serial_number"]
Andy Bavierae33e162019-01-28 11:47:00 -070092 model.admin_state = onu["admin_state"]
Matteo Scandolod44ca992018-05-17 15:02:10 -070093
Andy Bavierae33e162019-01-28 11:47:00 -070094 log.debug("ONUDevice is new, creating it", serial_number=onu["serial_number"], admin_state=onu["admin_state"])
Matteo Scandolod44ca992018-05-17 15:02:10 -070095
Scott Baker22b46c52018-11-15 15:15:29 -080096 try:
97 olt = OLTDevice.objects.get(device_id=onu["parent_id"])
98 except IndexError:
99 log.warning("Unable to find olt for ONUDevice", serial_number=onu["serial_number"], olt_device_id=onu["parent_id"])
100 continue
101
102 try:
103 pon_port = PONPort.objects.get(port_no=onu["parent_port_no"], olt_device_id=olt.id)
104 except IndexError:
105 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"])
106 continue
107
Matteo Scandolod44ca992018-05-17 15:02:10 -0700108 # Adding feedback state to the device
109 model.vendor = onu["vendor"]
110 model.device_type = onu["type"]
111 model.device_id = onu["id"]
112
Matteo Scandolod44ca992018-05-17 15:02:10 -0700113 model.oper_status = onu["oper_status"]
114 model.connect_status = onu["connect_status"]
Scott Baker3db0eef2019-01-15 11:56:41 -0800115 model.reason = onu["reason"]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700116 model.xos_managed = False
Matteo Scandolod44ca992018-05-17 15:02:10 -0700117
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700118 model.pon_port = pon_port
119 model.pon_port_id = pon_port.id
Matteo Scandolod44ca992018-05-17 15:02:10 -0700120
Andy Bavierae33e162019-01-28 11:47:00 -0700121 model.save_changed_fields()
Matteo Scandolod44ca992018-05-17 15:02:10 -0700122
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700123 self.fetch_onu_ports(model)
124
Matteo Scandolod44ca992018-05-17 15:02:10 -0700125 updated_onus.append(model)
126
127 return updated_onus
128
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700129 def fetch_onu_ports(self, onu):
130 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
131 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
Matteo Scandolod44ca992018-05-17 15:02:10 -0700132
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700133 try:
Matteo Scandolo10928dd2018-10-16 17:49:03 -0700134 r = requests.get("%s:%s/api/v1/devices/%s/ports" % (voltha_url, voltha_port, onu.device_id), timeout=1)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700135
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700136 if r.status_code != 200:
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700137 log.warn("It was not possible to fetch ports from VOLTHA for ONUDevice %s" % onu.device_id)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700138
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700139 ports = r.json()['items']
140
Andy Bavier00c573c2019-02-08 16:19:11 -0700141 log.debug("received ports", ports=ports, onu=onu.device_id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700142
143 self.create_or_update_ports(ports, onu)
144
145 except ConnectionError, e:
146 log.warn("It was not possible to connect to VOLTHA", reason=e)
147 return
148 except InvalidURL, e:
149 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
150 return
151 return
152
153 def create_or_update_ports(self, ports, onu):
154 uni_ports = [p for p in ports if "ETHERNET_UNI" in p["type"]]
155 pon_onu_ports = [p for p in ports if "PON_ONU" in p["type"]]
156
157 self.create_or_update_uni_port(uni_ports, onu)
Matteo Scandolo915c7b82019-05-09 16:26:12 -0700158 self.create_or_update_ani_port(pon_onu_ports, onu)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700159
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400160 def get_onu_port_id(self, port, onu):
161 # find the correct port id as represented in the logical_device
162 logical_device_id = onu.pon_port.olt_device.of_id
163
164 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
165 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
166
167 try:
Matteo Scandolo10928dd2018-10-16 17:49:03 -0700168 r = requests.get("%s:%s/api/v1/logical_devices/%s/ports" % (voltha_url, voltha_port, logical_device_id), timeout=1)
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400169
170 if r.status_code != 200:
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700171 log.warn("It was not possible to fetch ports from VOLTHA for logical_device %s" % logical_device_id)
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400172
173 logical_ports = r.json()['items']
Andy Bavier00c573c2019-02-08 16:19:11 -0700174 log.debug("logical device ports for ONUDevice %s" % onu.device_id, logical_ports=logical_ports)
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400175
Jonathan Hartd6de1b92018-07-18 18:52:28 -0700176 ports = [p['ofp_port']['port_no'] for p in logical_ports if p['device_id'] == onu.device_id]
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -0700177 # log.debug("Port_id for port %s on ONUDevice %s: %s" % (port['label'], onu.device_id, ports), logical_ports=logical_ports)
Matteo Scandolof140ded2018-10-17 15:37:15 -0700178 # FIXME if this throws an error ONUs from other OTLs are not sync'ed
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400179 return int(ports[0])
180
181 except ConnectionError, e:
182 log.warn("It was not possible to connect to VOLTHA", reason=e)
183 return
184 except InvalidURL, e:
185 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
186 return
187
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700188 def create_or_update_uni_port(self, uni_ports, onu):
189 update_ports = []
190
191 for port in uni_ports:
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400192 port_no = self.get_onu_port_id(port, onu)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700193 try:
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400194 model = UNIPort.objects.filter(port_no=port_no, onu_device_id=onu.id)[0]
Andy Bavier00c573c2019-02-08 16:19:11 -0700195 log.debug("UNIPort already exists, updating it", port_no=port_no, onu_device_id=onu.id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700196 except IndexError:
197 model = UNIPort()
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400198 model.port_no = port_no
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700199 model.onu_device_id = onu.id
200 model.name = port["label"]
201 log.debug("UNIPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id)
202
203 model.admin_state = port["admin_state"]
204 model.oper_status = port["oper_status"]
Andy Bavierae33e162019-01-28 11:47:00 -0700205 model.save_changed_fields()
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700206 update_ports.append(model)
207 return update_ports
208
Matteo Scandolo915c7b82019-05-09 16:26:12 -0700209 def create_or_update_ani_port(self, pon_onu_ports, onu):
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700210 update_ports = []
211
212 for port in pon_onu_ports:
213 try:
Matteo Scandolo915c7b82019-05-09 16:26:12 -0700214 model = ANIPort.objects.filter(port_no=port["port_no"], onu_device_id=onu.id)[0]
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700215 model.xos_managed = False
Matteo Scandolo915c7b82019-05-09 16:26:12 -0700216 log.debug("ANIPort already exists, updating it", port_no=port["port_no"], onu_device_id=onu.id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700217 except IndexError:
Matteo Scandolo915c7b82019-05-09 16:26:12 -0700218 model = ANIPort()
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700219 model.port_no = port["port_no"]
220 model.onu_device_id = onu.id
221 model.name = port["label"]
222 model.xos_managed = False
Matteo Scandolo915c7b82019-05-09 16:26:12 -0700223 log.debug("ANIPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700224
225 model.admin_state = port["admin_state"]
226 model.oper_status = port["oper_status"]
Andy Bavierae33e162019-01-28 11:47:00 -0700227 model.save_changed_fields()
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700228 update_ports.append(model)
229 return update_ports