blob: fcf8e2743929f498d4685e3bc01bf02752729255 [file] [log] [blame]
Scott Baker91bf91b2019-03-15 16:13:51 -07001
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 Bakere9855012019-04-01 15:01:34 -070016from __future__ import absolute_import
Scott Baker91bf91b2019-03-15 16:13:51 -070017
Scott Bakercabc3de2019-04-03 17:23:08 -070018import datetime
Scott Baker91bf91b2019-03-15 16:13:51 -070019import json
Scott Bakercabc3de2019-04-03 17:23:08 -070020import time
Scott Baker91bf91b2019-03-15 16:13:51 -070021from xossynchronizer.event_steps.eventstep import EventStep
22from xosconfig import Config
Scott Bakercabc3de2019-04-03 17:23:08 -070023from xoskafka import XOSKafkaProducer
Scott Baker91bf91b2019-03-15 16:13:51 -070024from multistructlog import create_logger
25
26log = create_logger(Config().get('logging'))
27
28
29class 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 Bakercabc3de2019-04-03 17:23:08 -070036 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 Baker91bf91b2019-03-15 16:13:51 -070063 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 Bakercabc3de2019-04-03 17:23:08 -070091 self.send_alarm(switch, port, value)