blob: abe1bbc5e31976faaf584a2408443ac32f9f38f8 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001#
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"""
18Adapter abstract base class
19"""
20
21import structlog
khenaidoob9203542018-09-17 22:56:37 -040022from twisted.internet import reactor
khenaidoo6fdf0ba2018-11-02 14:38:33 -040023from zope.interface import implementer
khenaidoob9203542018-09-17 22:56:37 -040024
khenaidoofdbad6e2018-11-06 22:26:38 -050025from interface import IAdapterInterface
26from python.protos.adapter_pb2 import Adapter
27from python.protos.adapter_pb2 import AdapterConfig
28from python.protos.common_pb2 import AdminState
29from python.protos.common_pb2 import LogLevel
30from python.protos.device_pb2 import DeviceType, DeviceTypes
31from python.protos.health_pb2 import HealthStatus
khenaidoob9203542018-09-17 22:56:37 -040032
khenaidoo43c82122018-11-22 18:38:28 -050033
khenaidoob9203542018-09-17 22:56:37 -040034log = structlog.get_logger()
35
36
37@implementer(IAdapterInterface)
38class IAdapter(object):
khenaidoo6fdf0ba2018-11-02 14:38:33 -040039 def __init__(self,
40 core_proxy,
41 adapter_proxy,
42 config,
43 device_handler_class,
44 name,
45 vendor,
46 version,
47 device_type, vendor_id,
khenaidoob9203542018-09-17 22:56:37 -040048 accepts_bulk_flow_update=True,
khenaidoo6fdf0ba2018-11-02 14:38:33 -040049 accepts_add_remove_flow_updates=False):
50 log.debug(
51 'Initializing adapter: {} {} {}'.format(vendor, name, version))
52 self.core_proxy = core_proxy
53 self.adapter_proxy = adapter_proxy
khenaidoob9203542018-09-17 22:56:37 -040054 self.config = config
55 self.name = name
56 self.supported_device_types = [
57 DeviceType(
58 id=device_type,
59 vendor_id=vendor_id,
60 adapter=name,
61 accepts_bulk_flow_update=accepts_bulk_flow_update,
62 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates
63 )
64 ]
65 self.descriptor = Adapter(
66 id=self.name,
67 vendor=vendor,
68 version=version,
69 config=AdapterConfig(log_level=LogLevel.INFO)
70 )
71 self.devices_handlers = dict() # device_id -> Olt/OnuHandler()
72 self.device_handler_class = device_handler_class
73
74 def start(self):
75 log.info('Starting adapter: {}'.format(self.name))
76
77 def stop(self):
78 log.info('Stopping adapter: {}'.format(self.name))
79
80 def adapter_descriptor(self):
81 return self.descriptor
82
83 def device_types(self):
84 return DeviceTypes(items=self.supported_device_types)
85
86 def health(self):
87 # return HealthStatus(state=HealthStatus.HealthState.HEALTHY)
88 return HealthStatus(state=HealthStatus.HEALTHY)
89
90 def change_master_state(self, master):
91 raise NotImplementedError()
92
93 def get_ofp_device_info(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -040094 log.debug('get_ofp_device_info_start', device_id=device.id)
khenaidoo6fdf0ba2018-11-02 14:38:33 -040095 ofp_device_info = self.devices_handlers[device.id].get_ofp_device_info(
96 device)
khenaidoo92e62c52018-10-03 14:02:54 -040097 log.debug('get_ofp_device_info_ends', device_id=device.id)
98 return ofp_device_info
khenaidoob9203542018-09-17 22:56:37 -040099
100 def get_ofp_port_info(self, device, port_no):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400101 log.debug('get_ofp_port_info_start', device_id=device.id,
102 port_no=port_no)
103 ofp_port_info = self.devices_handlers[device.id].get_ofp_port_info(
104 device, port_no)
105 log.debug('get_ofp_port_info_ends', device_id=device.id,
106 port_no=port_no)
khenaidoo92e62c52018-10-03 14:02:54 -0400107 return ofp_port_info
khenaidoob9203542018-09-17 22:56:37 -0400108
109 def adopt_device(self, device):
110 log.debug('adopt_device', device_id=device.id)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400111 self.devices_handlers[device.id] = self.device_handler_class(self,
112 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400113 reactor.callLater(0, self.devices_handlers[device.id].activate, device)
114 log.debug('adopt_device_done', device_id=device.id)
115 return device
116
117 def reconcile_device(self, device):
118 raise NotImplementedError()
119
120 def abandon_device(self, device):
121 raise NotImplementedError()
122
123 def disable_device(self, device):
124 log.info('disable-device', device_id=device.id)
khenaidoo4d4802d2018-10-04 21:59:49 -0400125 reactor.callLater(0, self.devices_handlers[device.id].disable)
126 log.debug('disable-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400127 return device
128
129 def reenable_device(self, device):
130 log.info('reenable-device', device_id=device.id)
131 reactor.callLater(0, self.devices_handlers[device.id].reenable)
khenaidoo4d4802d2018-10-04 21:59:49 -0400132 log.info('reenable-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400133 return device
134
135 def reboot_device(self, device):
136 log.info('reboot-device', device_id=device.id)
137 reactor.callLater(0, self.devices_handlers[device.id].reboot)
khenaidoo4d4802d2018-10-04 21:59:49 -0400138 log.info('reboot-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400139 return device
140
141 def download_image(self, device, request):
142 raise NotImplementedError()
143
144 def get_image_download_status(self, device, request):
145 raise NotImplementedError()
146
147 def cancel_image_download(self, device, request):
148 raise NotImplementedError()
149
150 def activate_image_update(self, device, request):
151 raise NotImplementedError()
152
153 def revert_image_update(self, device, request):
154 raise NotImplementedError()
155
156 def self_test_device(self, device):
khenaidoo4d4802d2018-10-04 21:59:49 -0400157 log.info('self-test', device_id=device.id)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400158 result = reactor.callLater(0, self.devices_handlers[
159 device.id].self_test_device)
khenaidoo4d4802d2018-10-04 21:59:49 -0400160 log.info('self-test-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400161 return result
162
163 def delete_device(self, device):
164 log.info('delete-device', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400165 reactor.callLater(0, self.devices_handlers[device.id].delete)
khenaidoo4d4802d2018-10-04 21:59:49 -0400166 log.info('delete-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400167 return device
168
169 def get_device_details(self, device):
170 raise NotImplementedError()
171
172 def update_flows_bulk(self, device, flows, groups):
173 log.info('bulk-flow-update', device_id=device.id,
174 flows=flows, groups=groups)
175 assert len(groups.items) == 0
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400176 reactor.callLater(0, self.devices_handlers[device.id].update_flow_table,
177 flows.items)
khenaidoo19d7b632018-10-30 10:49:50 -0400178 return device
179
khenaidoob9203542018-09-17 22:56:37 -0400180 def update_flows_incrementally(self, device, flow_changes, group_changes):
181 log.info('incremental-flow-update', device_id=device.id,
182 flows=flow_changes, groups=group_changes)
183 # For now, there is no support for group changes
184 assert len(group_changes.to_add.items) == 0
185 assert len(group_changes.to_remove.items) == 0
186
187 handler = self.devices_handlers[device.id]
188 # Remove flows
189 if len(flow_changes.to_remove.items) != 0:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400190 reactor.callLater(0, handler.remove_from_flow_table,
191 flow_changes.to_remove.items)
khenaidoob9203542018-09-17 22:56:37 -0400192
193 # Add flows
194 if len(flow_changes.to_add.items) != 0:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400195 reactor.callLater(0, handler.add_to_flow_table,
196 flow_changes.to_add.items)
khenaidoo19d7b632018-10-30 10:49:50 -0400197 return device
khenaidoob9203542018-09-17 22:56:37 -0400198
199 def update_pm_config(self, device, pm_config):
200 log.info("adapter-update-pm-config", device=device,
201 pm_config=pm_config)
202 handler = self.devices_handlers[device.id]
khenaidoobcf205b2019-01-25 22:21:14 -0500203 if handler:
204 reactor.callLater(0, handler.update_pm_config, device, pm_config)
khenaidoob9203542018-09-17 22:56:37 -0400205
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400206 def process_inter_adapter_message(self, msg):
khenaidoob9203542018-09-17 22:56:37 -0400207 raise NotImplementedError()
208
khenaidoobcf205b2019-01-25 22:21:14 -0500209 def receive_packet_out(self, device_id, egress_port_no, msg):
khenaidoob9203542018-09-17 22:56:37 -0400210 raise NotImplementedError()
211
khenaidoob9203542018-09-17 22:56:37 -0400212 def suppress_alarm(self, filter):
213 raise NotImplementedError()
214
215 def unsuppress_alarm(self, filter):
216 raise NotImplementedError()
217
218 def _get_handler(self, device):
219 if device.id in self.devices_handlers:
220 handler = self.devices_handlers[device.id]
221 if handler is not None:
222 return handler
223 return None
224
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400225
khenaidoob9203542018-09-17 22:56:37 -0400226"""
227OLT Adapter base class
228"""
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400229
230
khenaidoob9203542018-09-17 22:56:37 -0400231class OltAdapter(IAdapter):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400232 def __init__(self,
233 core_proxy,
234 adapter_proxy,
235 config,
236 device_handler_class,
237 name,
238 vendor,
239 version, device_type,
khenaidoob9203542018-09-17 22:56:37 -0400240 accepts_bulk_flow_update=True,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400241 accepts_add_remove_flow_updates=False):
242 super(OltAdapter, self).__init__(core_proxy=core_proxy,
243 adapter_proxy=adapter_proxy,
khenaidoob9203542018-09-17 22:56:37 -0400244 config=config,
245 device_handler_class=device_handler_class,
246 name=name,
247 vendor=vendor,
248 version=version,
249 device_type=device_type,
250 vendor_id=None,
251 accepts_bulk_flow_update=accepts_bulk_flow_update,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400252 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates)
khenaidoob9203542018-09-17 22:56:37 -0400253 self.logical_device_id_to_root_device_id = dict()
254
255 def reconcile_device(self, device):
256 try:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400257 self.devices_handlers[device.id] = self.device_handler_class(self,
258 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400259 # Work only required for devices that are in ENABLED state
260 if device.admin_state == AdminState.ENABLED:
261 reactor.callLater(0,
262 self.devices_handlers[device.id].reconcile,
263 device)
264 else:
265 # Invoke the children reconciliation which would setup the
266 # basic children data structures
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400267 self.core_proxy.reconcile_child_devices(device.id)
khenaidoob9203542018-09-17 22:56:37 -0400268 return device
269 except Exception, e:
270 log.exception('Exception', e=e)
271
272 def send_proxied_message(self, proxy_address, msg):
273 log.info('send-proxied-message', proxy_address=proxy_address, msg=msg)
274 handler = self.devices_handlers[proxy_address.device_id]
275 handler.send_proxied_message(proxy_address, msg)
276
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400277 def process_inter_adapter_message(self, msg):
khenaidoofdbad6e2018-11-06 22:26:38 -0500278 log.debug('process-inter-adapter-message', msg=msg)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400279 # Unpack the header to know which device needs to handle this message
280 handler = None
281 if msg.header.proxy_device_id:
282 # typical request
283 handler = self.devices_handlers[msg.header.proxy_device_id]
284 elif msg.header.to_device_id and \
285 msg.header.to_device_id in self.devices_handlers:
286 # typical response
287 handler = self.devices_handlers[msg.header.to_device_id]
288 if handler:
289 reactor.callLater(0, handler.process_inter_adapter_message, msg)
290
khenaidoofdbad6e2018-11-06 22:26:38 -0500291 def receive_packet_out(self, device_id, egress_port_no, msg):
khenaidoobcf205b2019-01-25 22:21:14 -0500292 try:
293 log.info('receive_packet_out', device_id=device_id,
294 egress_port=egress_port_no, msg=msg)
295 handler = self.devices_handlers[device_id]
296 if handler:
297 reactor.callLater(0, handler.packet_out, egress_port_no, msg.data)
298 except Exception, e:
299 log.exception('packet-out-failure', e=e)
khenaidoob9203542018-09-17 22:56:37 -0400300
301
302"""
303ONU Adapter base class
304"""
305
306
307class OnuAdapter(IAdapter):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400308 def __init__(self,
309 core_proxy,
310 adapter_proxy,
311 config,
312 device_handler_class,
313 name,
314 vendor,
315 version,
316 device_type,
317 vendor_id,
318 accepts_bulk_flow_update=True,
khenaidoob9203542018-09-17 22:56:37 -0400319 accepts_add_remove_flow_updates=False):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400320 super(OnuAdapter, self).__init__(core_proxy=core_proxy,
321 adapter_proxy=adapter_proxy,
khenaidoob9203542018-09-17 22:56:37 -0400322 config=config,
323 device_handler_class=device_handler_class,
324 name=name,
325 vendor=vendor,
326 version=version,
327 device_type=device_type,
328 vendor_id=vendor_id,
329 accepts_bulk_flow_update=accepts_bulk_flow_update,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400330 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates)
khenaidoob9203542018-09-17 22:56:37 -0400331
332 def reconcile_device(self, device):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400333 self.devices_handlers[device.id] = self.device_handler_class(self,
334 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400335 # Reconcile only if state was ENABLED
336 if device.admin_state == AdminState.ENABLED:
337 reactor.callLater(0,
338 self.devices_handlers[device.id].reconcile,
339 device)
340 return device
341
342 def receive_proxied_message(self, proxy_address, msg):
343 log.info('receive-proxied-message', proxy_address=proxy_address,
344 device_id=proxy_address.device_id, msg=msg)
345 # Device_id from the proxy_address is the olt device id. We need to
346 # get the onu device id using the port number in the proxy_address
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400347 device = self.core_proxy. \
khenaidoob9203542018-09-17 22:56:37 -0400348 get_child_device_with_proxy_address(proxy_address)
349 if device:
350 handler = self.devices_handlers[device.id]
351 handler.receive_message(msg)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400352
353 def process_inter_adapter_message(self, msg):
354 log.info('process-inter-adapter-message', msg=msg)
355 # Unpack the header to know which device needs to handle this message
356 if msg.header:
357 handler = self.devices_handlers[msg.header.to_device_id]
358 handler.process_inter_adapter_message(msg)