blob: 8cdcd6e444e6095ce904f242a44f0d8ef66e59ca [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
22from zope.interface import implementer
23from twisted.internet import reactor
24
25from adapters.protos.common_pb2 import AdminState
26from adapters.protos.device_pb2 import DeviceType, DeviceTypes
27from adapters.interface import IAdapterInterface
28from adapters.protos.adapter_pb2 import Adapter
29from adapters.protos.adapter_pb2 import AdapterConfig
30from adapters.protos.common_pb2 import LogLevel
31from adapters.protos.health_pb2 import HealthStatus
32from adapters.protos.device_pb2 import Device
33
34log = structlog.get_logger()
35
36
37@implementer(IAdapterInterface)
38class IAdapter(object):
39 def __init__(self, adapter_agent, config, device_handler_class, name,
40 vendor, version, device_type, vendor_id,
41 accepts_bulk_flow_update=True,
42 accepts_add_remove_flow_updates=False, core_proxy=None):
43 log.debug('Initializing adapter: {} {} {}'.format(vendor, name, version))
44 self.adapter_agent = adapter_agent
45 self.core_proxy=core_proxy
46 self.config = config
47 self.name = name
48 self.supported_device_types = [
49 DeviceType(
50 id=device_type,
51 vendor_id=vendor_id,
52 adapter=name,
53 accepts_bulk_flow_update=accepts_bulk_flow_update,
54 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates
55 )
56 ]
57 self.descriptor = Adapter(
58 id=self.name,
59 vendor=vendor,
60 version=version,
61 config=AdapterConfig(log_level=LogLevel.INFO)
62 )
63 self.devices_handlers = dict() # device_id -> Olt/OnuHandler()
64 self.device_handler_class = device_handler_class
65
66 def start(self):
67 log.info('Starting adapter: {}'.format(self.name))
68
69 def stop(self):
70 log.info('Stopping adapter: {}'.format(self.name))
71
72 def adapter_descriptor(self):
73 return self.descriptor
74
75 def device_types(self):
76 return DeviceTypes(items=self.supported_device_types)
77
78 def health(self):
79 # return HealthStatus(state=HealthStatus.HealthState.HEALTHY)
80 return HealthStatus(state=HealthStatus.HEALTHY)
81
82 def change_master_state(self, master):
83 raise NotImplementedError()
84
85 def get_ofp_device_info(self, device):
khenaidoo92e62c52018-10-03 14:02:54 -040086 log.debug('get_ofp_device_info_start', device_id=device.id)
87 ofp_device_info = self.devices_handlers[device.id].get_ofp_device_info(device)
88 log.debug('get_ofp_device_info_ends', device_id=device.id)
89 return ofp_device_info
khenaidoob9203542018-09-17 22:56:37 -040090
91 def get_ofp_port_info(self, device, port_no):
khenaidoo92e62c52018-10-03 14:02:54 -040092 log.debug('get_ofp_port_info_start', device_id=device.id, port_no=port_no)
93 ofp_port_info = self.devices_handlers[device.id].get_ofp_port_info(device, port_no)
94 log.debug('get_ofp_port_info_ends', device_id=device.id, port_no=port_no)
95 return ofp_port_info
khenaidoob9203542018-09-17 22:56:37 -040096
97 def adopt_device(self, device):
98 log.debug('adopt_device', device_id=device.id)
99 self.devices_handlers[device.id] = self.device_handler_class(self, device.id)
100 reactor.callLater(0, self.devices_handlers[device.id].activate, device)
101 log.debug('adopt_device_done', device_id=device.id)
102 return device
103
104 def reconcile_device(self, device):
105 raise NotImplementedError()
106
107 def abandon_device(self, device):
108 raise NotImplementedError()
109
110 def disable_device(self, device):
111 log.info('disable-device', device_id=device.id)
khenaidoo4d4802d2018-10-04 21:59:49 -0400112 reactor.callLater(0, self.devices_handlers[device.id].disable)
113 log.debug('disable-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400114 return device
115
116 def reenable_device(self, device):
117 log.info('reenable-device', device_id=device.id)
118 reactor.callLater(0, self.devices_handlers[device.id].reenable)
khenaidoo4d4802d2018-10-04 21:59:49 -0400119 log.info('reenable-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400120 return device
121
122 def reboot_device(self, device):
123 log.info('reboot-device', device_id=device.id)
124 reactor.callLater(0, self.devices_handlers[device.id].reboot)
khenaidoo4d4802d2018-10-04 21:59:49 -0400125 log.info('reboot-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400126 return device
127
128 def download_image(self, device, request):
129 raise NotImplementedError()
130
131 def get_image_download_status(self, device, request):
132 raise NotImplementedError()
133
134 def cancel_image_download(self, device, request):
135 raise NotImplementedError()
136
137 def activate_image_update(self, device, request):
138 raise NotImplementedError()
139
140 def revert_image_update(self, device, request):
141 raise NotImplementedError()
142
143 def self_test_device(self, device):
khenaidoo4d4802d2018-10-04 21:59:49 -0400144 log.info('self-test', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400145 result = reactor.callLater(0, self.devices_handlers[device.id].self_test_device)
khenaidoo4d4802d2018-10-04 21:59:49 -0400146 log.info('self-test-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400147 return result
148
149 def delete_device(self, device):
150 log.info('delete-device', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400151 reactor.callLater(0, self.devices_handlers[device.id].delete)
khenaidoo4d4802d2018-10-04 21:59:49 -0400152 log.info('delete-device-done', device_id=device.id)
khenaidoob9203542018-09-17 22:56:37 -0400153 return device
154
155 def get_device_details(self, device):
156 raise NotImplementedError()
157
158 def update_flows_bulk(self, device, flows, groups):
159 log.info('bulk-flow-update', device_id=device.id,
160 flows=flows, groups=groups)
161 assert len(groups.items) == 0
162 handler = self.devices_handlers[device.id]
163 return handler.update_flow_table(flows.items)
164
165 def update_flows_incrementally(self, device, flow_changes, group_changes):
166 log.info('incremental-flow-update', device_id=device.id,
167 flows=flow_changes, groups=group_changes)
168 # For now, there is no support for group changes
169 assert len(group_changes.to_add.items) == 0
170 assert len(group_changes.to_remove.items) == 0
171
172 handler = self.devices_handlers[device.id]
173 # Remove flows
174 if len(flow_changes.to_remove.items) != 0:
175 handler.remove_from_flow_table(flow_changes.to_remove.items)
176
177 # Add flows
178 if len(flow_changes.to_add.items) != 0:
179 handler.add_to_flow_table(flow_changes.to_add.items)
180
181 def update_pm_config(self, device, pm_config):
182 log.info("adapter-update-pm-config", device=device,
183 pm_config=pm_config)
184 handler = self.devices_handlers[device.id]
185 handler.update_pm_config(device, pm_config)
186
187 def send_proxied_message(self, proxy_address, msg):
188 raise NotImplementedError()
189
190 def receive_proxied_message(self, proxy_address, msg):
191 raise NotImplementedError()
192
193 def receive_packet_out(self, logical_device_id, egress_port_no, msg):
194 raise NotImplementedError()
195
196 def receive_inter_adapter_message(self, msg):
197 raise NotImplementedError()
198
199 def suppress_alarm(self, filter):
200 raise NotImplementedError()
201
202 def unsuppress_alarm(self, filter):
203 raise NotImplementedError()
204
205 def _get_handler(self, device):
206 if device.id in self.devices_handlers:
207 handler = self.devices_handlers[device.id]
208 if handler is not None:
209 return handler
210 return None
211
212"""
213OLT Adapter base class
214"""
215class OltAdapter(IAdapter):
216 def __init__(self, adapter_agent, config, device_handler_class, name,
217 vendor, version, device_type,
218 accepts_bulk_flow_update=True,
219 accepts_add_remove_flow_updates=False,
220 core_proxy=None):
221 super(OltAdapter, self).__init__(adapter_agent=adapter_agent,
222 config=config,
223 device_handler_class=device_handler_class,
224 name=name,
225 vendor=vendor,
226 version=version,
227 device_type=device_type,
228 vendor_id=None,
229 accepts_bulk_flow_update=accepts_bulk_flow_update,
230 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates,
231 core_proxy=None)
232 self.logical_device_id_to_root_device_id = dict()
233
234 def reconcile_device(self, device):
235 try:
236 self.devices_handlers[device.id] = self.device_handler_class(self, device.id)
237 # Work only required for devices that are in ENABLED state
238 if device.admin_state == AdminState.ENABLED:
239 reactor.callLater(0,
240 self.devices_handlers[device.id].reconcile,
241 device)
242 else:
243 # Invoke the children reconciliation which would setup the
244 # basic children data structures
245 self.adapter_agent.reconcile_child_devices(device.id)
246 return device
247 except Exception, e:
248 log.exception('Exception', e=e)
249
250 def send_proxied_message(self, proxy_address, msg):
251 log.info('send-proxied-message', proxy_address=proxy_address, msg=msg)
252 handler = self.devices_handlers[proxy_address.device_id]
253 handler.send_proxied_message(proxy_address, msg)
254
255 def receive_packet_out(self, logical_device_id, egress_port_no, msg):
256 def ldi_to_di(ldi):
257 di = self.logical_device_id_to_root_device_id.get(ldi)
258 if di is None:
259 logical_device = self.adapter_agent.get_logical_device(ldi)
260 di = logical_device.root_device_id
261 self.logical_device_id_to_root_device_id[ldi] = di
262 return di
263
264 device_id = ldi_to_di(logical_device_id)
265 handler = self.devices_handlers[device_id]
266 handler.packet_out(egress_port_no, msg)
267
268
269"""
270ONU Adapter base class
271"""
272
273
274class OnuAdapter(IAdapter):
275 def __init__(self, adapter_agent, config, device_handler_class, name,
276 vendor, version, device_type, vendor_id, accepts_bulk_flow_update=True,
277 accepts_add_remove_flow_updates=False):
278 super(OnuAdapter, self).__init__(adapter_agent=adapter_agent,
279 config=config,
280 device_handler_class=device_handler_class,
281 name=name,
282 vendor=vendor,
283 version=version,
284 device_type=device_type,
285 vendor_id=vendor_id,
286 accepts_bulk_flow_update=accepts_bulk_flow_update,
287 accepts_add_remove_flow_updates=accepts_add_remove_flow_updates,
288 core_proxy=None
289 )
290
291 def reconcile_device(self, device):
292 self.devices_handlers[device.id] = self.device_handler_class(self, device.id)
293 # Reconcile only if state was ENABLED
294 if device.admin_state == AdminState.ENABLED:
295 reactor.callLater(0,
296 self.devices_handlers[device.id].reconcile,
297 device)
298 return device
299
300 def receive_proxied_message(self, proxy_address, msg):
301 log.info('receive-proxied-message', proxy_address=proxy_address,
302 device_id=proxy_address.device_id, msg=msg)
303 # Device_id from the proxy_address is the olt device id. We need to
304 # get the onu device id using the port number in the proxy_address
305 device = self.adapter_agent. \
306 get_child_device_with_proxy_address(proxy_address)
307 if device:
308 handler = self.devices_handlers[device.id]
309 handler.receive_message(msg)