blob: acb6a10998c91bd91ded827e682e134c9605407a [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
15from synchronizers.new_base.pullstep import PullStep
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070016from synchronizers.new_base.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):
33 def __init__(self):
34 super(OLTDevicePullStep, self).__init__(observed_model=OLTDevice)
35
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 Scandolo19466a02018-05-16 17:43:39 -070041 r = requests.get("%s:%s/api/v1/logical_devices" % (voltha_url, voltha_port))
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 return o
53
54 raise Exception("Can't find a logical device for device id: %s" % o.device_id)
Matteo Scandolo33523412018-04-12 15:21:13 -070055
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080056 def pull_records(self):
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070057 log.info("[OLT pull step] pulling OLT devices from VOLTHA")
Matteo Scandolo33523412018-04-12 15:21:13 -070058
59 try:
60 self.volt_service = VOLTService.objects.all()[0]
61 except IndexError:
62 log.warn('VOLTService not found')
63 return
64
Matteo Scandolod44ca992018-05-17 15:02:10 -070065 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
66 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
Matteo Scandolo33523412018-04-12 15:21:13 -070067
68 try:
Matteo Scandolo19466a02018-05-16 17:43:39 -070069 r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port))
Matteo Scandolo33523412018-04-12 15:21:13 -070070
71 if r.status_code != 200:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070072 log.info("[OLT pull step] It was not possible to fetch devices from VOLTHA")
Matteo Scandolo33523412018-04-12 15:21:13 -070073
74 # keeping only OLTs
75 devices = [d for d in r.json()["items"] if "olt" in d["type"]]
76
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070077 log.debug("[OLT pull step] received devices", olts=devices)
Matteo Scandolo33523412018-04-12 15:21:13 -070078
Matteo Scandolo33523412018-04-12 15:21:13 -070079 olts_in_voltha = self.create_or_update_olts(devices)
80
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070081 self.delete_olts(olts_in_voltha)
82
83
Matteo Scandolo33523412018-04-12 15:21:13 -070084 except ConnectionError, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070085 log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
Matteo Scandolo33523412018-04-12 15:21:13 -070086 return
87 except InvalidURL, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -070088 log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
Matteo Scandolo33523412018-04-12 15:21:13 -070089 return
90
91 def create_or_update_olts(self, olts):
92
93 updated_olts = []
94
95 for olt in olts:
96 try:
97 if olt["type"] == "simulated_olt":
98 [host, port] = ["172.17.0.1", "50060"]
99 else:
100 [host, port] = olt["host_and_port"].split(":")
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700101
Matteo Scandolo33523412018-04-12 15:21:13 -0700102 model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700103
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700104 log.debug("[OLT pull step] OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port)
Matteo Scandolo33523412018-04-12 15:21:13 -0700105
106 if model.enacted < model.updated:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700107 log.info("[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 -0700108 # if we are not updating the device we still need to pull ports
109 self.fetch_olt_ports(model)
Matteo Scandoloa0291a12018-08-21 10:13:18 -0700110 updated_olts.append(model)
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700111 continue
Matteo Scandolo33523412018-04-12 15:21:13 -0700112
113 except IndexError:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700114
Matteo Scandolo33523412018-04-12 15:21:13 -0700115 model = OLTDevice()
116 model.device_type = olt["type"]
117
118 if olt["type"] == "simulated_olt":
119 model.host = "172.17.0.1"
120 model.port = 50060
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700121 else:
122 [host, port] = olt["host_and_port"].split(":")
123 model.host = host
124 model.port = int(port)
Matteo Scandolo33523412018-04-12 15:21:13 -0700125
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700126 log.debug("[OLT pull step] OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port)
Matteo Scandolo33523412018-04-12 15:21:13 -0700127
128 # Adding feedback state to the device
129 model.device_id = olt["id"]
130 model.admin_state = olt["admin_state"]
131 model.oper_status = olt["oper_status"]
132
133 model.volt_service = self.volt_service
134 model.volt_service_id = self.volt_service.id
135
136 # get logical device
137 OLTDevicePullStep.get_ids_from_logical_device(model)
138
139 model.save()
140
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700141 self.fetch_olt_ports(model)
142
Matteo Scandolo33523412018-04-12 15:21:13 -0700143 updated_olts.append(model)
144
145 return updated_olts
146
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700147 def fetch_olt_ports(self, olt):
148 voltha_url = Helpers.get_voltha_info(self.volt_service)['url']
149 voltha_port = Helpers.get_voltha_info(self.volt_service)['port']
150
151 try:
152 r = requests.get("%s:%s/api/v1/devices/%s/ports" % (voltha_url, voltha_port, olt.device_id))
153
154 if r.status_code != 200:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700155 log.info("[OLT pull step] It was not possible to fetch ports from VOLTHA for device %s" % olt.device_id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700156
157 ports = r.json()['items']
158
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700159 log.debug("[OLT pull step] received ports", ports=ports, olt=olt.device_id)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700160
161 self.create_or_update_ports(ports, olt)
162
163 except ConnectionError, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700164 log.warn("[OLT pull step] It was not possible to connect to VOLTHA", reason=e)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700165 return
166 except InvalidURL, e:
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700167 log.warn("[OLT pull step] VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700168 return
169 return
170
171 def create_or_update_ports(self, ports, olt):
172 nni_ports = [p for p in ports if "ETHERNET_NNI" in p["type"]]
173 pon_ports = [p for p in ports if "PON_OLT" in p["type"]]
174
175 self.create_or_update_nni_port(nni_ports, olt)
176 self.create_or_update_pon_port(pon_ports, olt)
177
178 def create_or_update_pon_port(self, pon_ports, olt):
179
180 update_ports = []
181
182 for port in pon_ports:
183 try:
184 model = PONPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0]
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700185 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 -0700186 except IndexError:
187 model = PONPort()
188 model.port_no = port["port_no"]
189 model.olt_device_id = olt.id
190 model.name = port["label"]
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700191 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 -0700192
193 model.admin_state = port["admin_state"]
194 model.oper_status = port["oper_status"]
195 model.save()
196 update_ports.append(model)
197 return update_ports
198
199 def create_or_update_nni_port(self, nni_ports, olt):
200 update_ports = []
201
202 for port in nni_ports:
203 try:
204 model = NNIPort.objects.filter(port_no=port["port_no"], olt_device_id=olt.id)[0]
205 model.xos_managed = False
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700206 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 -0700207 except IndexError:
208 model = NNIPort()
209 model.port_no = port["port_no"]
210 model.olt_device_id = olt.id
211 model.name = port["label"]
212 model.xos_managed = False
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700213 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 -0700214
215 model.admin_state = port["admin_state"]
216 model.oper_status = port["oper_status"]
217 model.save()
218 update_ports.append(model)
219 return update_ports
Matteo Scandolo33523412018-04-12 15:21:13 -0700220
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700221 def delete_olts(self, olts_in_voltha):
Matteo Scandoloa0291a12018-08-21 10:13:18 -0700222
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700223 olts_id_in_voltha = [m.device_id for m in olts_in_voltha]
Matteo Scandolo33523412018-04-12 15:21:13 -0700224
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700225 xos_olts = OLTDevice.objects.all()
Matteo Scandolo33523412018-04-12 15:21:13 -0700226
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700227 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 -0700228
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700229 for model in deleted_in_voltha:
Matteo Scandolo33523412018-04-12 15:21:13 -0700230
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700231 if model.enacted < model.updated:
232 # DO NOT delete a model that is being processed
233 log.info("[OLT pull step] device is not present in VOLTHA, skipping deletion as sync is in progress", device_id=o.device_id,
234 name=o.name)
235 continue
236
237 log.info("[OLT pull step] deleting device as it's not present in VOLTHA", device_id=o.device_id, name=o.name)
238 model.delete()