blob: 760471d638337420dbb408cabca03c72f72d7b9b [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import structlog
18
19from common.pon_resource_manager.resource_manager import PONResourceManager
20from voltha.registry import registry
21from voltha.core.config.config_backend import ConsulStore
22from voltha.core.config.config_backend import EtcdStore
23from voltha.adapters.openolt.openolt_flow_mgr import *
24
25from voltha.adapters.openolt.protos import openolt_pb2
26from voltha.adapters.openolt.openolt_platform import OpenOltPlatform
27
28
29class OpenOltResourceMgr(object):
30 BASE_PATH_KV_STORE = "service/voltha/openolt/{}" # service/voltha/openolt/<device_id>
31
32 def __init__(self, device_id, host_and_port, extra_args, device_info):
33 self.log = structlog.get_logger(id=device_id,
34 ip=host_and_port)
35 self.device_id = device_id
36 self.host_and_port = host_and_port
37 self.extra_args = extra_args
38 self.device_info = device_info
39 self.args = registry('main').get_args()
40
41 # KV store's IP Address and PORT
42 if self.args.backend == 'etcd':
43 host, port = self.args.etcd.split(':', 1)
44 self.kv_store = EtcdStore(host, port,
45 OpenOltResourceMgr.BASE_PATH_KV_STORE.format(device_id))
46 elif self.args.backend == 'consul':
47 host, port = self.args.consul.split(':', 1)
48 self.kv_store = ConsulStore(host, port,
49 OpenOltResourceMgr.BASE_PATH_KV_STORE.format(device_id))
50 else:
51 self.log.error('Invalid-backend')
52 raise Exception("Invalid-backend-for-kv-store")
53
54 ranges = dict()
55 resource_mgrs_by_tech = dict()
56 self.resource_mgrs = dict()
57
58 # If a legacy driver returns protobuf without any ranges,s synthesize one from
59 # the legacy global per-device informaiton. This, in theory, is temporary until
60 # the legacy drivers are upgrade to support pool ranges.
61 if len(self.device_info.ranges) == 0:
62 arange = self.device_info.ranges.add()
63 arange.technology = self.device_info.technology
64 arange.intf_ids.extend(range(0, device_info.pon_ports))
65
66 pool = arange.pools.add()
67 pool.type = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.ONU_ID
68 pool.start = self.device_info.onu_id_start
69 pool.end = self.device_info.onu_id_end
70 pool.sharing = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.DEDICATED_PER_INTF
71
72 pool = arange.pools.add()
73 pool.type = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.ALLOC_ID
74 pool.start = self.device_info.alloc_id_start
75 pool.end = self.device_info.alloc_id_end
76 pool.sharing = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH
77
78 pool = arange.pools.add()
79 pool.type = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.GEMPORT_ID
80 pool.start = self.device_info.gemport_id_start
81 pool.end = self.device_info.gemport_id_end
82 pool.sharing = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH
83
84 pool = arange.pools.add()
85 pool.type = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.FLOW_ID
86 pool.start = self.device_info.flow_id_start
87 pool.end = self.device_info.flow_id_end
88 pool.sharing = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH
89
90 # Create a separate Resource Manager instance for each range. This assumes that
91 # each technology is represented by only a single range
92 global_resource_mgr = None
93 for arange in self.device_info.ranges:
94 technology = arange.technology
95 self.log.info("device-info", technology=technology)
96 ranges[technology] = arange
97 extra_args = self.extra_args + ' ' + PONResourceManager.OLT_MODEL_ARG + ' {}'.format(self.device_info.model)
98 resource_mgr = PONResourceManager(technology,
99 extra_args, self.device_id, self.args.backend, host, port)
100 resource_mgrs_by_tech[technology] = resource_mgr
101 if global_resource_mgr is None:
102 global_resource_mgr = resource_mgr
103 for intf_id in arange.intf_ids:
104 self.resource_mgrs[intf_id] = resource_mgrs_by_tech[technology]
105 self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
106
107 # After we have initialized resource ranges, initialize the
108 # resource pools accordingly.
109 for technology, resource_mgr in resource_mgrs_by_tech.iteritems():
110 resource_mgr.init_device_resource_pool()
111
112 def __del__(self):
113 self.log.info("clearing-device-resource-pool")
114 for key, resource_mgr in self.resource_mgrs.iteritems():
115 resource_mgr.clear_device_resource_pool()
116
117 def assert_pon_id_limit(self, pon_intf_id):
118 assert pon_intf_id in self.resource_mgrs
119
120 def assert_onu_id_limit(self, pon_intf_id, onu_id):
121 self.assert_pon_id_limit(pon_intf_id)
122 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
123
124 @property
125 def max_uni_id_per_onu(self):
126 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
127
128 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
129 self.assert_onu_id_limit(pon_intf_id, onu_id)
130 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
131
132 def get_onu_id(self, pon_intf_id):
133 onu_id = self.resource_mgrs[pon_intf_id].get_resource_id(
134 pon_intf_id, PONResourceManager.ONU_ID, 1)
135
136 if onu_id is not None:
137 pon_intf_onu_id = (pon_intf_id, onu_id)
138 self.resource_mgrs[pon_intf_id].init_resource_map(
139 pon_intf_onu_id)
140
141 return onu_id
142
143 def get_flow_id(self, pon_intf_id, onu_id, uni_id, flow_store_cookie,
144 flow_category=None):
145 pon_intf_onu_id = (pon_intf_id, onu_id, uni_id)
146 try:
147 flow_ids = self.resource_mgrs[pon_intf_id]. \
148 get_current_flow_ids_for_onu(pon_intf_onu_id)
149 if flow_ids is not None:
150 for flow_id in flow_ids:
151 flows = self.get_flow_id_info(pon_intf_id, onu_id, uni_id, flow_id)
152 assert (isinstance(flows, list))
153 for flow in flows:
154
155 if flow_category is not None and \
156 'flow_category' in flow and \
157 flow['flow_category'] == flow_category:
158 return flow_id
159 if flow['flow_store_cookie'] == flow_store_cookie:
160 return flow_id
161 except Exception as e:
162 self.log.error("error-retrieving-flow-info", e=e)
163
164 flow_id = self.resource_mgrs[pon_intf_id].get_resource_id(
165 pon_intf_onu_id[0], PONResourceManager.FLOW_ID)
166 if flow_id is not None:
167 self.resource_mgrs[pon_intf_id].update_flow_id_for_onu(
168 pon_intf_onu_id, flow_id
169 )
170
171 return flow_id
172
173 def get_flow_id_info(self, pon_intf_id, onu_id, uni_id, flow_id):
174 pon_intf_onu_id = (pon_intf_id, onu_id, uni_id)
175 return self.resource_mgrs[pon_intf_id].get_flow_id_info(pon_intf_onu_id, flow_id)
176
177 def get_current_flow_ids_for_uni(self, pon_intf_id, onu_id, uni_id):
178 pon_intf_onu_id = (pon_intf_id, onu_id, uni_id)
179 return self.resource_mgrs[pon_intf_id].get_current_flow_ids_for_onu(pon_intf_onu_id)
180
181 def update_flow_id_info_for_uni(self, pon_intf_id, onu_id, uni_id, flow_id, flow_data):
182 pon_intf_onu_id = (pon_intf_id, onu_id, uni_id)
183 return self.resource_mgrs[pon_intf_id].update_flow_id_info_for_onu(
184 pon_intf_onu_id, flow_id, flow_data)
185
186 def get_alloc_id(self, pon_intf_onu_id):
187 # Derive the pon_intf from the pon_intf_onu_id tuple
188 pon_intf = pon_intf_onu_id[0]
189 alloc_id_list = self.resource_mgrs[pon_intf].get_current_alloc_ids_for_onu(
190 pon_intf_onu_id)
191
192 if alloc_id_list and len(alloc_id_list) > 0:
193 # Since we support only one alloc_id for the ONU at the moment,
194 # return the first alloc_id in the list, if available, for that
195 # ONU.
196 return alloc_id_list[0]
197
198 alloc_id = self.resource_mgrs[pon_intf].get_resource_id(
199 pon_intf_id=pon_intf,
200 resource_type=PONResourceManager.ALLOC_ID,
201 num_of_id=1
202 )
203 if alloc_id is None:
204 self.log.error("no-alloc-id-available")
205 return None
206
207 # update the resource map on KV store with the list of alloc_id
208 # allocated for the pon_intf_onu_id tuple
209 self.resource_mgrs[pon_intf].update_alloc_ids_for_onu(pon_intf_onu_id,
210 list(alloc_id))
211
212 return alloc_id
213
214 def get_current_gemport_ids_for_onu(self, pon_intf_onu_id):
215 pon_intf_id = pon_intf_onu_id[0]
216 return self.resource_mgrs[pon_intf_id].get_current_gemport_ids_for_onu(pon_intf_onu_id)
217
218 def get_current_alloc_ids_for_onu(self, pon_intf_onu_id):
219 pon_intf_id = pon_intf_onu_id[0]
220 alloc_ids = self.resource_mgrs[pon_intf_id].get_current_alloc_ids_for_onu(pon_intf_onu_id)
221 if alloc_ids is None:
222 return None
223 # We support only one tcont at the moment
224 return alloc_ids[0]
225
226 def update_gemports_ponport_to_onu_map_on_kv_store(self, gemport_list, pon_port, onu_id, uni_id):
227 for gemport in gemport_list:
228 pon_intf_gemport = (pon_port, gemport)
229 # This information is used when packet_indication is received and
230 # we need to derive the ONU Id for which the packet arrived based
231 # on the pon_intf and gemport available in the packet_indication
232 self.kv_store[str(pon_intf_gemport)] = ' '.join(map(str, (onu_id, uni_id)))
233
234 def get_onu_uni_from_ponport_gemport(self, pon_port, gemport):
235 pon_intf_gemport = (pon_port, gemport)
236 return tuple(map(int, self.kv_store[str(pon_intf_gemport)].split(' ')))
237
238 def get_gemport_id(self, pon_intf_onu_id, num_of_id=1):
239 # Derive the pon_intf and onu_id from the pon_intf_onu_id tuple
240 pon_intf = pon_intf_onu_id[0]
241 onu_id = pon_intf_onu_id[1]
242 uni_id = pon_intf_onu_id[2]
243 assert False, 'unused function'
244
245 gemport_id_list = self.resource_mgrs[pon_intf].get_current_gemport_ids_for_onu(
246 pon_intf_onu_id)
247 if gemport_id_list and len(gemport_id_list) > 0:
248 return gemport_id_list
249
250 gemport_id_list = self.resource_mgrs[pon_intf].get_resource_id(
251 pon_intf_id=pon_intf,
252 resource_type=PONResourceManager.GEMPORT_ID,
253 num_of_id=num_of_id
254 )
255
256 if gemport_id_list and len(gemport_id_list) == 0:
257 self.log.error("no-gemport-id-available")
258 return None
259
260 # update the resource map on KV store with the list of gemport_id
261 # allocated for the pon_intf_onu_id tuple
262 self.resource_mgrs[pon_intf].update_gemport_ids_for_onu(pon_intf_onu_id,
263 gemport_id_list)
264
265 self.update_gemports_ponport_to_onu_map_on_kv_store(gemport_id_list,
266 pon_intf, onu_id, uni_id)
267 return gemport_id_list
268
269 def free_onu_id(self, pon_intf_id, onu_id):
270 _ = self.resource_mgrs[pon_intf_id].free_resource_id(
271 pon_intf_id, PONResourceManager.ONU_ID, onu_id)
272
273 pon_intf_onu_id = (pon_intf_id, onu_id)
274 self.resource_mgrs[pon_intf_id].remove_resource_map(
275 pon_intf_onu_id)
276
277 def free_flow_id_for_uni(self, pon_intf_id, onu_id, uni_id, flow_id):
278 self.resource_mgrs[pon_intf_id].free_resource_id(
279 pon_intf_id, PONResourceManager.FLOW_ID, flow_id)
280 pon_intf_onu_id = (pon_intf_id, onu_id, uni_id)
281 self.resource_mgrs[pon_intf_id].update_flow_id_for_onu(pon_intf_onu_id,
282 flow_id, False)
283 self.resource_mgrs[pon_intf_id].remove_flow_id_info(pon_intf_onu_id,
284 flow_id)
285
286 def free_pon_resources_for_onu(self, pon_intf_id_onu_id):
287
288 pon_intf_id = pon_intf_id_onu_id[0]
289 onu_id = pon_intf_id_onu_id[1]
290 alloc_ids = \
291 self.resource_mgrs[pon_intf_id].get_current_alloc_ids_for_onu(pon_intf_id_onu_id)
292 self.resource_mgrs[pon_intf_id].free_resource_id(pon_intf_id,
293 PONResourceManager.ALLOC_ID,
294 alloc_ids)
295
296 gemport_ids = \
297 self.resource_mgrs[pon_intf_id].get_current_gemport_ids_for_onu(pon_intf_id_onu_id)
298 self.resource_mgrs[pon_intf_id].free_resource_id(pon_intf_id,
299 PONResourceManager.GEMPORT_ID,
300 gemport_ids)
301
302 flow_ids = \
303 self.resource_mgrs[pon_intf_id].get_current_flow_ids_for_onu(pon_intf_id_onu_id)
304 self.resource_mgrs[pon_intf_id].free_resource_id(pon_intf_id,
305 PONResourceManager.FLOW_ID,
306 flow_ids)
307
308 self.resource_mgrs[pon_intf_id].free_resource_id(pon_intf_id,
309 PONResourceManager.ONU_ID,
310 onu_id)
311
312 # Clear resource map associated with (pon_intf_id, gemport_id) tuple.
313 self.resource_mgrs[pon_intf_id].remove_resource_map(pon_intf_id_onu_id)
314
315 # Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
316 for gemport_id in gemport_ids:
317 del self.kv_store[str((pon_intf_id, gemport_id))]
318
319 def initialize_device_resource_range_and_pool(self, resource_mgr, global_resource_mgr, arange):
320 self.log.info("resource-range-pool-init", technology=resource_mgr.technology)
321
322 # first load from KV profiles
323 status = resource_mgr.init_resource_ranges_from_kv_store()
324 if not status:
325 self.log.info("failed-to-load-resource-range-from-kv-store", technology=resource_mgr.technology)
326
327 # Then apply device specific information. If KV doesn't exist
328 # or is broader than the device, the device's informationw ill
329 # dictate the range limits
330 self.log.info("using-device-info-to-init-pon-resource-ranges", technology=resource_mgr.technology)
331
332 onu_id_start = self.device_info.onu_id_start
333 onu_id_end = self.device_info.onu_id_end
334 onu_id_shared = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.DEDICATED_PER_INTF
335 onu_id_shared_pool_id = None
336 alloc_id_start = self.device_info.alloc_id_start
337 alloc_id_end = self.device_info.alloc_id_end
338 alloc_id_shared = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH # TODO EdgeCore/BAL limitation
339 alloc_id_shared_pool_id = None
340 gemport_id_start = self.device_info.gemport_id_start
341 gemport_id_end = self.device_info.gemport_id_end
342 gemport_id_shared = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH # TODO EdgeCore/BAL limitation
343 gemport_id_shared_pool_id = None
344 flow_id_start = self.device_info.flow_id_start
345 flow_id_end = self.device_info.flow_id_end
346 flow_id_shared = openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH # TODO EdgeCore/BAL limitation
347 flow_id_shared_pool_id = None
348
349 global_pool_id = 0
350 for first_intf_pool_id in arange.intf_ids:
351 break
352
353 for pool in arange.pools:
354 shared_pool_id = global_pool_id if pool.sharing == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH else \
355 first_intf_pool_id if pool.sharing == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_SAME_TECH else \
356 None
357
358 if pool.type == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.ONU_ID:
359 onu_id_start = pool.start
360 onu_id_end = pool.end
361 onu_id_shared = pool.sharing
362 onu_id_shared_pool_id = shared_pool_id
363 elif pool.type == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.ALLOC_ID:
364 alloc_id_start = pool.start
365 alloc_id_end = pool.end
366 alloc_id_shared = pool.sharing
367 alloc_id_shared_pool_id = shared_pool_id
368 elif pool.type == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.GEMPORT_ID:
369 gemport_id_start = pool.start
370 gemport_id_end = pool.end
371 gemport_id_shared = pool.sharing
372 gemport_id_shared_pool_id = shared_pool_id
373 elif pool.type == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.FLOW_ID:
374 flow_id_start = pool.start
375 flow_id_end = pool.end
376 flow_id_shared = pool.sharing
377 flow_id_shared_pool_id = shared_pool_id
378
379 self.log.info("device-info-init", technology=arange.technology,
380 onu_id_start=onu_id_start, onu_id_end=onu_id_end, onu_id_shared_pool_id=onu_id_shared_pool_id,
381 alloc_id_start=alloc_id_start, alloc_id_end=alloc_id_end,
382 alloc_id_shared_pool_id=alloc_id_shared_pool_id,
383 gemport_id_start=gemport_id_start, gemport_id_end=gemport_id_end,
384 gemport_id_shared_pool_id=gemport_id_shared_pool_id,
385 flow_id_start_idx=flow_id_start,
386 flow_id_end_idx=flow_id_end,
387 flow_id_shared_pool_id=flow_id_shared_pool_id,
388 intf_ids=arange.intf_ids,
389 uni_id_start_idx=0,
390 uni_id_end_idx=self.max_uni_id_per_onu)
391
392 resource_mgr.init_default_pon_resource_ranges(
393 onu_id_start_idx=onu_id_start,
394 onu_id_end_idx=onu_id_end,
395 onu_id_shared_pool_id=onu_id_shared_pool_id,
396 alloc_id_start_idx=alloc_id_start,
397 alloc_id_end_idx=alloc_id_end,
398 alloc_id_shared_pool_id=alloc_id_shared_pool_id,
399 gemport_id_start_idx=gemport_id_start,
400 gemport_id_end_idx=gemport_id_end,
401 gemport_id_shared_pool_id=gemport_id_shared_pool_id,
402 flow_id_start_idx=flow_id_start,
403 flow_id_end_idx=flow_id_end,
404 flow_id_shared_pool_id=flow_id_shared_pool_id,
405 uni_id_start_idx=0, uni_id_end_idx=self.max_uni_id_per_onu,
406 num_of_pon_ports=self.device_info.pon_ports,
407 intf_ids=arange.intf_ids
408 )
409
410 # For global sharing, make sure to refresh both local and global resource manager instances' range
411 if global_resource_mgr is not self:
412 if onu_id_shared == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH:
413 global_resource_mgr.update_ranges(onu_id_start_idx=onu_id_start, onu_id_end_idx=onu_id_end)
414 resource_mgr.update_ranges(onu_id_start_idx=onu_id_start, onu_id_end_idx=onu_id_end,
415 onu_id_shared_resource_mgr=global_resource_mgr)
416
417 if alloc_id_shared == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH:
418 global_resource_mgr.update_ranges(alloc_id_start_idx=alloc_id_start, alloc_id_end_idx=alloc_id_end)
419 resource_mgr.update_ranges(alloc_id_start_idx=alloc_id_start, alloc_id_end_idx=alloc_id_end,
420 alloc_id_shared_resource_mgr=global_resource_mgr)
421
422 if gemport_id_shared == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH:
423 global_resource_mgr.update_ranges(gemport_id_start_idx=gemport_id_start,
424 gemport_id_end_idx=gemport_id_end)
425 resource_mgr.update_ranges(gemport_id_start_idx=gemport_id_start, gemport_id_end_idx=gemport_id_end,
426 gemport_id_shared_resource_mgr=global_resource_mgr)
427
428 if flow_id_shared == openolt_pb2.DeviceInfo.DeviceResourceRanges.Pool.SHARED_BY_ALL_INTF_ALL_TECH:
429 global_resource_mgr.update_ranges(flow_id_start_idx=flow_id_start,
430 flow_id_end_idx=flow_id_end)
431 resource_mgr.update_ranges(flow_id_start_idx=flow_id_start, flow_id_end_idx=flow_id_end,
432 flow_id_shared_resource_mgr=global_resource_mgr)
433
434 # Make sure loaded range fits the platform bit encoding ranges
435 resource_mgr.update_ranges(uni_id_start_idx=0, uni_id_end_idx=OpenOltPlatform.MAX_UNIS_PER_ONU-1)