blob: d6e32af5c6c5b426c7cab7f931edd52843edb886 [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
Zsolt Haraszti66862032016-11-28 14:28:39 -0800224 def add_port(self, device_id, port):
225 assert isinstance(port, Port)
226
227 # for referential integrity, add/augment references
228 port.device_id = device_id
229 me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
230 for peer in port.peers:
231 peer_port_path = '/devices/{}/ports/{}'.format(
232 peer.device_id, peer.port_no)
233 peer_port = self.root_proxy.get(peer_port_path)
234 if me_as_peer not in peer_port.peers:
235 new = peer_port.peers.add()
236 new.CopyFrom(me_as_peer)
237 self.root_proxy.update(peer_port_path, peer_port)
238
239 self._make_up_to_date('/devices/{}/ports'.format(device_id),
240 port.port_no, port)
241
Khen Nursimulud068d812017-03-06 11:44:18 -0500242 def disable_all_ports(self, device_id):
243 """
244 Disable all ports on that device, i.e. change the admin status to
245 disable and operational status to UNKNOWN
246 :param device_id: device id
247 :return: None
248 """
249
250 # get all device ports
251 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
252 for port in ports:
253 port.admin_state = AdminState.DISABLED
254 port.oper_status = OperStatus.UNKNOWN
255 self._make_up_to_date('/devices/{}/ports'.format(device_id),
256 port.port_no, port)
257
258 def reenable_all_ports(self, device_id):
259 """
260 Re-enable all ports on that device, i.e. change the admin status to
261 enabled and operational status to ACTIVE
262 :param device_id: device id
263 :return: None
264 """
265
266 # get all device ports
267 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
268 for port in ports:
269 port.admin_state = AdminState.ENABLED
270 port.oper_status = OperStatus.ACTIVE
271 self._make_up_to_date('/devices/{}/ports'.format(device_id),
272 port.port_no, port)
273
274 def delete_all_peer_references(self, device_id):
275 """
276 Remove all peer port references for that device
277 :param device_id: device_id of device
278 :return: None
279 """
280 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
281 for port in ports:
282 port_path = '/devices/{}/ports/{}'.format(device_id, port.port_no)
283 for peer in port.peers:
284 port.peers.remove(peer)
285 self.root_proxy.update(port_path, port)
286
287 def delete_port_reference_from_parent(self, device_id, port):
288 """
289 Delete the port reference from the parent device
290 :param device_id: id of device containing the port
291 :param port: port to remove
292 :return: None
293 """
294 assert isinstance(port, Port)
295 self.log.info('delete-port-reference', device_id=device_id, port=port)
296
297 # for referential integrity, remove references
298 me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
299 for peer in port.peers:
300 peer_port_path = '/devices/{}/ports/{}'.format(
301 peer.device_id, peer.port_no)
302 peer_port = self.root_proxy.get(peer_port_path)
303 if me_as_peer in peer_port.peers:
304 peer_port.peers.remove(me_as_peer)
305 self.root_proxy.update(peer_port_path, peer_port)
306
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800307 def _find_first_available_id(self):
308 logical_devices = self.root_proxy.get('/logical_devices')
309 existing_ids = set(ld.id for ld in logical_devices)
310 existing_datapath_ids = set(ld.datapath_id for ld in logical_devices)
311 i = 1
312 while True:
313 if i not in existing_datapath_ids and str(i) not in existing_ids:
314 return i
315 i += 1
316
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800317 def get_logical_device(self, logical_device_id):
318 return self.root_proxy.get('/logical_devices/{}'.format(
319 logical_device_id))
320
Khen Nursimulud068d812017-03-06 11:44:18 -0500321 def get_logical_port(self, logical_device_id, port_id):
322 return self.root_proxy.get('/logical_devices/{}/ports/{}'.format(
323 logical_device_id, port_id))
324
Zsolt Haraszti66862032016-11-28 14:28:39 -0800325 def create_logical_device(self, logical_device):
326 assert isinstance(logical_device, LogicalDevice)
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800327
328 if not logical_device.id:
329 id = self._find_first_available_id()
330 logical_device.id = str(id)
331 logical_device.datapath_id = id
332
Zsolt Haraszti66862032016-11-28 14:28:39 -0800333 self._make_up_to_date('/logical_devices',
334 logical_device.id, logical_device)
335
Khen Nursimulud068d812017-03-06 11:44:18 -0500336 # Keep a reference to the packet out subscription as it will be
337 # referred during removal
338 self.packet_out_subscription = self.event_bus.subscribe(
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800339 topic='packet-out:{}'.format(logical_device.id),
340 callback=lambda _, p: self.receive_packet_out(logical_device.id, p)
341 )
342
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800343 return logical_device
344
Khen Nursimulud068d812017-03-06 11:44:18 -0500345
346 def delete_logical_device(self, logical_device):
347 """
348 This will remove the logical device as well as all logical ports
349 associated with it
350 :param logical_device: The logical device to remove
351 :return: None
352 """
353 assert isinstance(logical_device, LogicalDevice)
354
355 # Remove packet out subscription
356 self.event_bus.unsubscribe(self.packet_out_subscription)
357
358 # Remove node from the data model - this will trigger the logical
359 # device 'remove callbacks' as well as logical ports 'remove
360 # callbacks' if present
361 self._remove_node('/logical_devices', logical_device.id)
362
363
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800364 def receive_packet_out(self, logical_device_id, ofp_packet_out):
365
366 def get_port_out(opo):
367 for action in opo.actions:
368 if action.type == OUTPUT:
369 return action.output.port
370
371 out_port = get_port_out(ofp_packet_out)
372 frame = ofp_packet_out.data
373 self.adapter.receive_packet_out(logical_device_id, out_port, frame)
374
Zsolt Haraszti66862032016-11-28 14:28:39 -0800375 def add_logical_port(self, logical_device_id, port):
376 assert isinstance(port, LogicalPort)
377 self._make_up_to_date(
378 '/logical_devices/{}/ports'.format(logical_device_id),
379 port.id, port)
380
Khen Nursimulud068d812017-03-06 11:44:18 -0500381 def delete_logical_port(self, logical_device_id, port):
382 assert isinstance(port, LogicalPort)
383 self._remove_node('/logical_devices/{}/ports'.format(
384 logical_device_id), port.id)
385
386 def update_logical_port(self, logical_device_id, port):
387 assert isinstance(port, LogicalPort)
388 self.log.debug('update-logical-port',
389 logical_device_id=logical_device_id,
390 port=port)
391
392 self._make_up_to_date(
393 '/logical_devices/{}/ports'.format(logical_device_id),
394 port.id, port)
395
Zsolt Haraszti66862032016-11-28 14:28:39 -0800396 def child_device_detected(self,
397 parent_device_id,
398 parent_port_no,
399 child_device_type,
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800400 proxy_address,
401 **kw):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800402 # we create new ONU device objects and insert them into the config
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800403 # TODO should we auto-enable the freshly created device? Probably.
Zsolt Haraszti66862032016-11-28 14:28:39 -0800404 device = Device(
405 id=uuid4().hex[:12],
406 type=child_device_type,
407 parent_id=parent_device_id,
408 parent_port_no=parent_port_no,
409 admin_state=AdminState.ENABLED,
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800410 proxy_address=proxy_address,
411 **kw
Zsolt Haraszti66862032016-11-28 14:28:39 -0800412 )
413 self._make_up_to_date(
414 '/devices', device.id, device)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800415
416 topic = self._gen_tx_proxy_address_topic(proxy_address)
417 self._tx_event_subscriptions[topic] = self.event_bus.subscribe(
418 topic, lambda t, m: self._send_proxied_message(proxy_address, m))
419
Khen Nursimulud068d812017-03-06 11:44:18 -0500420 def remove_all_logical_ports(self, logical_device_id):
421 """ Remove all logical ports from a given logical device"""
422 ports = self.root_proxy.get('/logical_devices/{}/ports')
423 for port in ports:
424 self._remove_node('/logical_devices/{}/ports', port.id)
425
426 def delete_all_child_devices(self, parent_device_id):
427 """ Remove all ONUs from a given OLT """
428 devices = self.root_proxy.get('/devices')
429 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
430 self.log.debug('devices-to-delete',
431 parent_id=parent_device_id,
432 children_ids=children_ids)
433 for child_id in children_ids:
434 self._remove_node('/devices', child_id)
435
436 def reenable_all_child_devices(self, parent_device_id):
437 """ Re-enable all ONUs from a given OLT """
438 devices = self.root_proxy.get('/devices')
439 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
440 self.log.debug('devices-to-reenable',
441 parent_id=parent_device_id,
442 children_ids=children_ids)
443 for child_id in children_ids:
444 device = self.get_device(child_id)
445 device.admin_state = AdminState.ENABLED
446 self._make_up_to_date(
447 '/devices', device.id, device)
448
449 def disable_all_child_devices(self, parent_device_id):
450 """ Disable all ONUs from a given OLT """
451 devices = self.root_proxy.get('/devices')
452 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
453 self.log.debug('devices-to-disable',
454 parent_id=parent_device_id,
455 children_ids=children_ids)
456 for child_id in children_ids:
457 # Change the admin state pf the device to DISABLE
458 device = self.get_device(child_id)
459 device.admin_state = AdminState.DISABLED
460 self._make_up_to_date(
461 '/devices', device.id, device)
462
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800463 def _gen_rx_proxy_address_topic(self, proxy_address):
464 """Generate unique topic name specific to this proxy address for rx"""
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800465 topic = 'rx:' + MessageToJson(proxy_address)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800466 return topic
467
468 def _gen_tx_proxy_address_topic(self, proxy_address):
469 """Generate unique topic name specific to this proxy address for tx"""
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800470 topic = 'tx:' + MessageToJson(proxy_address)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800471 return topic
472
473 def register_for_proxied_messages(self, proxy_address):
474 topic = self._gen_rx_proxy_address_topic(proxy_address)
475 self._rx_event_subscriptions[topic] = self.event_bus.subscribe(
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500476 topic,
477 lambda t, m: self._receive_proxied_message(proxy_address, m))
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800478
Khen Nursimulud068d812017-03-06 11:44:18 -0500479 def unregister_for_proxied_messages(self, proxy_address):
480 topic = self._gen_rx_proxy_address_topic(proxy_address)
481 self.event_bus.unsubscribe(self._rx_event_subscriptions[topic])
482 del self._rx_event_subscriptions[topic]
483
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800484 def _receive_proxied_message(self, proxy_address, msg):
485 self.adapter.receive_proxied_message(proxy_address, msg)
486
487 def send_proxied_message(self, proxy_address, msg):
488 topic = self._gen_tx_proxy_address_topic(proxy_address)
489 self.event_bus.publish(topic, msg)
490
491 def _send_proxied_message(self, proxy_address, msg):
492 self.adapter.send_proxied_message(proxy_address, msg)
493
494 def receive_proxied_message(self, proxy_address, msg):
495 topic = self._gen_rx_proxy_address_topic(proxy_address)
496 self.event_bus.publish(topic, msg)
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800497
498 # ~~~~~~~~~~~~~~~~~~ Handling packet-in and packet-out ~~~~~~~~~~~~~~~~~~~~
499
500 def send_packet_in(self, logical_device_id, logical_port_no, packet):
501 self.log.debug('send-packet-in', logical_device_id=logical_device_id,
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800502 logical_port_no=logical_port_no, packet=hexify(packet))
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800503
504 if isinstance(packet, Packet):
505 packet = str(packet)
506
507 topic = 'packet-in:' + logical_device_id
508 self.event_bus.publish(topic, (logical_port_no, packet))
Zsolt Haraszti749b0952017-01-18 09:02:35 -0800509
510 # ~~~~~~~~~~~~~~~~~~~ Handling KPI metric submissions ~~~~~~~~~~~~~~~~~~~~~
Zsolt Harasztic5f740b2017-01-18 09:53:17 -0800511
Zsolt Haraszti749b0952017-01-18 09:02:35 -0800512 def submit_kpis(self, kpi_event_msg):
513 try:
514 assert isinstance(kpi_event_msg, KpiEvent)
515 self.event_bus.publish('kpis', kpi_event_msg)
516 except Exception as e:
517 self.log.exception('failed-kpi-submission',
518 type=type(kpi_event_msg))
Stephane Barbarie52198b92017-03-02 13:44:46 -0500519
520 # ~~~~~~~~~~~~~~~~~~~ Handle alarm submissions ~~~~~~~~~~~~~~~~~~~~~
521
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500522 def create_alarm(self, id=None, resource_id=None, description=None,
523 raised_ts=0, changed_ts=0,
524 type=AlarmEventType.EQUIPMENT,
Stephane Barbariebf3e10c2017-03-03 10:15:58 -0500525 category=AlarmEventCategory.PON,
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500526 severity=AlarmEventSeverity.MINOR,
527 state=AlarmEventState.RAISED,
Stephane Barbarie52198b92017-03-02 13:44:46 -0500528 context=None):
529
530 # Construct the ID if it is not provided
531 if id == None:
532 id = 'voltha.{}.{}'.format(self.adapter_name, resource_id)
533
534 return AlarmEvent(
535 id=id,
536 resource_id=resource_id,
537 type=type,
538 category=category,
539 severity=severity,
540 state=state,
541 description=description,
542 reported_ts=arrow.utcnow().timestamp,
543 raised_ts=raised_ts,
544 changed_ts=changed_ts,
545 context=context
546 )
547
548 def submit_alarm(self, alarm_event_msg):
549 try:
550 assert isinstance(alarm_event_msg, AlarmEvent)
551 self.event_bus.publish('alarms', alarm_event_msg)
552
553 except Exception as e:
554 self.log.exception('failed-alarm-submission',
555 type=type(alarm_event_msg))