blob: 78cf42a0937bb193f0a701f66536881b6b947bfc [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]
203 handler.update_pm_config(device, pm_config)
204
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400205 def process_inter_adapter_message(self, msg):
khenaidoob9203542018-09-17 22:56:37 -0400206 raise NotImplementedError()
207
208 def receive_packet_out(self, logical_device_id, egress_port_no, msg):
209 raise NotImplementedError()
210
khenaidoob9203542018-09-17 22:56:37 -0400211 def suppress_alarm(self, filter):
212 raise NotImplementedError()
213
214 def unsuppress_alarm(self, filter):
215 raise NotImplementedError()
216
217 def _get_handler(self, device):
218 if device.id in self.devices_handlers:
219 handler = self.devices_handlers[device.id]
220 if handler is not None:
221 return handler
222 return None
223
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400224
khenaidoob9203542018-09-17 22:56:37 -0400225"""
226OLT Adapter base class
227"""
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400228
229
khenaidoob9203542018-09-17 22:56:37 -0400230class OltAdapter(IAdapter):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400231 def __init__(self,
232 core_proxy,
233 adapter_proxy,
234 config,
235 device_handler_class,
236 name,
237 vendor,
238 version, device_type,
khenaidoob9203542018-09-17 22:56:37 -0400239 accepts_bulk_flow_update=True,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400240 accepts_add_remove_flow_updates=False):
241 super(OltAdapter, self).__init__(core_proxy=core_proxy,
242 adapter_proxy=adapter_proxy,
khenaidoob9203542018-09-17 22:56:37 -0400243 config=config,
244 device_handler_class=device_handler_class,
245 name=name,
246 vendor=vendor,
247 version=version,
248 device_type=device_type,
249 vendor_id=None,
250 accepts_bulk_flow_update=accepts_bulk_flow_update,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400251 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates)
khenaidoob9203542018-09-17 22:56:37 -0400252 self.logical_device_id_to_root_device_id = dict()
253
254 def reconcile_device(self, device):
255 try:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400256 self.devices_handlers[device.id] = self.device_handler_class(self,
257 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400258 # Work only required for devices that are in ENABLED state
259 if device.admin_state == AdminState.ENABLED:
260 reactor.callLater(0,
261 self.devices_handlers[device.id].reconcile,
262 device)
263 else:
264 # Invoke the children reconciliation which would setup the
265 # basic children data structures
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400266 self.core_proxy.reconcile_child_devices(device.id)
khenaidoob9203542018-09-17 22:56:37 -0400267 return device
268 except Exception, e:
269 log.exception('Exception', e=e)
270
271 def send_proxied_message(self, proxy_address, msg):
272 log.info('send-proxied-message', proxy_address=proxy_address, msg=msg)
273 handler = self.devices_handlers[proxy_address.device_id]
274 handler.send_proxied_message(proxy_address, msg)
275
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400276 def process_inter_adapter_message(self, msg):
khenaidoofdbad6e2018-11-06 22:26:38 -0500277 log.debug('process-inter-adapter-message', msg=msg)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400278 # Unpack the header to know which device needs to handle this message
279 handler = None
280 if msg.header.proxy_device_id:
281 # typical request
282 handler = self.devices_handlers[msg.header.proxy_device_id]
283 elif msg.header.to_device_id and \
284 msg.header.to_device_id in self.devices_handlers:
285 # typical response
286 handler = self.devices_handlers[msg.header.to_device_id]
287 if handler:
288 reactor.callLater(0, handler.process_inter_adapter_message, msg)
289
khenaidoofdbad6e2018-11-06 22:26:38 -0500290 def receive_packet_out(self, device_id, egress_port_no, msg):
291 log.info('receive_packet_out', device_id=device_id,
292 egress_port=egress_port_no, msg=msg)
khenaidoob9203542018-09-17 22:56:37 -0400293 handler = self.devices_handlers[device_id]
khenaidoofdbad6e2018-11-06 22:26:38 -0500294 handler.packet_out(egress_port_no, msg.data)
khenaidoob9203542018-09-17 22:56:37 -0400295
296
297"""
298ONU Adapter base class
299"""
300
301
302class OnuAdapter(IAdapter):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400303 def __init__(self,
304 core_proxy,
305 adapter_proxy,
306 config,
307 device_handler_class,
308 name,
309 vendor,
310 version,
311 device_type,
312 vendor_id,
313 accepts_bulk_flow_update=True,
khenaidoob9203542018-09-17 22:56:37 -0400314 accepts_add_remove_flow_updates=False):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400315 super(OnuAdapter, self).__init__(core_proxy=core_proxy,
316 adapter_proxy=adapter_proxy,
khenaidoob9203542018-09-17 22:56:37 -0400317 config=config,
318 device_handler_class=device_handler_class,
319 name=name,
320 vendor=vendor,
321 version=version,
322 device_type=device_type,
323 vendor_id=vendor_id,
324 accepts_bulk_flow_update=accepts_bulk_flow_update,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400325 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates)
khenaidoob9203542018-09-17 22:56:37 -0400326
327 def reconcile_device(self, device):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400328 self.devices_handlers[device.id] = self.device_handler_class(self,
329 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400330 # Reconcile only if state was ENABLED
331 if device.admin_state == AdminState.ENABLED:
332 reactor.callLater(0,
333 self.devices_handlers[device.id].reconcile,
334 device)
335 return device
336
337 def receive_proxied_message(self, proxy_address, msg):
338 log.info('receive-proxied-message', proxy_address=proxy_address,
339 device_id=proxy_address.device_id, msg=msg)
340 # Device_id from the proxy_address is the olt device id. We need to
341 # get the onu device id using the port number in the proxy_address
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400342 device = self.core_proxy. \
khenaidoob9203542018-09-17 22:56:37 -0400343 get_child_device_with_proxy_address(proxy_address)
344 if device:
345 handler = self.devices_handlers[device.id]
346 handler.receive_message(msg)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400347
348 def process_inter_adapter_message(self, msg):
349 log.info('process-inter-adapter-message', msg=msg)
350 # Unpack the header to know which device needs to handle this message
351 if msg.header:
352 handler = self.devices_handlers[msg.header.to_device_id]
353 handler.process_inter_adapter_message(msg)