blob: 4f663048cb4f2b1d94cb7710e15c2644394a1290 [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
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070016from synchronizers.new_base.modelaccessor import model_accessor, ONUDevice, VOLTService, OLTDevice, PONPort, PONONUPort, 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):
33 def __init__(self):
34 super(ONUDevicePullStep, self).__init__(observed_model=ONUDevice)
35
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
54 # keeping only ONUs
55 devices = [d for d in r.json()["items"] if "onu" in d["type"]]
56
Matteo Scandolof7ebb112018-09-18 16:17:22 -070057 log.trace("received devices", onus=devices)
Matteo Scandolod44ca992018-05-17 15:02:10 -070058
59 # TODO
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070060 # [ ] delete ONUS as ONUDevice.objects.all() - updated ONUs
Matteo Scandolod44ca992018-05-17 15:02:10 -070061
Matteo Scandolod44ca992018-05-17 15:02:10 -070062 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 Scandolof7ebb112018-09-18 16:17:22 -070079 log.trace("ONUDevice already exists, updating it", serial_number=onu["serial_number"])
Matteo Scandolod44ca992018-05-17 15:02:10 -070080
81 if model.enacted < model.updated:
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -070082 log.debug("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 Scandolod8ed60e2018-06-18 17:00:57 -070083 # if we are not updating the device we still need to pull ports
84 self.fetch_onu_ports(model)
Scott Baker22b46c52018-11-15 15:15:29 -080085 continue
Matteo Scandolod44ca992018-05-17 15:02:10 -070086
87 except IndexError:
88 model = ONUDevice()
89 model.serial_number = onu["serial_number"]
90
91 log.debug("ONUDevice is new, creating it", serial_number=onu["serial_number"])
92
Scott Baker22b46c52018-11-15 15:15:29 -080093 try:
94 olt = OLTDevice.objects.get(device_id=onu["parent_id"])
95 except IndexError:
96 log.warning("Unable to find olt for ONUDevice", serial_number=onu["serial_number"], olt_device_id=onu["parent_id"])
97 continue
98
99 try:
100 pon_port = PONPort.objects.get(port_no=onu["parent_port_no"], olt_device_id=olt.id)
101 except IndexError:
102 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"])
103 continue
104
Matteo Scandolod44ca992018-05-17 15:02:10 -0700105 # Adding feedback state to the device
106 model.vendor = onu["vendor"]
107 model.device_type = onu["type"]
108 model.device_id = onu["id"]
109
110 model.admin_state = onu["admin_state"]
111 model.oper_status = onu["oper_status"]
112 model.connect_status = onu["connect_status"]
Scott Baker3db0eef2019-01-15 11:56:41 -0800113 model.reason = onu["reason"]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700114 model.xos_managed = False
Matteo Scandolod44ca992018-05-17 15:02:10 -0700115
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700116 model.pon_port = pon_port
117 model.pon_port_id = pon_port.id
Matteo Scandolod44ca992018-05-17 15:02:10 -0700118
119 model.save()
120
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700121 self.fetch_onu_ports(model)
122
Matteo Scandolod44ca992018-05-17 15:02:10 -0700123 updated_onus.append(model)
124
125 return updated_onus
126
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700127 def fetch_onu_ports(self, onu):
128 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
129 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
Matteo Scandolod44ca992018-05-17 15:02:10 -0700130
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700131 try:
Matteo Scandolo10928dd2018-10-16 17:49:03 -0700132 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 -0700133
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700134 if r.status_code != 200:
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700135 log.warn("It was not possible to fetch ports from VOLTHA for ONUDevice %s" % onu.device_id)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700136
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700137 ports = r.json()['items']
138
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700139 log.trace("received ports", ports=ports, onu=onu.device_id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700140
141 self.create_or_update_ports(ports, onu)
142
143 except ConnectionError, e:
144 log.warn("It was not possible to connect to VOLTHA", reason=e)
145 return
146 except InvalidURL, e:
147 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
148 return
149 return
150
151 def create_or_update_ports(self, ports, onu):
152 uni_ports = [p for p in ports if "ETHERNET_UNI" in p["type"]]
153 pon_onu_ports = [p for p in ports if "PON_ONU" in p["type"]]
154
155 self.create_or_update_uni_port(uni_ports, onu)
156 self.create_or_update_pon_onu_port(pon_onu_ports, onu)
157
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400158 def get_onu_port_id(self, port, onu):
159 # find the correct port id as represented in the logical_device
160 logical_device_id = onu.pon_port.olt_device.of_id
161
162 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
163 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
164
165 try:
Matteo Scandolo10928dd2018-10-16 17:49:03 -0700166 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 -0400167
168 if r.status_code != 200:
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700169 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 -0400170
171 logical_ports = r.json()['items']
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700172 log.trace("logical device ports for ONUDevice %s" % onu.device_id, logical_ports=logical_ports)
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400173
Jonathan Hartd6de1b92018-07-18 18:52:28 -0700174 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 -0700175 # 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 -0700176 # FIXME if this throws an error ONUs from other OTLs are not sync'ed
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400177 return int(ports[0])
178
179 except ConnectionError, e:
180 log.warn("It was not possible to connect to VOLTHA", reason=e)
181 return
182 except InvalidURL, e:
183 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
184 return
185
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700186 def create_or_update_uni_port(self, uni_ports, onu):
187 update_ports = []
188
189 for port in uni_ports:
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400190 port_no = self.get_onu_port_id(port, onu)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700191 try:
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400192 model = UNIPort.objects.filter(port_no=port_no, onu_device_id=onu.id)[0]
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700193 log.trace("UNIPort already exists, updating it", port_no=port_no, onu_device_id=onu.id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700194 except IndexError:
195 model = UNIPort()
Matteo Scandolo72f43d02018-07-16 10:54:01 -0400196 model.port_no = port_no
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700197 model.onu_device_id = onu.id
198 model.name = port["label"]
199 log.debug("UNIPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id)
200
201 model.admin_state = port["admin_state"]
202 model.oper_status = port["oper_status"]
203 model.save()
204 update_ports.append(model)
205 return update_ports
206
207 def create_or_update_pon_onu_port(self, pon_onu_ports, onu):
208 update_ports = []
209
210 for port in pon_onu_ports:
211 try:
212 model = PONONUPort.objects.filter(port_no=port["port_no"], onu_device_id=onu.id)[0]
213 model.xos_managed = False
Matteo Scandolof7ebb112018-09-18 16:17:22 -0700214 log.trace("PONONUPort already exists, updating it", port_no=port["port_no"], onu_device_id=onu.id)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700215 except IndexError:
216 model = PONONUPort()
217 model.port_no = port["port_no"]
218 model.onu_device_id = onu.id
219 model.name = port["label"]
220 model.xos_managed = False
221 log.debug("PONONUPort is new, creating it", port_no=port["port_no"], onu_device_id=onu.id)
222
223 model.admin_state = port["admin_state"]
224 model.oper_status = port["oper_status"]
225 model.save()
226 update_ports.append(model)
227 return update_ports