blob: ee4d116fbb8159628a85c73b6ec3bd11db35f377 [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
khenaidoob9203542018-09-17 22:56:37 -040025from adapters.interface import IAdapterInterface
26from adapters.protos.adapter_pb2 import Adapter
27from adapters.protos.adapter_pb2 import AdapterConfig
khenaidoo6fdf0ba2018-11-02 14:38:33 -040028from adapters.protos.common_pb2 import AdminState
khenaidoob9203542018-09-17 22:56:37 -040029from adapters.protos.common_pb2 import LogLevel
khenaidoo6fdf0ba2018-11-02 14:38:33 -040030from adapters.protos.device_pb2 import DeviceType, DeviceTypes
khenaidoob9203542018-09-17 22:56:37 -040031from adapters.protos.health_pb2 import HealthStatus
khenaidoob9203542018-09-17 22:56:37 -040032
33log = structlog.get_logger()
34
35
36@implementer(IAdapterInterface)
37class IAdapter(object):
khenaidoo6fdf0ba2018-11-02 14:38:33 -040038 def __init__(self,
39 core_proxy,
40 adapter_proxy,
41 config,
42 device_handler_class,
43 name,
44 vendor,
45 version,
46 device_type, vendor_id,
khenaidoob9203542018-09-17 22:56:37 -040047 accepts_bulk_flow_update=True,
khenaidoo6fdf0ba2018-11-02 14:38:33 -040048 accepts_add_remove_flow_updates=False):
49 log.debug(
50 'Initializing adapter: {} {} {}'.format(vendor, name, version))
51 self.core_proxy = core_proxy
52 self.adapter_proxy = adapter_proxy
khenaidoob9203542018-09-17 22:56:37 -040053 self.config = config
54 self.name = name
55 self.supported_device_types = [
56 DeviceType(
57 id=device_type,
58 vendor_id=vendor_id,
59 adapter=name,
60 accepts_bulk_flow_update=accepts_bulk_flow_update,
61 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates
62 )
63 ]
64 self.descriptor = Adapter(
65 id=self.name,
66 vendor=vendor,
67 version=version,
68 config=AdapterConfig(log_level=LogLevel.INFO)
69 )
70 self.devices_handlers = dict() # device_id -> Olt/OnuHandler()
71 self.device_handler_class = device_handler_class
72
73 def start(self):
74 log.info('Starting adapter: {}'.format(self.name))
75
76 def stop(self):
77 log.info('Stopping adapter: {}'.format(self.name))
78
79 def adapter_descriptor(self):
80 return self.descriptor
81
82 def device_types(self):
83 return DeviceTypes(items=self.supported_device_types)
84
85 def health(self):
86 # return HealthStatus(state=HealthStatus.HealthState.HEALTHY)
87 return HealthStatus(state=HealthStatus.HEALTHY)
88
89 def change_master_state(self, master):
90 raise NotImplementedError()
91
92 def get_ofp_device_info(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -040093 log.debug('get_ofp_device_info_start', device_id=device.id)
khenaidoo6fdf0ba2018-11-02 14:38:33 -040094 ofp_device_info = self.devices_handlers[device.id].get_ofp_device_info(
95 device)
khenaidoo92e62c52018-10-03 14:02:54 -040096 log.debug('get_ofp_device_info_ends', device_id=device.id)
97 return ofp_device_info
khenaidoob9203542018-09-17 22:56:37 -040098
99 def get_ofp_port_info(self, device, port_no):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400100 log.debug('get_ofp_port_info_start', device_id=device.id,
101 port_no=port_no)
102 ofp_port_info = self.devices_handlers[device.id].get_ofp_port_info(
103 device, port_no)
104 log.debug('get_ofp_port_info_ends', device_id=device.id,
105 port_no=port_no)
khenaidoo92e62c52018-10-03 14:02:54 -0400106 return ofp_port_info
khenaidoob9203542018-09-17 22:56:37 -0400107
108 def adopt_device(self, device):
109 log.debug('adopt_device', device_id=device.id)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400110 self.devices_handlers[device.id] = self.device_handler_class(self,
111 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400112 reactor.callLater(0, self.devices_handlers[device.id].activate, device)
113 log.debug('adopt_device_done', device_id=device.id)
114 return device
115
116 def reconcile_device(self, device):
117 raise NotImplementedError()
118
119 def abandon_device(self, device):
120 raise NotImplementedError()
121
122 def disable_device(self, device):
123 log.info('disable-device', device_id=device.id)
khenaidoo4d4802d2018-10-04 21:59:49 -0400124 reactor.callLater(0, self.devices_handlers[device.id].disable)
125 log.debug('disable-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400126 return device
127
128 def reenable_device(self, device):
129 log.info('reenable-device', device_id=device.id)
130 reactor.callLater(0, self.devices_handlers[device.id].reenable)
khenaidoo4d4802d2018-10-04 21:59:49 -0400131 log.info('reenable-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400132 return device
133
134 def reboot_device(self, device):
135 log.info('reboot-device', device_id=device.id)
136 reactor.callLater(0, self.devices_handlers[device.id].reboot)
khenaidoo4d4802d2018-10-04 21:59:49 -0400137 log.info('reboot-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400138 return device
139
140 def download_image(self, device, request):
141 raise NotImplementedError()
142
143 def get_image_download_status(self, device, request):
144 raise NotImplementedError()
145
146 def cancel_image_download(self, device, request):
147 raise NotImplementedError()
148
149 def activate_image_update(self, device, request):
150 raise NotImplementedError()
151
152 def revert_image_update(self, device, request):
153 raise NotImplementedError()
154
155 def self_test_device(self, device):
khenaidoo4d4802d2018-10-04 21:59:49 -0400156 log.info('self-test', device_id=device.id)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400157 result = reactor.callLater(0, self.devices_handlers[
158 device.id].self_test_device)
khenaidoo4d4802d2018-10-04 21:59:49 -0400159 log.info('self-test-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400160 return result
161
162 def delete_device(self, device):
163 log.info('delete-device', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400164 reactor.callLater(0, self.devices_handlers[device.id].delete)
khenaidoo4d4802d2018-10-04 21:59:49 -0400165 log.info('delete-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400166 return device
167
168 def get_device_details(self, device):
169 raise NotImplementedError()
170
171 def update_flows_bulk(self, device, flows, groups):
172 log.info('bulk-flow-update', device_id=device.id,
173 flows=flows, groups=groups)
174 assert len(groups.items) == 0
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400175 reactor.callLater(0, self.devices_handlers[device.id].update_flow_table,
176 flows.items)
khenaidoo19d7b632018-10-30 10:49:50 -0400177 return device
178
khenaidoob9203542018-09-17 22:56:37 -0400179 def update_flows_incrementally(self, device, flow_changes, group_changes):
180 log.info('incremental-flow-update', device_id=device.id,
181 flows=flow_changes, groups=group_changes)
182 # For now, there is no support for group changes
183 assert len(group_changes.to_add.items) == 0
184 assert len(group_changes.to_remove.items) == 0
185
186 handler = self.devices_handlers[device.id]
187 # Remove flows
188 if len(flow_changes.to_remove.items) != 0:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400189 reactor.callLater(0, handler.remove_from_flow_table,
190 flow_changes.to_remove.items)
khenaidoob9203542018-09-17 22:56:37 -0400191
192 # Add flows
193 if len(flow_changes.to_add.items) != 0:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400194 reactor.callLater(0, handler.add_to_flow_table,
195 flow_changes.to_add.items)
khenaidoo19d7b632018-10-30 10:49:50 -0400196 return device
khenaidoob9203542018-09-17 22:56:37 -0400197
198 def update_pm_config(self, device, pm_config):
199 log.info("adapter-update-pm-config", device=device,
200 pm_config=pm_config)
201 handler = self.devices_handlers[device.id]
202 handler.update_pm_config(device, pm_config)
203
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400204 def process_inter_adapter_message(self, msg):
khenaidoob9203542018-09-17 22:56:37 -0400205 raise NotImplementedError()
206
207 def receive_packet_out(self, logical_device_id, egress_port_no, msg):
208 raise NotImplementedError()
209
khenaidoob9203542018-09-17 22:56:37 -0400210 def suppress_alarm(self, filter):
211 raise NotImplementedError()
212
213 def unsuppress_alarm(self, filter):
214 raise NotImplementedError()
215
216 def _get_handler(self, device):
217 if device.id in self.devices_handlers:
218 handler = self.devices_handlers[device.id]
219 if handler is not None:
220 return handler
221 return None
222
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400223
khenaidoob9203542018-09-17 22:56:37 -0400224"""
225OLT Adapter base class
226"""
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400227
228
khenaidoob9203542018-09-17 22:56:37 -0400229class OltAdapter(IAdapter):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400230 def __init__(self,
231 core_proxy,
232 adapter_proxy,
233 config,
234 device_handler_class,
235 name,
236 vendor,
237 version, device_type,
khenaidoob9203542018-09-17 22:56:37 -0400238 accepts_bulk_flow_update=True,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400239 accepts_add_remove_flow_updates=False):
240 super(OltAdapter, self).__init__(core_proxy=core_proxy,
241 adapter_proxy=adapter_proxy,
khenaidoob9203542018-09-17 22:56:37 -0400242 config=config,
243 device_handler_class=device_handler_class,
244 name=name,
245 vendor=vendor,
246 version=version,
247 device_type=device_type,
248 vendor_id=None,
249 accepts_bulk_flow_update=accepts_bulk_flow_update,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400250 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates)
khenaidoob9203542018-09-17 22:56:37 -0400251 self.logical_device_id_to_root_device_id = dict()
252
253 def reconcile_device(self, device):
254 try:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400255 self.devices_handlers[device.id] = self.device_handler_class(self,
256 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400257 # Work only required for devices that are in ENABLED state
258 if device.admin_state == AdminState.ENABLED:
259 reactor.callLater(0,
260 self.devices_handlers[device.id].reconcile,
261 device)
262 else:
263 # Invoke the children reconciliation which would setup the
264 # basic children data structures
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400265 self.core_proxy.reconcile_child_devices(device.id)
khenaidoob9203542018-09-17 22:56:37 -0400266 return device
267 except Exception, e:
268 log.exception('Exception', e=e)
269
270 def send_proxied_message(self, proxy_address, msg):
271 log.info('send-proxied-message', proxy_address=proxy_address, msg=msg)
272 handler = self.devices_handlers[proxy_address.device_id]
273 handler.send_proxied_message(proxy_address, msg)
274
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400275 def process_inter_adapter_message(self, msg):
276 log.info('process-inter-adapter-message', msg=msg)
277 # Unpack the header to know which device needs to handle this message
278 handler = None
279 if msg.header.proxy_device_id:
280 # typical request
281 handler = self.devices_handlers[msg.header.proxy_device_id]
282 elif msg.header.to_device_id and \
283 msg.header.to_device_id in self.devices_handlers:
284 # typical response
285 handler = self.devices_handlers[msg.header.to_device_id]
286 if handler:
287 reactor.callLater(0, handler.process_inter_adapter_message, msg)
288
khenaidoob9203542018-09-17 22:56:37 -0400289 def receive_packet_out(self, logical_device_id, egress_port_no, msg):
290 def ldi_to_di(ldi):
291 di = self.logical_device_id_to_root_device_id.get(ldi)
292 if di is None:
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400293 logical_device = self.core_proxy.get_logical_device(ldi)
khenaidoob9203542018-09-17 22:56:37 -0400294 di = logical_device.root_device_id
295 self.logical_device_id_to_root_device_id[ldi] = di
296 return di
297
298 device_id = ldi_to_di(logical_device_id)
299 handler = self.devices_handlers[device_id]
300 handler.packet_out(egress_port_no, msg)
301
302
303"""
304ONU Adapter base class
305"""
306
307
308class OnuAdapter(IAdapter):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400309 def __init__(self,
310 core_proxy,
311 adapter_proxy,
312 config,
313 device_handler_class,
314 name,
315 vendor,
316 version,
317 device_type,
318 vendor_id,
319 accepts_bulk_flow_update=True,
khenaidoob9203542018-09-17 22:56:37 -0400320 accepts_add_remove_flow_updates=False):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400321 super(OnuAdapter, self).__init__(core_proxy=core_proxy,
322 adapter_proxy=adapter_proxy,
khenaidoob9203542018-09-17 22:56:37 -0400323 config=config,
324 device_handler_class=device_handler_class,
325 name=name,
326 vendor=vendor,
327 version=version,
328 device_type=device_type,
329 vendor_id=vendor_id,
330 accepts_bulk_flow_update=accepts_bulk_flow_update,
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400331 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates)
khenaidoob9203542018-09-17 22:56:37 -0400332
333 def reconcile_device(self, device):
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400334 self.devices_handlers[device.id] = self.device_handler_class(self,
335 device.id)
khenaidoob9203542018-09-17 22:56:37 -0400336 # Reconcile only if state was ENABLED
337 if device.admin_state == AdminState.ENABLED:
338 reactor.callLater(0,
339 self.devices_handlers[device.id].reconcile,
340 device)
341 return device
342
343 def receive_proxied_message(self, proxy_address, msg):
344 log.info('receive-proxied-message', proxy_address=proxy_address,
345 device_id=proxy_address.device_id, msg=msg)
346 # Device_id from the proxy_address is the olt device id. We need to
347 # get the onu device id using the port number in the proxy_address
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400348 device = self.core_proxy. \
khenaidoob9203542018-09-17 22:56:37 -0400349 get_child_device_with_proxy_address(proxy_address)
350 if device:
351 handler = self.devices_handlers[device.id]
352 handler.receive_message(msg)
khenaidoo6fdf0ba2018-11-02 14:38:33 -0400353
354 def process_inter_adapter_message(self, msg):
355 log.info('process-inter-adapter-message', msg=msg)
356 # Unpack the header to know which device needs to handle this message
357 if msg.header:
358 handler = self.devices_handlers[msg.header.to_device_id]
359 handler.process_inter_adapter_message(msg)