blob: e377f1bc13df9ba416094015e10f10742df85658 [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 Scandolo33523412018-04-12 15:21:13 -070016from synchronizers.new_base.modelaccessor import model_accessor, OLTDevice, VOLTService
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 Scandolo4a8b4d62018-03-06 17:18:46 -080025log = create_logger(Config().get('logging'))
26
27class OLTDevicePullStep(PullStep):
28 def __init__(self):
29 super(OLTDevicePullStep, self).__init__(observed_model=OLTDevice)
30
Matteo Scandolo33523412018-04-12 15:21:13 -070031 # NOTE move helpers where they can be loaded by multiple modules?
32 @staticmethod
33 def format_url(url):
34 if 'http' in url:
35 return url
36 else:
37 return 'http://%s' % url
38
39 @staticmethod
40 def get_voltha_info(olt_service):
41 return {
42 'url': OLTDevicePullStep.format_url(olt_service.voltha_url),
43 'user': olt_service.voltha_user,
44 'pass': olt_service.voltha_pass
45 }
46
47 @staticmethod
48 def datapath_id_to_hex(id):
49 if isinstance(id, basestring):
50 id = int(id)
51 return "{0:0{1}x}".format(id, 16)
52
53 @staticmethod
54 def get_ids_from_logical_device(o):
55 voltha_url = OLTDevicePullStep.get_voltha_info(o.volt_service)['url']
56
57 r = requests.get(voltha_url + "/api/v1/logical_devices")
58
59 if r.status_code != 200:
60 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text)
61
62 res = r.json()
63
64 for ld in res["items"]:
65 if ld["root_device_id"] == o.device_id:
66 o.of_id = ld["id"]
67 o.dp_id = "of:" + OLTDevicePullStep.datapath_id_to_hex(ld["datapath_id"]) # convert to hex
68 return o
69
70 raise Exception("Can't find a logical device for device id: %s" % o.device_id)
71 # end note
72
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080073 def pull_records(self):
74 log.info("pulling OLT devices from VOLTHA")
Matteo Scandolo33523412018-04-12 15:21:13 -070075
76 try:
77 self.volt_service = VOLTService.objects.all()[0]
78 except IndexError:
79 log.warn('VOLTService not found')
80 return
81
82 voltha_url = OLTDevicePullStep.get_voltha_info(self.volt_service)['url']
83
84 try:
85 devices = []
86 r = requests.get(voltha_url + "/api/v1/devices")
87
88 if r.status_code != 200:
89 log.info("It was not possible to fetch devices from VOLTHA")
90
91 # keeping only OLTs
92 devices = [d for d in r.json()["items"] if "olt" in d["type"]]
93
94 log.debug("received devices", olts=devices)
95
96 # TODO
97 # [X] for each device
98 # [X] check if exists, if not save it
99 # [X] if exists and enacted > updated it has already been sync'ed
100 # [X] keep track of the updated OLTs
101 # delete OLTS as OLTDevice.objects.all() - updated OLTs
102
103 if r.status_code != 200:
104 log.info("It was not possible to fetch devices from VOLTHA")
105
106 olts_in_voltha = self.create_or_update_olts(devices)
107
108 except ConnectionError, e:
109 log.warn("It was not possible to connect to VOLTHA", reason=e)
110 return
111 except InvalidURL, e:
112 log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e)
113 return
114
115 def create_or_update_olts(self, olts):
116
117 updated_olts = []
118
119 for olt in olts:
120 try:
121 if olt["type"] == "simulated_olt":
122 [host, port] = ["172.17.0.1", "50060"]
123 else:
124 [host, port] = olt["host_and_port"].split(":")
125 model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0]
126 log.debug("OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port)
127
128 if model.enacted < model.updated:
129 log.info("Skipping pull on OLTDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated)
130 return
131
132 except IndexError:
133 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
139
140 log.debug("OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port)
141
142 # Adding feedback state to the device
143 model.device_id = olt["id"]
144 model.admin_state = olt["admin_state"]
145 model.oper_status = olt["oper_status"]
146
147 model.volt_service = self.volt_service
148 model.volt_service_id = self.volt_service.id
149
150 # get logical device
151 OLTDevicePullStep.get_ids_from_logical_device(model)
152
153 model.save()
154
155 updated_olts.append(model)
156
157 return updated_olts
158
159
160
161
162
163