blob: 8a7049c8c13d9dd677e586715ab0c846e243d33e [file] [log] [blame]
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +01001/*
2 * Copyright 2018 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 */
16
17package org.onosproject.drivers.polatis.netconf;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.commons.configuration.HierarchicalConfiguration;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080021import org.onosproject.alarm.Alarm;
22import org.onosproject.alarm.AlarmConsumer;
23import org.onosproject.alarm.AlarmId;
24import org.onosproject.alarm.DefaultAlarm;
25import org.onosproject.alarm.XmlEventParser;
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010026import org.onosproject.net.DeviceId;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.netconf.NetconfController;
30import org.onosproject.netconf.NetconfException;
31import org.onosproject.mastership.MastershipService;
32
33import org.slf4j.Logger;
34
35import java.util.ArrayList;
36import java.util.List;
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010037
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080038import static org.onosproject.alarm.Alarm.SeverityLevel;
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010039import static com.google.common.base.Preconditions.checkNotNull;
40import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt;
41import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlEmpty;
42import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_SYSTEMALARMS;
43import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_SYSTEMALARMS_XMLNS;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Polatis specific implementation to provide a list of current alarms.
48 */
49public class PolatisAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer {
50 private final Logger log = getLogger(getClass());
51
52 private static final String ALARM_TIME = "alarm-time";
53 private static final String ALARM_TYPE = "alarm-type";
Laszlo Papp23e0c512018-09-18 16:19:17 +000054 private static final String ALARM_TYPE_LOS = "NOTIF_PORT_POWER";
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010055 private static final String ALARM_MESSAGE = "alarm-message";
56
57 private DeviceId deviceId;
58
59 @Override
60 public List<Alarm> consumeAlarms() {
61 DriverHandler handler = handler();
62 NetconfController controller = handler.get(NetconfController.class);
63 checkNotNull(controller, "Netconf controller is null");
64
65 MastershipService mastershipService = handler.get(MastershipService.class);
66 deviceId = handler.data().deviceId();
67
68 List<Alarm> alarms = new ArrayList<>();
69 if (!mastershipService.isLocalMaster(deviceId)) {
70 log.warn("Not master for {} Use {} to execute command",
71 deviceId,
72 mastershipService.getMasterFor(deviceId));
73 return ImmutableList.copyOf(alarms);
74 }
75
76 try {
77 String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS);
78 String reply = controller.getDevicesMap()
79 .get(deviceId)
80 .getSession()
81 .get(request, null);
82 if (reply != null) {
83 alarms = parseAlarms(reply);
84 }
85 } catch (NetconfException e) {
86 log.error("Error reading alarms for device {} exception {}", deviceId, e);
87 }
88
89 return ImmutableList.copyOf(alarms);
90 }
91
92 private List<Alarm> parseAlarms(String content) {
93 List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_SYSTEMALARMS);
94 List<Alarm> alarms = new ArrayList<>();
95 for (HierarchicalConfiguration alarm : subtrees) {
96 alarms.add(parseAlarm(alarm));
97 }
98 return alarms;
99 }
100
101 private Alarm parseAlarm(HierarchicalConfiguration cfg) {
102 boolean cleared = false;
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100103 String alarmType = cfg.getString(ALARM_TYPE);
104 String alarmMessage = cfg.getString(ALARM_MESSAGE);
105 SeverityLevel alarmLevel = SeverityLevel.INDETERMINATE;
Laszlo Papp23e0c512018-09-18 16:19:17 +0000106 if (alarmType.equals(ALARM_TYPE_LOS)) {
107 alarmLevel = SeverityLevel.MAJOR;
108 }
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100109 long timeRaised = getTimeRaised(cfg);
110 DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder(
Laszlo Papp52a35902018-09-19 10:13:45 +0000111 AlarmId.alarmId(deviceId, alarmMessage),
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100112 deviceId, alarmMessage, alarmLevel, timeRaised);
113 return alarmBuilder.build();
114 }
115
116 private long getTimeRaised(HierarchicalConfiguration cfg) {
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100117 String alarmTime = cfg.getString(ALARM_TIME);
Laszlo Pappf1a7b7f2018-09-19 07:38:00 +0000118 return XmlEventParser.getEventTime(alarmTime);
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100119 }
120}