blob: 682b62caf6b4628524f2df620fb33537cd58df54 [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
17"""
18Openolt adapter.
19"""
William Kurkian6f436d02019-02-06 16:25:01 -050020import structlog
Matt Jeanneret7906d232019-02-14 14:57:38 -050021
22from zope.interface import implementer
William Kurkian6f436d02019-02-06 16:25:01 -050023from twisted.internet import reactor
Matt Jeanneret7906d232019-02-14 14:57:38 -050024from pyvoltha.adapters.iadapter import IAdapterInterface
William Kurkian44cd7bb2019-02-11 16:39:12 -050025from pyvoltha.common.utils.registry import registry
William Kurkian8b1690c2019-03-04 16:53:22 -050026from voltha_protos.common_pb2 import LogLevel
27from voltha_protos.common_pb2 import OperationResp
Matt Jeannerete33a7092019-03-12 21:54:14 -040028from voltha_protos.device_pb2 import DeviceType, DeviceTypes
William Kurkian8b1690c2019-03-04 16:53:22 -050029from voltha_protos.adapter_pb2 import Adapter
30from voltha_protos.adapter_pb2 import AdapterConfig
William Kurkian33508752019-02-12 14:56:06 -050031from openolt_flow_mgr import OpenOltFlowMgr
32from openolt_alarms import OpenOltAlarmMgr
33from openolt_statistics import OpenOltStatisticsMgr
34from openolt_bw import OpenOltBW
35from openolt_platform import OpenOltPlatform
36from openolt_resource_manager import OpenOltResourceMgr
37from openolt_device import OpenoltDevice
Matt Jeanneret9fd36df2019-02-14 19:14:36 -050038
William Kurkian6f436d02019-02-06 16:25:01 -050039log = structlog.get_logger()
William Kurkianfefd4642019-02-07 15:30:03 -050040OpenOltDefaults = {
41 'support_classes': {
42 'platform': OpenOltPlatform,
43 'resource_mgr': OpenOltResourceMgr,
44 'flow_mgr': OpenOltFlowMgr,
45 'alarm_mgr': OpenOltAlarmMgr,
46 'stats_mgr': OpenOltStatisticsMgr,
47 'bw_mgr': OpenOltBW
48 }
49}
William Kurkian6f436d02019-02-06 16:25:01 -050050
William Kurkian6f436d02019-02-06 16:25:01 -050051
Matt Jeanneret7906d232019-02-14 14:57:38 -050052@implementer(IAdapterInterface)
53class OpenoltAdapter(object):
William Kurkian6f436d02019-02-06 16:25:01 -050054 name = 'openolt'
55
56 supported_device_types = [
57 DeviceType(
58 id=name,
59 adapter=name,
60 accepts_bulk_flow_update=True,
61 accepts_add_remove_flow_updates=True
62 )
63 ]
64
65 # System Init Methods #
66 def __init__(self, core_proxy, adapter_proxy, config):
Matt Jeanneretbad3d982019-03-11 16:06:10 -040067 self.core_proxy = core_proxy
William Kurkian6f436d02019-02-06 16:25:01 -050068 self.adapter_proxy = adapter_proxy
William Kurkian6f436d02019-02-06 16:25:01 -050069 self.config = config
70 self.descriptor = Adapter(
71 id=self.name,
72 vendor='OLT white box vendor',
73 version='0.1',
Matt Jeanneret7906d232019-02-14 14:57:38 -050074 config=AdapterConfig(log_level=LogLevel.INFO)
William Kurkian6f436d02019-02-06 16:25:01 -050075 )
Matt Jeanneretbad3d982019-03-11 16:06:10 -040076 log.debug('openolt.__init__', core_proxy=core_proxy, adapter_proxy=adapter_proxy)
William Kurkian6f436d02019-02-06 16:25:01 -050077 self.devices = dict() # device_id -> OpenoltDevice()
78 self.interface = registry('main').get_args().interface
79 self.logical_device_id_to_root_device_id = dict()
80 self.num_devices = 0
81
82 def start(self):
83 log.info('started', interface=self.interface)
84
85 def stop(self):
86 log.info('stopped', interface=self.interface)
87
Arun Arora75e6bb22019-03-07 13:57:31 +000088 def get_ofp_device_info(self, device):
Matt Jeannerete33a7092019-03-12 21:54:14 -040089 ofp_device_info = self.devices[device.id].get_ofp_device_info(device)
90 log.debug('get_ofp_device_info', ofp_device_info=ofp_device_info)
91 return ofp_device_info
Arun Arora75e6bb22019-03-07 13:57:31 +000092
93 def get_ofp_port_info(self, device, port_no):
Matt Jeannerete33a7092019-03-12 21:54:14 -040094 ofp_port_info = self.devices[device.id].get_ofp_port_info(device, port_no)
95 log.debug('get_ofp_port_info', device_id=device.id, ofp_port_no=ofp_port_info)
96 return ofp_port_info
Arun Arora75e6bb22019-03-07 13:57:31 +000097
William Kurkian6f436d02019-02-06 16:25:01 -050098 def adapter_descriptor(self):
99 log.debug('get descriptor', interface=self.interface)
100 return self.descriptor
101
102 def device_types(self):
103 log.debug('get device_types', interface=self.interface,
104 items=self.supported_device_types)
105 return DeviceTypes(items=self.supported_device_types)
106
107 def health(self):
108 log.debug('get health', interface=self.interface)
109 raise NotImplementedError()
110
William Kurkian6f436d02019-02-06 16:25:01 -0500111 def change_master_state(self, master):
112 log.debug('change_master_state', interface=self.interface,
113 master=master)
114 raise NotImplementedError()
115
Matt Jeanneret7906d232019-02-14 14:57:38 -0500116 def adopt_device(self, device):
117 log.info('adopt-device', device=device)
118
119 kwargs = {
120 'support_classes': OpenOltDefaults['support_classes'],
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400121 'core_proxy': self.core_proxy,
Matt Jeanneret7906d232019-02-14 14:57:38 -0500122 'adapter_proxy': self.adapter_proxy,
Matt Jeanneret7906d232019-02-14 14:57:38 -0500123 'device': device,
124 'device_num': self.num_devices + 1
125 }
126 try:
127 self.devices[device.id] = OpenoltDevice(**kwargs)
128 except Exception as e:
129 log.error('Failed to adopt OpenOLT device', error=e)
130 # TODO set status to ERROR so that is clear something went wrong
131 del self.devices[device.id]
132 raise
133 else:
134 self.num_devices += 1
135
136 def reconcile_device(self, device):
137 log.info('reconcile-device', device=device)
138 kwargs = {
139 'support_classes': OpenOltDefaults['support_classes'],
Matt Jeanneretbad3d982019-03-11 16:06:10 -0400140 'core_proxy': self.core_proxy,
141 'adapter_proxy': self.adapter_proxy,
Matt Jeanneret7906d232019-02-14 14:57:38 -0500142 'device': device,
143 'device_num': self.num_devices + 1,
144 'reconciliation': True
145 }
146 try:
147 reconciled_device = OpenoltDevice(**kwargs)
148 log.debug('reconciled-device-recreated',
149 device_id=reconciled_device.device_id)
150 self.devices[device.id] = reconciled_device
151 except Exception as e:
152 log.error('Failed to reconcile OpenOLT device', error=e,
153 exception_type=type(e).__name__)
154 del self.devices[device.id]
155 raise
156 else:
157 self.num_devices += 1
158 # Invoke the children reconciliation which would setup the
159 # basic children data structures
160 self.adapter_agent.reconcile_child_devices(device.id)
161 return device
162
William Kurkian6f436d02019-02-06 16:25:01 -0500163 def abandon_device(self, device):
164 log.info('abandon-device', device=device)
165 raise NotImplementedError()
166
Matt Jeanneret7906d232019-02-14 14:57:38 -0500167 def disable_device(self, device):
168 log.info('disable-device', device=device)
169 handler = self.devices[device.id]
170 handler.disable()
William Kurkian6f436d02019-02-06 16:25:01 -0500171
Matt Jeanneret7906d232019-02-14 14:57:38 -0500172 def reenable_device(self, device):
173 log.info('reenable-device', device=device)
174 handler = self.devices[device.id]
175 handler.reenable()
William Kurkian6f436d02019-02-06 16:25:01 -0500176
Matt Jeanneret7906d232019-02-14 14:57:38 -0500177 def reboot_device(self, device):
178 log.info('reboot_device', device=device)
179 handler = self.devices[device.id]
180 handler.reboot()
William Kurkian6f436d02019-02-06 16:25:01 -0500181
William Kurkian6f436d02019-02-06 16:25:01 -0500182 def download_image(self, device, request):
183 log.info('image_download - Not implemented yet', device=device,
184 request=request)
185 raise NotImplementedError()
186
187 def get_image_download_status(self, device, request):
188 log.info('get_image_download - Not implemented yet', device=device,
189 request=request)
190 raise NotImplementedError()
191
192 def cancel_image_download(self, device, request):
193 log.info('cancel_image_download - Not implemented yet', device=device)
194 raise NotImplementedError()
195
196 def activate_image_update(self, device, request):
197 log.info('activate_image_update - Not implemented yet',
198 device=device, request=request)
199 raise NotImplementedError()
200
201 def revert_image_update(self, device, request):
202 log.info('revert_image_update - Not implemented yet',
203 device=device, request=request)
204 raise NotImplementedError()
205
206 def self_test_device(self, device):
William Kurkian6f436d02019-02-06 16:25:01 -0500207 log.info('Not implemented yet')
208 raise NotImplementedError()
209
Matt Jeanneret7906d232019-02-14 14:57:38 -0500210 def delete_device(self, device):
211 log.info('delete-device', device=device)
212 handler = self.devices[device.id]
213 handler.delete()
214 del self.devices[device.id]
215 del self.logical_device_id_to_root_device_id[device.parent_id]
216 return device
William Kurkian6f436d02019-02-06 16:25:01 -0500217
Matt Jeanneret7906d232019-02-14 14:57:38 -0500218 def get_device_details(self, device):
219 log.debug('get_device_details', device=device)
220 raise NotImplementedError()
221
222 def update_flows_bulk(self, device, flows, groups):
223 log.info('bulk-flow-update', device_id=device.id,
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500224 number_of_flows=len(flows.items),
225 number_of_groups=len(groups.items))
Matt Jeanneret7906d232019-02-14 14:57:38 -0500226 log.debug('flows and grousp details', flows=flows, groups=groups)
227 assert len(groups.items) == 0, "Cannot yet deal with groups"
228 handler = self.devices[device.id]
229 return handler.update_flow_table(flows.items)
230
231 def update_flows_incrementally(self, device, flow_changes, group_changes):
232 log.debug('update_flows_incrementally', device=device,
233 flow_changes=flow_changes, group_changes=group_changes)
234 log.info('This device does not allow this, therefore it is Not '
235 'implemented')
236 raise NotImplementedError()
237
238 def update_logical_flows(self, device_id, flows_to_add, flows_to_remove,
239 groups, device_rules_map):
240
241 log.info('logical-flows-update', flows_to_add=len(flows_to_add),
242 flows_to_remove=len(flows_to_remove))
243 log.debug('logical-flows-details', flows_to_add=flows_to_add,
244 flows_to_remove=flows_to_remove)
245 assert len(groups) == 0, "Cannot yet deal with groups"
246 handler = self.devices[device_id]
247 handler.update_logical_flows(flows_to_add, flows_to_remove,
248 device_rules_map)
249
250 def update_pm_config(self, device, pm_configs):
251 log.info('update_pm_config - Not implemented yet', device=device,
252 pm_configs=pm_configs)
253 raise NotImplementedError()
254
255 def send_proxied_message(self, proxy_address, msg):
256 log.debug('send-proxied-message',
257 proxy_address=proxy_address,
258 proxied_msg=msg)
259 handler = self.devices[proxy_address.device_id]
260 handler.send_proxied_message(proxy_address, msg)
261
262 def receive_proxied_message(self, proxy_address, msg):
263 log.debug('receive_proxied_message - Not implemented',
264 proxy_address=proxy_address,
265 proxied_msg=msg)
266 raise NotImplementedError()
267
268 def receive_packet_out(self, logical_device_id, egress_port_no, msg):
269 log.debug('packet-out', logical_device_id=logical_device_id,
270 egress_port_no=egress_port_no, msg_len=len(msg))
271
272 def ldi_to_di(ldi):
273 di = self.logical_device_id_to_root_device_id.get(ldi)
274 if di is None:
275 logical_device = self.adapter_agent.get_logical_device(ldi)
276 di = logical_device.root_device_id
277 self.logical_device_id_to_root_device_id[ldi] = di
278 return di
279
280 try:
281 device_id = ldi_to_di(logical_device_id)
282 handler = self.devices[device_id]
283 handler.packet_out(egress_port_no, msg)
284 except Exception as e:
285 log.error('packet-out:exception', e=e.message)
286
Matt Jeanneretb428b952019-03-07 05:14:17 -0500287 def process_inter_adapter_message(self, msg):
288 log.debug('process-inter-adapter-message', msg=msg)
289 # Unpack the header to know which device needs to handle this message
290 handler = None
291 if msg.header.proxy_device_id:
292 # typical request
293 handler = self.devices[msg.header.proxy_device_id]
294 elif msg.header.to_device_id and \
295 msg.header.to_device_id in self.devices_handlers:
296 # typical response
297 handler = self.devices[msg.header.to_device_id]
298 if handler:
299 reactor.callLater(0, handler.process_inter_adapter_message, msg)
300
Matt Jeanneret7906d232019-02-14 14:57:38 -0500301 def receive_inter_adapter_message(self, msg):
302 log.info('rx_inter_adapter_msg - Not implemented')
303 raise NotImplementedError()
304
305 def suppress_alarm(self, filter):
306 log.info('suppress_alarm - Not implemented yet', filter=filter)
307 raise NotImplementedError()
308
309 def unsuppress_alarm(self, filter):
310 log.info('unsuppress_alarm - Not implemented yet', filter=filter)
311 raise NotImplementedError()
312
313 # PON Mgnt APIs #
William Kurkian6f436d02019-02-06 16:25:01 -0500314 def create_interface(self, device, data):
315 log.debug('create-interface - Not implemented - We do not use this',
316 data=data)
317 raise NotImplementedError()
318
319 def update_interface(self, device, data):
320 log.debug('update-interface - Not implemented - We do not use this',
321 data=data)
322 raise NotImplementedError()
323
324 def remove_interface(self, device, data):
325 log.debug('remove-interface - Not implemented - We do not use this',
326 data=data)
327 raise NotImplementedError()
328
329 def receive_onu_detect_state(self, proxy_address, state):
330 log.debug('receive-onu-detect-state - Not implemented - We do not '
331 'use this', proxy_address=proxy_address,
332 state=state)
333 raise NotImplementedError()
334
335 def create_tcont(self, device, tcont_data, traffic_descriptor_data):
336 log.info('create-tcont - Not implemented - We do not use this',
337 tcont_data=tcont_data,
338 traffic_descriptor_data=traffic_descriptor_data)
339 raise NotImplementedError()
340
341 def update_tcont(self, device, tcont_data, traffic_descriptor_data):
342 log.info('update-tcont - Not implemented - We do not use this',
343 tcont_data=tcont_data,
344 traffic_descriptor_data=traffic_descriptor_data)
345 raise NotImplementedError()
346
347 def remove_tcont(self, device, tcont_data, traffic_descriptor_data):
348 log.info('remove-tcont - Not implemented - We do not use this',
349 tcont_data=tcont_data,
350 traffic_descriptor_data=traffic_descriptor_data)
351 raise NotImplementedError()
352
353 def create_gemport(self, device, data):
354 log.info('create-gemport - Not implemented - We do not use this',
355 data=data)
356 raise NotImplementedError()
357
358 def update_gemport(self, device, data):
359 log.info('update-gemport - Not implemented - We do not use this',
360 data=data)
361 raise NotImplementedError()
362
363 def remove_gemport(self, device, data):
364 log.info('remove-gemport - Not implemented - We do not use this',
365 data=data)
366 raise NotImplementedError()
367
368 def create_multicast_gemport(self, device, data):
369 log.info('create-mcast-gemport - Not implemented - We do not use '
370 'this', data=data)
371 raise NotImplementedError()
372
373 def update_multicast_gemport(self, device, data):
374 log.info('update-mcast-gemport - Not implemented - We do not use '
375 'this', data=data)
376 raise NotImplementedError()
377
378 def remove_multicast_gemport(self, device, data):
379 log.info('remove-mcast-gemport - Not implemented - We do not use '
380 'this', data=data)
381 raise NotImplementedError()
382
383 def create_multicast_distribution_set(self, device, data):
384 log.info('create-mcast-distribution-set - Not implemented - We do '
385 'not use this', data=data)
386 raise NotImplementedError()
387
388 def update_multicast_distribution_set(self, device, data):
389 log.info('update-mcast-distribution-set - Not implemented - We do '
390 'not use this', data=data)
391 raise NotImplementedError()
392
393 def remove_multicast_distribution_set(self, device, data):
394 log.info('remove-mcast-distribution-set - Not implemented - We do '
395 'not use this', data=data)
396 raise NotImplementedError()
397
Matt Jeanneret7906d232019-02-14 14:57:38 -0500398 def delete_child_device(self, parent_device_id, child_device):
399 log.info('delete-child_device', parent_device_id=parent_device_id,
400 child_device=child_device)
401 handler = self.devices[parent_device_id]
402 if handler is not None:
403 handler.delete_child_device(child_device)
William Kurkian6f436d02019-02-06 16:25:01 -0500404 else:
Matt Jeanneret7906d232019-02-14 14:57:38 -0500405 log.error('Could not find matching handler',
406 looking_for_device_id=parent_device_id,
407 available_handlers=self.devices.keys())
William Kurkian6f436d02019-02-06 16:25:01 -0500408
Matt Jeanneret7906d232019-02-14 14:57:38 -0500409 # This is currently not part of the Iadapter interface
410 def collect_stats(self, device_id):
411 log.info('collect_stats', device_id=device_id)
412 handler = self.devices[device_id]
413 if handler is not None:
414 handler.trigger_statistics_collection()
415 else:
416 log.error('Could not find matching handler',
417 looking_for_device_id=device_id,
418 available_handlers=self.devices.keys())
William Kurkian6f436d02019-02-06 16:25:01 -0500419
Matt Jeanneret7906d232019-02-14 14:57:38 -0500420 def simulate_alarm(self, device, request):
421 log.info('simulate_alarm', device=device, request=request)
William Kurkian6f436d02019-02-06 16:25:01 -0500422
Matt Jeanneret7906d232019-02-14 14:57:38 -0500423 if device.id not in self.devices:
424 log.error("Device does not exist", device_id=device.id)
425 return OperationResp(code=OperationResp.OPERATION_FAILURE,
426 additional_info="Device %s does not exist"
Matt Jeanneret9fd36df2019-02-14 19:14:36 -0500427 % device.id)
William Kurkian6f436d02019-02-06 16:25:01 -0500428
Matt Jeanneret7906d232019-02-14 14:57:38 -0500429 handler = self.devices[device.id]
William Kurkian6f436d02019-02-06 16:25:01 -0500430
Matt Jeanneret7906d232019-02-14 14:57:38 -0500431 handler.simulate_alarm(request)
William Kurkian6f436d02019-02-06 16:25:01 -0500432
Matt Jeanneret7906d232019-02-14 14:57:38 -0500433 return OperationResp(code=OperationResp.OPERATION_SUCCESS)