blob: 7ef9a69552f40d1d19a52f79a80da0b83977fdec [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
21
Stephane Barbarieee409292017-04-24 10:30:20 -040022import arrow
Zsolt Haraszti66862032016-11-28 14:28:39 -080023import structlog
Zsolt Harasztief05ad22017-01-07 22:08:06 -080024from google.protobuf.json_format import MessageToJson
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -080025from scapy.packet import Packet
Zsolt Haraszti66862032016-11-28 14:28:39 -080026from twisted.internet.defer import inlineCallbacks, returnValue
27from zope.interface import implementer
28
Zsolt Haraszti89a27302016-12-08 16:53:06 -080029from common.event_bus import EventBusClient
Zsolt Harasztief05ad22017-01-07 22:08:06 -080030from common.frameio.frameio import hexify
Zsolt Haraszti66862032016-11-28 14:28:39 -080031from voltha.adapters.interface import IAdapterAgent
32from voltha.protos import third_party
Stephane Barbarieee409292017-04-24 10:30:20 -040033from voltha.core.flow_decomposer import OUTPUT
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -050034from voltha.protos.device_pb2 import Device, Port, PmConfigs
Stephane Barbarieee409292017-04-24 10:30:20 -040035from voltha.protos.events_pb2 import AlarmEvent, AlarmEventType, \
36 AlarmEventSeverity, AlarmEventState, AlarmEventCategory
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -050037from voltha.protos.events_pb2 import KpiEvent
Zsolt Haraszti66862032016-11-28 14:28:39 -080038from voltha.protos.voltha_pb2 import DeviceGroup, LogicalDevice, \
Stephane Barbarieee409292017-04-24 10:30:20 -040039 LogicalPort, AdminState, OperStatus, AlarmFilterRuleKey
Zsolt Haraszti66862032016-11-28 14:28:39 -080040from voltha.registry import registry
41
42
Zsolt Haraszti66862032016-11-28 14:28:39 -080043@implementer(IAdapterAgent)
44class AdapterAgent(object):
45 """
46 Gate-keeper between CORE and device adapters.
47
48 On one side it interacts with Core's internal model and update/dispatch
49 mechanisms.
50
51 On the other side, it interacts with the adapters standard interface as
52 defined in
53 """
54
55 def __init__(self, adapter_name, adapter_cls):
56 self.adapter_name = adapter_name
57 self.adapter_cls = adapter_cls
58 self.core = registry('core')
59 self.adapter = None
60 self.adapter_node_proxy = None
61 self.root_proxy = self.core.get_proxy('/')
Zsolt Haraszti89a27302016-12-08 16:53:06 -080062 self._rx_event_subscriptions = {}
63 self._tx_event_subscriptions = {}
64 self.event_bus = EventBusClient()
Khen Nursimulud068d812017-03-06 11:44:18 -050065 self.packet_out_subscription = None
Zsolt Haraszti89a27302016-12-08 16:53:06 -080066 self.log = structlog.get_logger(adapter_name=adapter_name)
Zsolt Haraszti66862032016-11-28 14:28:39 -080067
68 @inlineCallbacks
69 def start(self):
Zsolt Haraszti89a27302016-12-08 16:53:06 -080070 self.log.debug('starting')
Zsolt Haraszti66862032016-11-28 14:28:39 -080071 config = self._get_adapter_config() # this may be None
Zsolt Haraszti89a27302016-12-08 16:53:06 -080072 try:
73 adapter = self.adapter_cls(self, config)
74 yield adapter.start()
Zsolt Haraszti656ecc62016-12-28 15:08:23 -080075 self.adapter = adapter
76 self.adapter_node_proxy = self._update_adapter_node()
77 self._update_device_types()
Zsolt Haraszti89a27302016-12-08 16:53:06 -080078 except Exception, e:
79 self.log.exception(e)
Zsolt Haraszti89a27302016-12-08 16:53:06 -080080 self.log.info('started')
Zsolt Haraszti66862032016-11-28 14:28:39 -080081 returnValue(self)
82
83 @inlineCallbacks
84 def stop(self):
Zsolt Haraszti89a27302016-12-08 16:53:06 -080085 self.log.debug('stopping')
Zsolt Haraszti66862032016-11-28 14:28:39 -080086 if self.adapter is not None:
87 yield self.adapter.stop()
88 self.adapter = None
Zsolt Haraszti89a27302016-12-08 16:53:06 -080089 self.log.info('stopped')
Zsolt Haraszti66862032016-11-28 14:28:39 -080090
91 def _get_adapter_config(self):
92 """
93 Opportunistically load persisted adapter configuration.
94 Return None if no configuration exists yet.
95 """
96 proxy = self.core.get_proxy('/')
97 try:
98 config = proxy.get('/adapters/' + self.adapter_name)
99 return config
100 except KeyError:
101 return None
102
103 def _update_adapter_node(self):
104 """
105 Creates or updates the adapter node object based on self
106 description from the adapter.
107 """
108
109 adapter_desc = self.adapter.adapter_descriptor()
110 assert adapter_desc.id == self.adapter_name
111 path = self._make_up_to_date(
112 '/adapters', self.adapter_name, adapter_desc)
113 return self.core.get_proxy(path)
114
115 def _update_device_types(self):
116 """
117 Make sure device types are registered in Core
118 """
119 device_types = self.adapter.device_types()
120 for device_type in device_types.items:
121 key = device_type.id
122 self._make_up_to_date('/device_types', key, device_type)
123
124 def _make_up_to_date(self, container_path, key, data):
125 full_path = container_path + '/' + str(key)
126 root_proxy = self.core.get_proxy('/')
127 try:
128 root_proxy.get(full_path)
129 root_proxy.update(full_path, data)
130 except KeyError:
131 root_proxy.add(container_path, data)
132 return full_path
133
Khen Nursimulud068d812017-03-06 11:44:18 -0500134 def _remove_node(self, container_path, key):
135 """
136 Remove a node from the data model
137 :param container_path: path to node
138 :param key: node
139 :return: None
140 """
141 full_path = container_path + '/' + str(key)
142 root_proxy = self.core.get_proxy('/')
143 try:
144 root_proxy.get(full_path)
145 root_proxy.remove(full_path)
146 except KeyError:
147 # Node does not exist
148 pass
149
Zsolt Haraszti66862032016-11-28 14:28:39 -0800150 # ~~~~~~~~~~~~~~~~~~~~~ Core-Facing Service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151
152 def adopt_device(self, device):
153 return self.adapter.adopt_device(device)
154
155 def abandon_device(self, device):
156 return self.adapter.abandon_device(device)
157
Khen Nursimulud068d812017-03-06 11:44:18 -0500158 def disable_device(self, device):
159 return self.adapter.disable_device(device)
160
161 def reenable_device(self, device):
162 return self.adapter.reenable_device(device)
163
164 def reboot_device(self, device):
165 return self.adapter.reboot_device(device)
166
167 def delete_device(self, device):
168 return self.adapter.delete_device(device)
169
170 def get_device_details(self, device):
171 return self.adapter.get_device_details(device)
Zsolt Haraszti66862032016-11-28 14:28:39 -0800172
Zsolt Harasztic5c5d102016-12-07 21:12:27 -0800173 def update_flows_bulk(self, device, flows, groups):
174 return self.adapter.update_flows_bulk(device, flows, groups)
175
176 def update_flows_incrementally(self, device, flow_changes, group_changes):
177 return self.update_flows_incrementally(
178 device, flow_changes, group_changes)
179
Stephane Barbarieee409292017-04-24 10:30:20 -0400180 # def update_pm_collection(self, device, pm_collection_config):
Sergio Slobodriana2eb52b2017-03-07 12:24:46 -0500181 # return self.adapter.update_pm_collection(device, pm_collection_config)
182
183
Zsolt Haraszti66862032016-11-28 14:28:39 -0800184 # ~~~~~~~~~~~~~~~~~~~ Adapter-Facing Service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185
186 def get_device(self, device_id):
187 return self.root_proxy.get('/devices/{}'.format(device_id))
188
189 def add_device(self, device):
190 assert isinstance(device, Device)
191 self._make_up_to_date('/devices', device.id, device)
192
alshabibbe8ca2e2017-02-01 18:28:57 -0800193 # Ultimately, assign devices to device grpups.
194 # see https://jira.opencord.org/browse/CORD-838
Zsolt Haraszti66862032016-11-28 14:28:39 -0800195
196 dg = DeviceGroup(id='1')
197 self._make_up_to_date('/device_groups', dg.id, dg)
198
199 # add device to device group
alshabibbe8ca2e2017-02-01 18:28:57 -0800200 # see https://jira.opencord.org/browse/CORD-838
Zsolt Haraszti66862032016-11-28 14:28:39 -0800201
202 def update_device(self, device):
203 assert isinstance(device, Device)
204
205 # we run the update through the device_agent so that the change
206 # does not loop back to the adapter unnecessarily
207 device_agent = self.core.get_device_agent(device.id)
208 device_agent.update_device(device)
209
Sergio Slobodrian2db4c102017-03-09 22:29:23 -0500210 def update_device_pm_config(self, device_pm_config, init=False):
211 assert isinstance(device_pm_config, PmConfigs)
212
213 # we run the update through the device_agent so that the change
214 # does not loop back to the adapter unnecessarily
215 device_agent = self.core.get_device_agent(device_pm_config.id)
Stephane Barbarieee409292017-04-24 10:30:20 -0400216 device_agent.update_device_pm_config(device_pm_config, init)
Sergio Slobodrian2db4c102017-03-09 22:29:23 -0500217
Sergio Slobodrian98eff412017-03-15 14:46:30 -0400218 def update_adapter_pm_config(self, device_id, device_pm_config):
219 device = self.get_device(device_id)
Sergio Slobodrian2db4c102017-03-09 22:29:23 -0500220 self.adapter.update_pm_config(device, device_pm_config)
221
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400222 def _add_peer_reference(self, device_id, port):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800223 # for referential integrity, add/augment references
224 port.device_id = device_id
225 me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
226 for peer in port.peers:
227 peer_port_path = '/devices/{}/ports/{}'.format(
228 peer.device_id, peer.port_no)
229 peer_port = self.root_proxy.get(peer_port_path)
230 if me_as_peer not in peer_port.peers:
231 new = peer_port.peers.add()
232 new.CopyFrom(me_as_peer)
233 self.root_proxy.update(peer_port_path, peer_port)
234
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400235 def _del_peer_reference(self, device_id, port):
236 me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
237 for peer in port.peers:
238 peer_port_path = '/devices/{}/ports/{}'.format(
239 peer.device_id, peer.port_no)
240 peer_port = self.root_proxy.get(peer_port_path)
241 if me_as_peer in peer_port.peers:
242 peer_port.peers.remove(me_as_peer)
243 self.root_proxy.update(peer_port_path, peer_port)
244
245 def add_port(self, device_id, port):
246 assert isinstance(port, Port)
247
248 # for referential integrity, add/augment references
249 self._add_peer_reference(device_id, port)
250
251 # Add port
Zsolt Haraszti66862032016-11-28 14:28:39 -0800252 self._make_up_to_date('/devices/{}/ports'.format(device_id),
253 port.port_no, port)
254
Khen Nursimulud068d812017-03-06 11:44:18 -0500255 def disable_all_ports(self, device_id):
256 """
257 Disable all ports on that device, i.e. change the admin status to
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400258 disable and operational status to UNKNOWN.
Khen Nursimulud068d812017-03-06 11:44:18 -0500259 :param device_id: device id
260 :return: None
261 """
262
263 # get all device ports
264 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
265 for port in ports:
266 port.admin_state = AdminState.DISABLED
267 port.oper_status = OperStatus.UNKNOWN
268 self._make_up_to_date('/devices/{}/ports'.format(device_id),
269 port.port_no, port)
270
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400271 def enable_all_ports(self, device_id):
Khen Nursimulud068d812017-03-06 11:44:18 -0500272 """
273 Re-enable all ports on that device, i.e. change the admin status to
274 enabled and operational status to ACTIVE
275 :param device_id: device id
276 :return: None
277 """
278
279 # get all device ports
280 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
281 for port in ports:
282 port.admin_state = AdminState.ENABLED
283 port.oper_status = OperStatus.ACTIVE
284 self._make_up_to_date('/devices/{}/ports'.format(device_id),
285 port.port_no, port)
286
287 def delete_all_peer_references(self, device_id):
288 """
289 Remove all peer port references for that device
290 :param device_id: device_id of device
291 :return: None
292 """
293 ports = self.root_proxy.get('/devices/{}/ports'.format(device_id))
294 for port in ports:
295 port_path = '/devices/{}/ports/{}'.format(device_id, port.port_no)
296 for peer in port.peers:
297 port.peers.remove(peer)
298 self.root_proxy.update(port_path, port)
299
300 def delete_port_reference_from_parent(self, device_id, port):
301 """
302 Delete the port reference from the parent device
303 :param device_id: id of device containing the port
304 :param port: port to remove
305 :return: None
306 """
307 assert isinstance(port, Port)
308 self.log.info('delete-port-reference', device_id=device_id, port=port)
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400309 self._del_peer_reference(device_id, port)
Khen Nursimulud068d812017-03-06 11:44:18 -0500310
Khen Nursimuluc60afa12017-03-13 14:33:50 -0400311 def add_port_reference_to_parent(self, device_id, port):
312 """
313 Add the port reference to the parent device
314 :param device_id: id of device containing the port
315 :param port: port to add
316 :return: None
317 """
318 assert isinstance(port, Port)
319 self.log.info('add-port-reference', device_id=device_id, port=port)
320 self._add_peer_reference(device_id, port)
Khen Nursimulud068d812017-03-06 11:44:18 -0500321
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800322 def _find_first_available_id(self):
323 logical_devices = self.root_proxy.get('/logical_devices')
324 existing_ids = set(ld.id for ld in logical_devices)
325 existing_datapath_ids = set(ld.datapath_id for ld in logical_devices)
326 i = 1
327 while True:
328 if i not in existing_datapath_ids and str(i) not in existing_ids:
329 return i
330 i += 1
331
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800332 def get_logical_device(self, logical_device_id):
333 return self.root_proxy.get('/logical_devices/{}'.format(
334 logical_device_id))
335
Khen Nursimulud068d812017-03-06 11:44:18 -0500336 def get_logical_port(self, logical_device_id, port_id):
337 return self.root_proxy.get('/logical_devices/{}/ports/{}'.format(
338 logical_device_id, port_id))
339
Zsolt Haraszti66862032016-11-28 14:28:39 -0800340 def create_logical_device(self, logical_device):
341 assert isinstance(logical_device, LogicalDevice)
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800342
343 if not logical_device.id:
344 id = self._find_first_available_id()
345 logical_device.id = str(id)
346 logical_device.datapath_id = id
347
Zsolt Haraszti66862032016-11-28 14:28:39 -0800348 self._make_up_to_date('/logical_devices',
349 logical_device.id, logical_device)
350
Khen Nursimulud068d812017-03-06 11:44:18 -0500351 # Keep a reference to the packet out subscription as it will be
352 # referred during removal
353 self.packet_out_subscription = self.event_bus.subscribe(
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800354 topic='packet-out:{}'.format(logical_device.id),
355 callback=lambda _, p: self.receive_packet_out(logical_device.id, p)
356 )
357
Zsolt Harasztid036b7e2016-12-23 15:36:01 -0800358 return logical_device
359
Khen Nursimulud068d812017-03-06 11:44:18 -0500360 def delete_logical_device(self, logical_device):
361 """
362 This will remove the logical device as well as all logical ports
363 associated with it
364 :param logical_device: The logical device to remove
365 :return: None
366 """
367 assert isinstance(logical_device, LogicalDevice)
368
369 # Remove packet out subscription
370 self.event_bus.unsubscribe(self.packet_out_subscription)
371
372 # Remove node from the data model - this will trigger the logical
373 # device 'remove callbacks' as well as logical ports 'remove
374 # callbacks' if present
375 self._remove_node('/logical_devices', logical_device.id)
376
Zsolt Haraszti656ecc62016-12-28 15:08:23 -0800377 def receive_packet_out(self, logical_device_id, ofp_packet_out):
378
379 def get_port_out(opo):
380 for action in opo.actions:
381 if action.type == OUTPUT:
382 return action.output.port
383
384 out_port = get_port_out(ofp_packet_out)
385 frame = ofp_packet_out.data
386 self.adapter.receive_packet_out(logical_device_id, out_port, frame)
387
Zsolt Haraszti66862032016-11-28 14:28:39 -0800388 def add_logical_port(self, logical_device_id, port):
389 assert isinstance(port, LogicalPort)
390 self._make_up_to_date(
391 '/logical_devices/{}/ports'.format(logical_device_id),
392 port.id, port)
393
Khen Nursimulud068d812017-03-06 11:44:18 -0500394 def delete_logical_port(self, logical_device_id, port):
395 assert isinstance(port, LogicalPort)
396 self._remove_node('/logical_devices/{}/ports'.format(
397 logical_device_id), port.id)
398
399 def update_logical_port(self, logical_device_id, port):
400 assert isinstance(port, LogicalPort)
401 self.log.debug('update-logical-port',
402 logical_device_id=logical_device_id,
403 port=port)
404
405 self._make_up_to_date(
406 '/logical_devices/{}/ports'.format(logical_device_id),
407 port.id, port)
408
Zsolt Haraszti66862032016-11-28 14:28:39 -0800409 def child_device_detected(self,
410 parent_device_id,
411 parent_port_no,
412 child_device_type,
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800413 proxy_address,
414 **kw):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800415 # we create new ONU device objects and insert them into the config
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800416 # TODO should we auto-enable the freshly created device? Probably.
Zsolt Haraszti66862032016-11-28 14:28:39 -0800417 device = Device(
418 id=uuid4().hex[:12],
419 type=child_device_type,
420 parent_id=parent_device_id,
421 parent_port_no=parent_port_no,
422 admin_state=AdminState.ENABLED,
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800423 proxy_address=proxy_address,
424 **kw
Zsolt Haraszti66862032016-11-28 14:28:39 -0800425 )
426 self._make_up_to_date(
427 '/devices', device.id, device)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800428
429 topic = self._gen_tx_proxy_address_topic(proxy_address)
430 self._tx_event_subscriptions[topic] = self.event_bus.subscribe(
431 topic, lambda t, m: self._send_proxied_message(proxy_address, m))
432
Khen Nursimulud068d812017-03-06 11:44:18 -0500433 def remove_all_logical_ports(self, logical_device_id):
434 """ Remove all logical ports from a given logical device"""
435 ports = self.root_proxy.get('/logical_devices/{}/ports')
436 for port in ports:
437 self._remove_node('/logical_devices/{}/ports', port.id)
438
439 def delete_all_child_devices(self, parent_device_id):
440 """ Remove all ONUs from a given OLT """
441 devices = self.root_proxy.get('/devices')
442 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
443 self.log.debug('devices-to-delete',
444 parent_id=parent_device_id,
445 children_ids=children_ids)
446 for child_id in children_ids:
447 self._remove_node('/devices', child_id)
448
khenaidoo2d7af132017-03-23 15:45:51 -0400449 def update_child_devices_state(self,
450 parent_device_id,
451 oper_status=None,
452 connect_status=None,
453 admin_state=None):
454 """ Update status of all child devices """
Khen Nursimulud068d812017-03-06 11:44:18 -0500455 devices = self.root_proxy.get('/devices')
456 children_ids = set(d.id for d in devices if d.parent_id == parent_device_id)
khenaidoo2d7af132017-03-23 15:45:51 -0400457 self.log.debug('update-devices',
Khen Nursimulud068d812017-03-06 11:44:18 -0500458 parent_id=parent_device_id,
khenaidoo2d7af132017-03-23 15:45:51 -0400459 children_ids=children_ids,
460 oper_status=oper_status,
461 connect_status=connect_status,
462 admin_state=admin_state)
Khen Nursimulud068d812017-03-06 11:44:18 -0500463
khenaidoo71d0a6c2017-03-22 21:46:04 -0400464 for child_id in children_ids:
465 device = self.get_device(child_id)
khenaidoo2d7af132017-03-23 15:45:51 -0400466 if oper_status:
467 device.oper_status = oper_status
468 if connect_status:
469 device.connect_status = connect_status
470 if admin_state:
471 device.admin_state = admin_state
khenaidoo71d0a6c2017-03-22 21:46:04 -0400472 self._make_up_to_date(
473 '/devices', device.id, device)
474
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800475 def _gen_rx_proxy_address_topic(self, proxy_address):
476 """Generate unique topic name specific to this proxy address for rx"""
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800477 topic = 'rx:' + MessageToJson(proxy_address)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800478 return topic
479
480 def _gen_tx_proxy_address_topic(self, proxy_address):
481 """Generate unique topic name specific to this proxy address for tx"""
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800482 topic = 'tx:' + MessageToJson(proxy_address)
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800483 return topic
484
485 def register_for_proxied_messages(self, proxy_address):
486 topic = self._gen_rx_proxy_address_topic(proxy_address)
487 self._rx_event_subscriptions[topic] = self.event_bus.subscribe(
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500488 topic,
489 lambda t, m: self._receive_proxied_message(proxy_address, m))
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800490
Khen Nursimulud068d812017-03-06 11:44:18 -0500491 def unregister_for_proxied_messages(self, proxy_address):
492 topic = self._gen_rx_proxy_address_topic(proxy_address)
493 self.event_bus.unsubscribe(self._rx_event_subscriptions[topic])
494 del self._rx_event_subscriptions[topic]
495
Zsolt Haraszti89a27302016-12-08 16:53:06 -0800496 def _receive_proxied_message(self, proxy_address, msg):
497 self.adapter.receive_proxied_message(proxy_address, msg)
498
499 def send_proxied_message(self, proxy_address, msg):
500 topic = self._gen_tx_proxy_address_topic(proxy_address)
501 self.event_bus.publish(topic, msg)
502
503 def _send_proxied_message(self, proxy_address, msg):
504 self.adapter.send_proxied_message(proxy_address, msg)
505
506 def receive_proxied_message(self, proxy_address, msg):
507 topic = self._gen_rx_proxy_address_topic(proxy_address)
508 self.event_bus.publish(topic, msg)
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800509
510 # ~~~~~~~~~~~~~~~~~~ Handling packet-in and packet-out ~~~~~~~~~~~~~~~~~~~~
511
512 def send_packet_in(self, logical_device_id, logical_port_no, packet):
513 self.log.debug('send-packet-in', logical_device_id=logical_device_id,
Zsolt Harasztief05ad22017-01-07 22:08:06 -0800514 logical_port_no=logical_port_no, packet=hexify(packet))
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800515
516 if isinstance(packet, Packet):
517 packet = str(packet)
518
519 topic = 'packet-in:' + logical_device_id
520 self.event_bus.publish(topic, (logical_port_no, packet))
Zsolt Haraszti749b0952017-01-18 09:02:35 -0800521
522 # ~~~~~~~~~~~~~~~~~~~ Handling KPI metric submissions ~~~~~~~~~~~~~~~~~~~~~
Zsolt Harasztic5f740b2017-01-18 09:53:17 -0800523
Zsolt Haraszti749b0952017-01-18 09:02:35 -0800524 def submit_kpis(self, kpi_event_msg):
525 try:
526 assert isinstance(kpi_event_msg, KpiEvent)
527 self.event_bus.publish('kpis', kpi_event_msg)
528 except Exception as e:
529 self.log.exception('failed-kpi-submission',
530 type=type(kpi_event_msg))
Stephane Barbarie52198b92017-03-02 13:44:46 -0500531
532 # ~~~~~~~~~~~~~~~~~~~ Handle alarm submissions ~~~~~~~~~~~~~~~~~~~~~
533
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500534 def create_alarm(self, id=None, resource_id=None, description=None,
535 raised_ts=0, changed_ts=0,
536 type=AlarmEventType.EQUIPMENT,
Stephane Barbariebf3e10c2017-03-03 10:15:58 -0500537 category=AlarmEventCategory.PON,
Stephane Barbariecc6b2e62017-03-02 14:35:55 -0500538 severity=AlarmEventSeverity.MINOR,
539 state=AlarmEventState.RAISED,
Stephane Barbarie52198b92017-03-02 13:44:46 -0500540 context=None):
541
542 # Construct the ID if it is not provided
543 if id == None:
544 id = 'voltha.{}.{}'.format(self.adapter_name, resource_id)
545
546 return AlarmEvent(
547 id=id,
548 resource_id=resource_id,
549 type=type,
550 category=category,
551 severity=severity,
552 state=state,
553 description=description,
554 reported_ts=arrow.utcnow().timestamp,
555 raised_ts=raised_ts,
556 changed_ts=changed_ts,
557 context=context
558 )
559
Stephane Barbarieee409292017-04-24 10:30:20 -0400560 def filter_alarm(self, device_id, alarm_event):
561 alarm_filters = self.root_proxy.get('/alarm_filters')
562
563 rule_values = {
564 'id': alarm_event.id,
565 'type': AlarmEventType.AlarmEventType.Name(alarm_event.type),
566 'category': AlarmEventCategory.AlarmEventCategory.Name(alarm_event.category),
567 'severity': AlarmEventSeverity.AlarmEventSeverity.Name(alarm_event.severity),
568 'resource_id': alarm_event.resource_id,
569 'device_id': device_id
570 }
571
572 for alarm_filter in alarm_filters:
573 if alarm_filter.rules:
574 exclude = True
575 for rule in alarm_filter.rules:
576 self.log.debug("compare-alarm-event",
577 key=AlarmFilterRuleKey.AlarmFilterRuleKey.Name(rule.key),
578 actual=rule_values[AlarmFilterRuleKey.AlarmFilterRuleKey.Name(rule.key).lower()],
579 expected=rule.value.lower())
580 exclude = exclude and \
581 (rule_values[AlarmFilterRuleKey.AlarmFilterRuleKey.Name(
582 rule.key).lower()] == rule.value.lower())
583 if not exclude:
584 break
585
586 if exclude:
587 self.log.info("filtered-alarm-event", alarm=alarm_event)
588 return True
589
590 return False
591
592 def submit_alarm(self, device_id, alarm_event_msg):
Stephane Barbarie52198b92017-03-02 13:44:46 -0500593 try:
594 assert isinstance(alarm_event_msg, AlarmEvent)
Stephane Barbarieee409292017-04-24 10:30:20 -0400595 if not self.filter_alarm(device_id, alarm_event_msg):
596 self.event_bus.publish('alarms', alarm_event_msg)
Stephane Barbarie52198b92017-03-02 13:44:46 -0500597
598 except Exception as e:
599 self.log.exception('failed-alarm-submission',
600 type=type(alarm_event_msg))