Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 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 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 17 | |
Scott Baker | cabc3de | 2019-04-03 17:23:08 -0700 | [diff] [blame] | 18 | import datetime |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 19 | import json |
Scott Baker | cabc3de | 2019-04-03 17:23:08 -0700 | [diff] [blame] | 20 | import time |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 21 | from xossynchronizer.event_steps.eventstep import EventStep |
| 22 | from xosconfig import Config |
Scott Baker | cabc3de | 2019-04-03 17:23:08 -0700 | [diff] [blame] | 23 | from xoskafka import XOSKafkaProducer |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 24 | from multistructlog import create_logger |
| 25 | |
| 26 | log = create_logger(Config().get('logging')) |
| 27 | |
| 28 | |
| 29 | class OnosPortEventStep(EventStep): |
| 30 | topics = ["onos.events.port"] |
| 31 | technology = "kafka" |
| 32 | |
| 33 | def __init__(self, *args, **kwargs): |
| 34 | super(OnosPortEventStep, self).__init__(*args, **kwargs) |
| 35 | |
Scott Baker | cabc3de | 2019-04-03 17:23:08 -0700 | [diff] [blame] | 36 | def send_alarm(self, switch, port, value): |
| 37 | timestamp = time.mktime(datetime.datetime.strptime(value["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ").timetuple()) |
| 38 | state = "RAISED" if port.oper_status == "disabled" else "CLEARED" |
| 39 | |
| 40 | context = {"portId": port.portId, |
| 41 | "portKind": port.kind or "unknown", |
| 42 | "switch.name": switch.name} |
| 43 | |
| 44 | alarm = {"category": "SWITCH", |
| 45 | "reported_ts": time.time(), |
| 46 | "raised_ts": timestamp, |
| 47 | "state": state, |
| 48 | "alarm_type_name": "SWITCH.PORT_LOS", |
| 49 | "severity": "MAJOR", |
| 50 | "resource_id": switch.ofId, |
| 51 | "context": context, |
| 52 | "type": "COMMUNICATION", |
| 53 | "id": "xos.fabricservice.%s.SWITCH_PORT_LOS" % switch.ofId, |
| 54 | "description": "xos.fabricservice.%s - SWITCH PORT LOS Alarm -" |
| 55 | " SWITCH_PORT_LOS - %s" % (switch.ofId, state)} |
| 56 | |
| 57 | topic = "xos.alarms.fabric-service" |
| 58 | key = "%s:%s" % (switch.ofId, port.portId) |
| 59 | value = json.dumps(alarm, default=lambda o: repr(o)) |
| 60 | |
| 61 | XOSKafkaProducer.produce(topic, key, value) |
| 62 | |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 63 | def process_event(self, event): |
| 64 | value = json.loads(event.value) |
| 65 | |
| 66 | switch = self.model_accessor.Switch.objects.filter( |
| 67 | ofId=value["deviceId"] |
| 68 | ) |
| 69 | if not switch: |
| 70 | log.info("Event for unknown switch", deviceId=value["deviceId"]) |
| 71 | return |
| 72 | |
| 73 | switch = switch[0] |
| 74 | |
| 75 | port = self.model_accessor.SwitchPort.objects.filter( |
| 76 | switch_id=switch.id, |
| 77 | portId=value["portId"] |
| 78 | ) |
| 79 | if not port: |
| 80 | log.info("Event for unknown port", |
| 81 | deviceId=value["deviceId"], |
| 82 | portId=value["portId"]) |
| 83 | return |
| 84 | |
| 85 | port = port[0] |
| 86 | |
| 87 | oper_status = "enabled" if value["enabled"] else "disabled" |
| 88 | if oper_status != port.oper_status: |
| 89 | port.oper_status = oper_status |
| 90 | port.save_changed_fields() |
Scott Baker | cabc3de | 2019-04-03 17:23:08 -0700 | [diff] [blame] | 91 | self.send_alarm(switch, port, value) |