blob: 7c2cadf28a4702cbb21550a8ea5f306fa4028d24 [file] [log] [blame]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -08001# 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
16from xossynchronizer.modelaccessor import model_accessor, OLTDevice, VOLTService, PONPort, NNIPort
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080017
18from xosconfig import Config
19from multistructlog import create_logger
20
Matteo Scandolo33523412018-04-12 15:21:13 -070021import requests
22from requests import ConnectionError
23from requests.models import InvalidURL
24
Matteo Scandolod44ca992018-05-17 15:02:10 -070025import os, sys
26sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
27
28from helpers import Helpers
29
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080030log = create_logger(Config().get('logging'))
31
32class OLTDevicePullStep(PullStep):
Scott Baker47b47302019-01-30 16:55:07 -080033 def __init__(self, model_accessor):
34 super(OLTDevicePullStep, self).__init__(model_accessor=model_accessor, observed_model=OLTDevice)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080035
Matteo Scandolo33523412018-04-12 15:21:13 -070036 @staticmethod
37 def get_ids_from_logical_device(o):
Matteo Scandolod44ca992018-05-17 15:02:10 -070038 voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
39 voltha_port = Helpers.get_voltha_info(o.volt_service)['port']
Matteo Scandolo33523412018-04-12 15:21:13 -070040
Matteo Scandolo10928dd2018-10-16 17:49:03 -070041 r = requests.get("%s:%s/api/v1/logical_devices" % (voltha_url, voltha_port), timeout=1)
Matteo Scandolo33523412018-04-12 15:21:13 -070042
43 if r.status_code != 200:
44 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text)
45
46 res = r.json()
47
48 for ld in res["items"]:
49 if ld["root_device_id"] == o.device_id:
50 o.of_id = ld["id"]
Matteo Scandolod44ca992018-05-17 15:02:10 -070051 o.dp_id = "of:" + Helpers.datapath_id_to_hex(ld["datapath_id"]) # convert to hex
Matteo Scandolo33523412018-04-12 15:21:13 -070052
Scott Baker09798d82019-01-17 08:34:59 -080053 # Note: If the device is administratively disabled, then it's likely we won't find a logical device for
54 # it. Only throw the exception for OLTs that are enabled.
55
56 if not o.of_id and not o.dp_id and o.admin_state == "ENABLED":
57 raise Exception("Can't find a logical device for device id: %s" % o.device_id)
Matteo Scandolo33523412018-04-12 15:21:13 -070058
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080059 def pull_records(self):
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -070060 log.debug("[OLT pull step] pulling OLT devices from VOLTHA")
Matteo Scandolo33523412018-04-12 15:21:13 -070061
62 try:
63 self.volt_service = VOLTService.objects.all()[0]
64 except IndexError:
65 log.warn('VOLTService not found')
66 return
67
Matteo Scandolod44ca992018-05-17 15:02:10 -070068 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
69 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
Matteo Scandolo33523412018-04-12 15:21:13 -070070
71 try:
Matteo Scandolo10928dd2018-10-16 17:49:03 -070072 r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port), timeout=1)
Matteo Scandolo33523412018-04-12 15:21:13 -070073
74 if r.status_code != 200:
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -070075 log.debug("[OLT pull step] It was not possible to fetch devices from VOLTHA")
Matteo Scandolo33523412018-04-12 15:21:13 -070076
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053077 # [SEBA-367] Handling blank response received from Voltha, Scenario occurs when voltha api is called while vcore service is re-starting
78 elif r.text:
79 # keeping only OLTs
80 devices = [d for d in r.json()["items"] if "olt" in d["type"]]
Matteo Scandolo33523412018-04-12 15:21:13 -070081
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053082 log.debug("[OLT pull step] received devices", olts=devices)
Matteo Scandolo33523412018-04-12 15:21:13 -070083
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053084 olts_in_voltha = self.create_or_update_olts(devices)
Matteo Scandolo33523412018-04-12 15:21:13 -070085
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053086 self.delete_olts(olts_in_voltha)
87 else:
88 log.debug("[OLT pull step] Blank response received")
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070089
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +053090 except (ValueError, TypeError), e:
91 log.warn("[OLT pull step] Invalid Json received in response from VOLTHA", reason=e)
92 return
Matteo Scandolo33523412018-04-12 15:21:13 -070093 except ConnectionError, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070094 log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
Matteo Scandolo33523412018-04-12 15:21:13 -070095 return
96 except InvalidURL, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070097 log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
Matteo Scandolo33523412018-04-12 15:21:13 -070098 return
99
100 def create_or_update_olts(self, olts):
101
102 updated_olts = []
103
104 for olt in olts:
Scott Bakerd01fae72018-12-11 12:27:27 -0800105 if olt["type"] == "simulated_olt":
106 [host, port] = ["172.17.0.1", "50060"]
Matteo Scandolo778fef72019-04-22 13:32:36 -0700107 elif "host_and_port" in olt:
Scott Bakerd01fae72018-12-11 12:27:27 -0800108 [host, port] = olt["host_and_port"].split(":")
Matteo Scandolo778fef72019-04-22 13:32:36 -0700109 elif "mac_address" in olt:
110 mac_address = olt["mac_address"]
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700111
Scott Bakerd01fae72018-12-11 12:27:27 -0800112 olt_ports = self.fetch_olt_ports(olt["id"])
113
114 try:
Matteo Scandolo778fef72019-04-22 13:32:36 -0700115 if "host_and_port" in olt:
116 model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0]
117 log.debug("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port)
118 elif "mac_address" in olt:
119 model = OLTDevice.objects.filter(device_type=olt["type"], mac_address=mac_address)[0]
120 log.debug("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], mac_address=mac_address)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700121
Matteo Scandolo33523412018-04-12 15:21:13 -0700122
123 if model.enacted < model.updated:
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -0700124 log.debug("[OLT pull step] Skipping pull on OLTDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700125 # if we are not updating the device we still need to pull ports
Scott Bakerd01fae72018-12-11 12:27:27 -0800126 if olt_ports:
127 self.create_or_update_ports(olt_ports, model)
Matteo Scandoloa0291a12018-08-21 10:13:18 -0700128 updated_olts.append(model)
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700129 continue
Matteo Scandolo33523412018-04-12 15:21:13 -0700130
131 except IndexError:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700132
Matteo Scandolo33523412018-04-12 15:21:13 -0700133 model = OLTDevice()
134 model.device_type = olt["type"]
135
136 if olt["type"] == "simulated_olt":
137 model.host = "172.17.0.1"
138 model.port = 50060
Matteo Scandolo778fef72019-04-22 13:32:36 -0700139 elif "host_and_port" in olt:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700140 [host, port] = olt["host_and_port"].split(":")
141 model.host = host
142 model.port = int(port)
Matteo Scandolo778fef72019-04-22 13:32:36 -0700143 log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port)
144 elif "mac_address" in olt:
145 model.mac_address = olt["mac_address"]
146 log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], mac_address=mac_address)
Matteo Scandolo33523412018-04-12 15:21:13 -0700147
Scott Bakerd01fae72018-12-11 12:27:27 -0800148 # there's no name in voltha, so make one up based on the id
149 model.name = "OLT-%s" % olt["id"]
150
151 nni_ports = [p for p in olt_ports if "ETHERNET_NNI" in p["type"]]
152 if not nni_ports:
153 log.warning("[OLT pull step] No NNI ports, so no way to determine uplink. Skipping.", device_type=olt["type"], host=host, port=port)
154 continue
155
156 # Exctract uplink from the first NNI port. This decision is arbitrary, we will worry about multiple
157 # NNI ports when that situation arises.
158 model.uplink = str(nni_ports[0]["port_no"])
159
Scott Baker09798d82019-01-17 08:34:59 -0800160 # Initial admin_state
161 model.admin_state = olt["admin_state"]
162
Matteo Scandolo33523412018-04-12 15:21:13 -0700163 # Adding feedback state to the device
164 model.device_id = olt["id"]
Matteo Scandolo33523412018-04-12 15:21:13 -0700165 model.oper_status = olt["oper_status"]
Matteo Scandolod6fce512018-10-16 10:35:29 -0700166 model.serial_number = olt['serial_number']
Matteo Scandolo33523412018-04-12 15:21:13 -0700167
168 model.volt_service = self.volt_service
169 model.volt_service_id = self.volt_service.id
170
171 # get logical device
172 OLTDevicePullStep.get_ids_from_logical_device(model)
173
Andy Bavier00c573c2019-02-08 16:19:11 -0700174 model.save_changed_fields()
Matteo Scandolo33523412018-04-12 15:21:13 -0700175
Scott Bakerd01fae72018-12-11 12:27:27 -0800176 if olt_ports:
177 self.create_or_update_ports(olt_ports, model)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700178
Matteo Scandolo33523412018-04-12 15:21:13 -0700179 updated_olts.append(model)
180
181 return updated_olts
182
Scott Bakerd01fae72018-12-11 12:27:27 -0800183 def fetch_olt_ports(self, olt_device_id):
184 """ Given an olt device_id, query voltha for the set of ports associated with that OLT.
185
186 Returns a list of port dictionaries, or None in case of error.
187 """
188
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700189 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
190 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
191
192 try:
Scott Bakerd01fae72018-12-11 12:27:27 -0800193 r = requests.get("%s:%s/api/v1/devices/%s/ports" % (voltha_url, voltha_port, olt_device_id), timeout=1)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700194
195 if r.status_code != 200:
Scott Bakerd01fae72018-12-11 12:27:27 -0800196 log.warn("[OLT pull step] It was not possible to fetch ports from VOLTHA for device %s" % olt_device_id,
197 status_code=r.status_code)
198 return None
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700199
200 ports = r.json()['items']
201
Andy Bavier00c573c2019-02-08 16:19:11 -0700202 log.debug("[OLT pull step] received ports", ports=ports, olt=olt_device_id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700203
Scott Bakerd01fae72018-12-11 12:27:27 -0800204 return ports
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700205
206 except ConnectionError, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700207 log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
Scott Bakerd01fae72018-12-11 12:27:27 -0800208 return None
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700209 except InvalidURL, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700210 log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
Scott Bakerd01fae72018-12-11 12:27:27 -0800211 return None
212
213 return None
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700214
215 def create_or_update_ports(self, ports, olt):
216 nni_ports = [p for p in ports if "ETHERNET_NNI" in p["type"]]
217 pon_ports = [p for p in ports if "PON_OLT" in p["type"]]
218
219 self.create_or_update_nni_port(nni_ports, olt)
220 self.create_or_update_pon_port(pon_ports, olt)
221
222 def create_or_update_pon_port(self, pon_ports, olt):
223
224 update_ports = []
225
226 for port in pon_ports:
227 try:
228 model = PONPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0]
Andy Bavier00c573c2019-02-08 16:19:11 -0700229 log.debug("[OLT pull step] PONPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700230 except IndexError:
231 model = PONPort()
232 model.port_no = port["port_no"]
233 model.olt_device_id = olt.id
234 model.name = port["label"]
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700235 log.debug("[OLT pull step] PONPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700236
237 model.admin_state = port["admin_state"]
238 model.oper_status = port["oper_status"]
Andy Bavier00c573c2019-02-08 16:19:11 -0700239 model.save_changed_fields()
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700240 update_ports.append(model)
241 return update_ports
242
243 def create_or_update_nni_port(self, nni_ports, olt):
244 update_ports = []
245
246 for port in nni_ports:
247 try:
248 model = NNIPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0]
249 model.xos_managed = False
Andy Bavier00c573c2019-02-08 16:19:11 -0700250 log.debug("[OLT pull step] NNIPort already exists, updating it", port_no=port["port_no"], olt_device_id=olt.id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700251 except IndexError:
252 model = NNIPort()
253 model.port_no = port["port_no"]
254 model.olt_device_id = olt.id
255 model.name = port["label"]
256 model.xos_managed = False
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700257 log.debug("[OLT pull step] NNIPort is new, creating it", port_no=port["port_no"], olt_device_id=olt.id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700258
259 model.admin_state = port["admin_state"]
260 model.oper_status = port["oper_status"]
Andy Bavier00c573c2019-02-08 16:19:11 -0700261 model.save_changed_fields()
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700262 update_ports.append(model)
263 return update_ports
Matteo Scandolo33523412018-04-12 15:21:13 -0700264
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700265 def delete_olts(self, olts_in_voltha):
Matteo Scandoloa0291a12018-08-21 10:13:18 -0700266
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700267 olts_id_in_voltha = [m.device_id for m in olts_in_voltha]
Matteo Scandolo33523412018-04-12 15:21:13 -0700268
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700269 xos_olts = OLTDevice.objects.all()
Matteo Scandolo33523412018-04-12 15:21:13 -0700270
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700271 deleted_in_voltha = [o for o in xos_olts if o.device_id not in olts_id_in_voltha]
Matteo Scandolo33523412018-04-12 15:21:13 -0700272
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700273 for model in deleted_in_voltha:
Matteo Scandolo33523412018-04-12 15:21:13 -0700274
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700275 if model.enacted < model.updated:
276 # DO NOT delete a model that is being processed
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -0700277 log.debug("[OLT pull step] device is not present in VOLTHA, skipping deletion as sync is in progress", device_id=o.device_id,
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700278 name=o.name)
279 continue
280
Matteo Scandolo27d2fbb2018-09-07 14:02:53 -0700281 log.debug("[OLT pull step] deleting device as it's not present in VOLTHA", device_id=o.device_id, name=o.name)
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700282 model.delete()