blob: e926d9dd80bd917c73c048b289bf9db85f7d90a1 [file] [log] [blame]
Zsolt Haraszti66862032016-11-28 14:28:39 -08001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti66862032016-11-28 14:28:39 -08003#
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"""
18Agent to play gateway between CORE and an individual adapter.
19"""
20from uuid import uuid4
Stephane Barbarie52198b92017-03-02 13:44:46 -050021import arrow
22import re
Zsolt Haraszti66862032016-11-28 14:28:39 -080023
24import structlog
Zsolt Harasztief05ad22017-01-07 22:08:06 -080025from google.protobuf.json_format import MessageToJson
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -080026from scapy.packet import Packet
Zsolt Haraszti66862032016-11-28 14:28:39 -080027from twisted.internet.defer import inlineCallbacks, returnValue
28from zope.interface import implementer
29
Zsolt Haraszti89a27302016-12-08 16:53:06 -080030from common.event_bus import EventBusClient
Zsolt Harasztief05ad22017-01-07 22:08:06 -080031from common.frameio.frameio import hexify
Zsolt Haraszti66862032016-11-28 14:28:39 -080032from voltha.adapters.interface import IAdapterAgent
33from voltha.protos import third_party
34from voltha.protos.device_pb2 import Device, Port
Stephane Barbariecc6b2e62017-03-02 14:35:55 -050035from voltha.protos.events_pb2 import KpiEvent, AlarmEvent, AlarmEventType, \
36 AlarmEventSeverity, AlarmEventState, AlarmEventCategory
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -050037from voltha.protos.device_pb2 import Device, Port, PmConfigs
38from voltha.protos.events_pb2 import KpiEvent
Zsolt Haraszti66862032016-11-28 14:28:39 -080039from voltha.protos.voltha_pb2 import DeviceGroup, LogicalDevice, \
Khen Nursimulud068d812017-03-06 11:44:18 -050040 LogicalPort, AdminState, OperStatus
Zsolt Haraszti66862032016-11-28 14:28:39 -080041from voltha.registry import registry
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080042from voltha.core.flow_decomposer import OUTPUT
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -050043import sys
Zsolt Haraszti66862032016-11-28 14:28:39 -080044
45
Zsolt Haraszti66862032016-11-28 14:28:39 -080046@implementer(IAdapterAgent)
47class AdapterAgent(object):
48 """
49 Gate-keeper between CORE and device adapters.
50
51 On one side it interacts with Core's internal model and update/dispatch
52 mechanisms.
53
54 On the other side, it interacts with the adapters standard interface as
55 defined in
56 """
57
58 def __init__(self, adapter_name, adapter_cls):
59 self.adapter_name = adapter_name
60 self.adapter_cls = adapter_cls
61 self.core = registry('core')
62 self.adapter = None
63 self.adapter_node_proxy = None
64 self.root_proxy = self.core.get_proxy('/')
Zsolt Haraszti89a27302016-12-08 16:53:06 -080065 self._rx_event_subscriptions = {}
66 self._tx_event_subscriptions = {}
67 self.event_bus = EventBusClient()
Khen Nursimulud068d812017-03-06 11:44:18 -050068 self.packet_out_subscription = None
Zsolt Haraszti89a27302016-12-08 16:53:06 -080069 self.log = structlog.get_logger(adapter_name=adapter_name)
Zsolt Haraszti66862032016-11-28 14:28:39 -080070
71 @inlineCallbacks
72 def start(self):
Zsolt Haraszti89a27302016-12-08 16:53:06 -080073 self.log.debug('starting')
Zsolt Haraszti66862032016-11-28 14:28:39 -080074 config = self._get_adapter_config() # this may be None
Zsolt Haraszti89a27302016-12-08 16:53:06 -080075 try:
76 adapter = self.adapter_cls(self, config)
77 yield adapter.start()
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080078 self.adapter = adapter
79 self.adapter_node_proxy = self._update_adapter_node()
80 self._update_device_types()
Zsolt Haraszti89a27302016-12-08 16:53:06 -080081 except Exception, e:
82 self.log.exception(e)
Zsolt Haraszti89a27302016-12-08 16:53:06 -080083 self.log.info('started')
Zsolt Haraszti66862032016-11-28 14:28:39 -080084 returnValue(self)
85
86 @inlineCallbacks
87 def stop(self):
Zsolt Haraszti89a27302016-12-08 16:53:06 -080088 self.log.debug('stopping')
Zsolt Haraszti66862032016-11-28 14:28:39 -080089 if self.adapter is not None:
90 yield self.adapter.stop()
91 self.adapter = None
Zsolt Haraszti89a27302016-12-08 16:53:06 -080092 self.log.info('stopped')
Zsolt Haraszti66862032016-11-28 14:28:39 -080093
94 def _get_adapter_config(self):
95 """
96 Opportunistically load persisted adapter configuration.
97 Return None if no configuration exists yet.
98 """
99 proxy = self.core.get_proxy('/')
100 try:
101 config = proxy.get('/adapters/' + self.adapter_name)
102 return config
103 except KeyError:
104 return None
105
106 def _update_adapter_node(self):
107 """
108 Creates or updates the adapter node object based on self
109 description from the adapter.
110 """
111
112 adapter_desc = self.adapter.adapter_descriptor()
113 assert adapter_desc.id == self.adapter_name
114 path = self._make_up_to_date(
115 '/adapters', self.adapter_name, adapter_desc)
116 return self.core.get_proxy(path)
117
118 def _update_device_types(self):
119 """
120 Make sure device types are registered in Core
121 """
122 device_types = self.adapter.device_types()
123 for device_type in device_types.items:
124 key = device_type.id
125 self._make_up_to_date('/device_types', key, device_type)
126
127 def _make_up_to_date(self, container_path, key, data):
128 full_path = container_path + '/' + str(key)
129 root_proxy = self.core.get_proxy('/')
130 try:
131 root_proxy.get(full_path)
132 root_proxy.update(full_path, data)
133 except KeyError:
134 root_proxy.add(container_path, data)
135 return full_path
136
Khen Nursimulud068d812017-03-06 11:44:18 -0500137 def _remove_node(self, container_path, key):
138 """
139 Remove a node from the data model
140 :param container_path: path to node
141 :param key: node
142 :return: None
143 """
144 full_path = container_path + '/' + str(key)
145 root_proxy = self.core.get_proxy('/')
146 try:
147 root_proxy.get(full_path)
148 root_proxy.remove(full_path)
149 except KeyError:
150 # Node does not exist
151 pass
152
Zsolt Haraszti66862032016-11-28 14:28:39 -0800153 # ~~~~~~~~~~~~~~~~~~~~~ Core-Facing Service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154
155 def adopt_device(self, device):
156 return self.adapter.adopt_device(device)
157
158 def abandon_device(self, device):
159 return self.adapter.abandon_device(device)
160
Khen Nursimulud068d812017-03-06 11:44:18 -0500161 def disable_device(self, device):
162 return self.adapter.disable_device(device)
163
164 def reenable_device(self, device):
165 return self.adapter.reenable_device(device)
166
167 def reboot_device(self, device):
168 return self.adapter.reboot_device(device)
169
170 def delete_device(self, device):
171 return self.adapter.delete_device(device)
172
173 def get_device_details(self, device):
174 return self.adapter.get_device_details(device)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800175
Zsolt Harasztic5c5d102016-12-07 21:12:27 -0800176 def update_flows_bulk(self, device, flows, groups):
177 return self.adapter.update_flows_bulk(device, flows, groups)
178
179 def update_flows_incrementally(self, device, flow_changes, group_changes):
180 return self.update_flows_incrementally(
181 device, flow_changes, group_changes)
182
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -0500183 #def update_pm_collection(self, device, pm_collection_config):
184 # return self.adapter.update_pm_collection(device, pm_collection_config)
185
186
Zsolt Haraszti66862032016-11-28 14:28:39 -0800187 # ~~~~~~~~~~~~~~~~~~~ Adapter-Facing Service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188
189 def get_device(self, device_id):
190 return self.root_proxy.get('/devices/{}'.format(device_id))
191
192 def add_device(self, device):
193 assert isinstance(device, Device)
194 self._make_up_to_date('/devices', device.id, device)
195
alshabibbe8ca2e2017-02-01 18:28:57 -0800196 # Ultimately, assign devices to device grpups.
197 # see https://jira.opencord.org/browse/CORD-838
Zsolt Haraszti66862032016-11-28 14:28:39 -0800198
199 dg = DeviceGroup(id='1')
200 self._make_up_to_date('/device_groups', dg.id, dg)
201
202 # add device to device group
alshabibbe8ca2e2017-02-01 18:28:57 -0800203 # see https://jira.opencord.org/browse/CORD-838
Zsolt Haraszti66862032016-11-28 14:28:39 -0800204
205 def update_device(self, device):
206 assert isinstance(device, Device)
207
208 # we run the update through the device_agent so that the change
209 # does not loop back to the adapter unnecessarily
210 device_agent = self.core.get_device_agent(device.id)
211 device_agent.update_device(device)
212
Sergio Slobodrian2db4c102017-03-09 22:29:23 -0500213 def update_device_pm_config(self, device_pm_config, init=False):
214 assert isinstance(device_pm_config, PmConfigs)
215
216 # we run the update through the device_agent so that the change
217 # does not loop back to the adapter unnecessarily
218 device_agent = self.core.get_device_agent(device_pm_config.id)
219 device_agent.update_device_pm_config(device_pm_config,init)
220
221 def update_adapter_pm_config(self, device, device_pm_config):
222 self.adapter.update_pm_config(device, device_pm_config)
223
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400224 def _add_peer_reference(self, device_id, port):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800225 # for referential integrity, add/augment references
226 port.device_id = device_id
227 me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
228 for peer in port.peers:
229 peer_port_path = '/devices/{}/ports/{}'.format(
230 peer.device_id, peer.port_no)
231 peer_port = self.root_proxy.get(peer_port_path)
232 if me_as_peer not in peer_port.peers:
233 new = peer_port.peers.add()
234 new.CopyFrom(me_as_peer)
235 self.root_proxy.update(peer_port_path, peer_port)
236
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400237 def _del_peer_reference(self, device_id, port):
238 me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
239 for peer in port.peers:
240 peer_port_path = '/devices/{}/ports/{}'.format(
241 peer.device_id, peer.port_no)
242 peer_port = self.root_proxy.get(peer_port_path)
243 if me_as_peer in peer_port.peers:
244 peer_port.peers.remove(me_as_peer)
245 self.root_proxy.update(peer_port_path, peer_port)
246
247 def add_port(self, device_id, port):
248 assert isinstance(port, Port)
249
250 # for referential integrity, add/augment references
251 self._add_peer_reference(device_id, port)
252
253 # Add port
Zsolt Haraszti66862032016-11-28 14:28:39 -0800254 self._make_up_to_date('/devices/{}/ports'.format(device_id),
255 port.port_no, port)
256
Khen Nursimulud068d812017-03-06 11:44:18 -0500257 def disable_all_ports(self, device_id):
258 """
259 Disable all ports on that device, i.e. change the admin status to
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400260 disable and operational status to UNKNOWN.
Khen Nursimulud068d812017-03-06 11:44:18 -0500261 :param device_id: device id
262 :return: None
263 """
264
265 # get all device ports
266 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
267 for port in ports:
268 port.admin_state = AdminState.DISABLED
269 port.oper_status = OperStatus.UNKNOWN
270 self._make_up_to_date('/devices/{}/ports'.format(device_id),
271 port.port_no, port)
272
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400273
274 def enable_all_ports(self, device_id):
Khen Nursimulud068d812017-03-06 11:44:18 -0500275 """
276 Re-enable all ports on that device, i.e. change the admin status to
277 enabled and operational status to ACTIVE
278 :param device_id: device id
279 :return: None
280 """
281
282 # get all device ports
283 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
284 for port in ports:
285 port.admin_state = AdminState.ENABLED
286 port.oper_status = OperStatus.ACTIVE
287 self._make_up_to_date('/devices/{}/ports'.format(device_id),
288 port.port_no, port)
289
290 def delete_all_peer_references(self, device_id):
291 """
292 Remove all peer port references for that device
293 :param device_id: device_id of device
294 :return: None
295 """
296 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
297 for port in ports:
298 port_path = '/devices/{}/ports/{}'.format(device_id, port.port_no)
299 for peer in port.peers:
300 port.peers.remove(peer)
301 self.root_proxy.update(port_path, port)
302
303 def delete_port_reference_from_parent(self, device_id, port):
304 """
305 Delete the port reference from the parent device
306 :param device_id: id of device containing the port
307 :param port: port to remove
308 :return: None
309 """
310 assert isinstance(port, Port)
311 self.log.info('delete-port-reference', device_id=device_id, port=port)
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400312 self._del_peer_reference(device_id, port)
Khen Nursimulud068d812017-03-06 11:44:18 -0500313
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400314 def add_port_reference_to_parent(self, device_id, port):
315 """
316 Add the port reference to the parent device
317 :param device_id: id of device containing the port
318 :param port: port to add
319 :return: None
320 """
321 assert isinstance(port, Port)
322 self.log.info('add-port-reference', device_id=device_id, port=port)
323 self._add_peer_reference(device_id, port)
Khen Nursimulud068d812017-03-06 11:44:18 -0500324
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800325 def _find_first_available_id(self):
326 logical_devices = self.root_proxy.get('/logical_devices')
327 existing_ids = set(ld.id for ld in logical_devices)
328 existing_datapath_ids = set(ld.datapath_id for ld in logical_devices)
329 i = 1
330 while True:
331 if i not in existing_datapath_ids and str(i) not in existing_ids:
332 return i
333 i += 1
334
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800335 def get_logical_device(self, logical_device_id):
336 return self.root_proxy.get('/logical_devices/{}'.format(
337 logical_device_id))
338
Khen Nursimulud068d812017-03-06 11:44:18 -0500339 def get_logical_port(self, logical_device_id, port_id):
340 return self.root_proxy.get('/logical_devices/{}/ports/{}'.format(
341 logical_device_id, port_id))
342
Zsolt Haraszti66862032016-11-28 14:28:39 -0800343 def create_logical_device(self, logical_device):
344 assert isinstance(logical_device, LogicalDevice)
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800345
346 if not logical_device.id:
347 id = self._find_first_available_id()
348 logical_device.id = str(id)
349 logical_device.datapath_id = id
350
Zsolt Haraszti66862032016-11-28 14:28:39 -0800351 self._make_up_to_date('/logical_devices',
352 logical_device.id, logical_device)
353
Khen Nursimulud068d812017-03-06 11:44:18 -0500354 # Keep a reference to the packet out subscription as it will be
355 # referred during removal
356 self.packet_out_subscription = self.event_bus.subscribe(
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800357 topic='packet-out:{}'.format(logical_device.id),
358 callback=lambda _, p: self.receive_packet_out(logical_device.id, p)
359 )
360
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800361 return logical_device
362
Khen Nursimulud068d812017-03-06 11:44:18 -0500363
364 def delete_logical_device(self, logical_device):
365 """
366 This will remove the logical device as well as all logical ports
367 associated with it
368 :param logical_device: The logical device to remove
369 :return: None
370 """
371 assert isinstance(logical_device, LogicalDevice)
372
373 # Remove packet out subscription
374 self.event_bus.unsubscribe(self.packet_out_subscription)
375
376 # Remove node from the data model - this will trigger the logical
377 # device 'remove callbacks' as well as logical ports 'remove
378 # callbacks' if present
379 self._remove_node('/logical_devices', logical_device.id)
380
381
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800382 def receive_packet_out(self, logical_device_id, ofp_packet_out):
383
384 def get_port_out(opo):
385 for action in opo.actions:
386 if action.type == OUTPUT:
387 return action.output.port
388
389 out_port = get_port_out(ofp_packet_out)
390 frame = ofp_packet_out.data
391 self.adapter.receive_packet_out(logical_device_id, out_port, frame)
392
Zsolt Haraszti66862032016-11-28 14:28:39 -0800393 def add_logical_port(self, logical_device_id, port):
394 assert isinstance(port, LogicalPort)
395 self._make_up_to_date(
396 '/logical_devices/{}/ports'.format(logical_device_id),
397 port.id, port)
398
Khen Nursimulud068d812017-03-06 11:44:18 -0500399 def delete_logical_port(self, logical_device_id, port):
400 assert isinstance(port, LogicalPort)
401 self._remove_node('/logical_devices/{}/ports'.format(
402 logical_device_id), port.id)
403
404 def update_logical_port(self, logical_device_id, port):
405 assert isinstance(port, LogicalPort)
406 self.log.debug('update-logical-port',
407 logical_device_id=logical_device_id,
408 port=port)
409
410 self._make_up_to_date(
411 '/logical_devices/{}/ports'.format(logical_device_id),
412 port.id, port)
413
Zsolt Haraszti66862032016-11-28 14:28:39 -0800414 def child_device_detected(self,
415 parent_device_id,
416 parent_port_no,
417 child_device_type,
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800418 proxy_address,
419 **kw):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800420 # we create new ONU device objects and insert them into the config
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800421 # TODO should we auto-enable the freshly created device? Probably.
Zsolt Haraszti66862032016-11-28 14:28:39 -0800422 device = Device(
423 id=uuid4().hex[:12],
424 type=child_device_type,
425 parent_id=parent_device_id,
426 parent_port_no=parent_port_no,
427 admin_state=AdminState.ENABLED,
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800428 proxy_address=proxy_address,
429 **kw
Zsolt Haraszti66862032016-11-28 14:28:39 -0800430 )
431 self._make_up_to_date(
432 '/devices', device.id, device)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800433
434 topic = self._gen_tx_proxy_address_topic(proxy_address)
435 self._tx_event_subscriptions[topic] = self.event_bus.subscribe(
436 topic, lambda t, m: self._send_proxied_message(proxy_address, m))
437
Khen Nursimulud068d812017-03-06 11:44:18 -0500438 def remove_all_logical_ports(self, logical_device_id):
439 """ Remove all logical ports from a given logical device"""
440 ports = self.root_proxy.get('/logical_devices/{}/ports')
441 for port in ports:
442 self._remove_node('/logical_devices/{}/ports', port.id)
443
444 def delete_all_child_devices(self, parent_device_id):
445 """ Remove all ONUs from a given OLT """
446 devices = self.root_proxy.get('/devices')
447 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
448 self.log.debug('devices-to-delete',
449 parent_id=parent_device_id,
450 children_ids=children_ids)
451 for child_id in children_ids:
452 self._remove_node('/devices', child_id)
453
454 def reenable_all_child_devices(self, parent_device_id):
455 """ Re-enable all ONUs from a given OLT """
456 devices = self.root_proxy.get('/devices')
457 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
458 self.log.debug('devices-to-reenable',
459 parent_id=parent_device_id,
460 children_ids=children_ids)
461 for child_id in children_ids:
462 device = self.get_device(child_id)
463 device.admin_state = AdminState.ENABLED
464 self._make_up_to_date(
465 '/devices', device.id, device)
466
467 def disable_all_child_devices(self, parent_device_id):
468 """ Disable all ONUs from a given OLT """
469 devices = self.root_proxy.get('/devices')
470 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
471 self.log.debug('devices-to-disable',
472 parent_id=parent_device_id,
473 children_ids=children_ids)
474 for child_id in children_ids:
475 # Change the admin state pf the device to DISABLE
476 device = self.get_device(child_id)
477 device.admin_state = AdminState.DISABLED
478 self._make_up_to_date(
479 '/devices', device.id, device)
480
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800481 def _gen_rx_proxy_address_topic(self, proxy_address):
482 """Generate unique topic name specific to this proxy address for rx"""
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800483 topic = 'rx:' + MessageToJson(proxy_address)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800484 return topic
485
486 def _gen_tx_proxy_address_topic(self, proxy_address):
487 """Generate unique topic name specific to this proxy address for tx"""
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800488 topic = 'tx:' + MessageToJson(proxy_address)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800489 return topic
490
491 def register_for_proxied_messages(self, proxy_address):
492 topic = self._gen_rx_proxy_address_topic(proxy_address)
493 self._rx_event_subscriptions[topic] = self.event_bus.subscribe(
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500494 topic,
495 lambda t, m: self._receive_proxied_message(proxy_address, m))
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800496
Khen Nursimulud068d812017-03-06 11:44:18 -0500497 def unregister_for_proxied_messages(self, proxy_address):
498 topic = self._gen_rx_proxy_address_topic(proxy_address)
499 self.event_bus.unsubscribe(self._rx_event_subscriptions[topic])
500 del self._rx_event_subscriptions[topic]
501
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800502 def _receive_proxied_message(self, proxy_address, msg):
503 self.adapter.receive_proxied_message(proxy_address, msg)
504
505 def send_proxied_message(self, proxy_address, msg):
506 topic = self._gen_tx_proxy_address_topic(proxy_address)
507 self.event_bus.publish(topic, msg)
508
509 def _send_proxied_message(self, proxy_address, msg):
510 self.adapter.send_proxied_message(proxy_address, msg)
511
512 def receive_proxied_message(self, proxy_address, msg):
513 topic = self._gen_rx_proxy_address_topic(proxy_address)
514 self.event_bus.publish(topic, msg)
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800515
516 # ~~~~~~~~~~~~~~~~~~ Handling packet-in and packet-out ~~~~~~~~~~~~~~~~~~~~
517
518 def send_packet_in(self, logical_device_id, logical_port_no, packet):
519 self.log.debug('send-packet-in', logical_device_id=logical_device_id,
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800520 logical_port_no=logical_port_no, packet=hexify(packet))
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800521
522 if isinstance(packet, Packet):
523 packet = str(packet)
524
525 topic = 'packet-in:' + logical_device_id
526 self.event_bus.publish(topic, (logical_port_no, packet))
Zsolt Haraszti749b0952017-01-18 09:02:35 -0800527
528 # ~~~~~~~~~~~~~~~~~~~ Handling KPI metric submissions ~~~~~~~~~~~~~~~~~~~~~
Zsolt Harasztic5f740b2017-01-18 09:53:17 -0800529
Zsolt Haraszti749b0952017-01-18 09:02:35 -0800530 def submit_kpis(self, kpi_event_msg):
531 try:
532 assert isinstance(kpi_event_msg, KpiEvent)
533 self.event_bus.publish('kpis', kpi_event_msg)
534 except Exception as e:
535 self.log.exception('failed-kpi-submission',
536 type=type(kpi_event_msg))
Stephane Barbarie52198b92017-03-02 13:44:46 -0500537
538 # ~~~~~~~~~~~~~~~~~~~ Handle alarm submissions ~~~~~~~~~~~~~~~~~~~~~
539
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500540 def create_alarm(self, id=None, resource_id=None, description=None,
541 raised_ts=0, changed_ts=0,
542 type=AlarmEventType.EQUIPMENT,
Stephane Barbariebf3e10c2017-03-03 10:15:58 -0500543 category=AlarmEventCategory.PON,
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500544 severity=AlarmEventSeverity.MINOR,
545 state=AlarmEventState.RAISED,
Stephane Barbarie52198b92017-03-02 13:44:46 -0500546 context=None):
547
548 # Construct the ID if it is not provided
549 if id == None:
550 id = 'voltha.{}.{}'.format(self.adapter_name, resource_id)
551
552 return AlarmEvent(
553 id=id,
554 resource_id=resource_id,
555 type=type,
556 category=category,
557 severity=severity,
558 state=state,
559 description=description,
560 reported_ts=arrow.utcnow().timestamp,
561 raised_ts=raised_ts,
562 changed_ts=changed_ts,
563 context=context
564 )
565
566 def submit_alarm(self, alarm_event_msg):
567 try:
568 assert isinstance(alarm_event_msg, AlarmEvent)
569 self.event_bus.publish('alarms', alarm_event_msg)
570
571 except Exception as e:
572 self.log.exception('failed-alarm-submission',
573 type=type(alarm_event_msg))