blob: 2421bfe5b1166cfefc093e489014c52d4c8e93a9 [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;
21import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
22import org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer;
23import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
24import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.driver.DriverHandler;
28import org.onosproject.netconf.NetconfController;
29import org.onosproject.netconf.NetconfException;
30import org.onosproject.mastership.MastershipService;
31
32import org.slf4j.Logger;
33
34import java.util.ArrayList;
35import java.util.List;
36import java.time.format.DateTimeFormatter;
37import java.time.DateTimeException;
38import java.time.OffsetDateTime;
39
40import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel;
41import static com.google.common.base.Preconditions.checkNotNull;
42import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt;
43import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlEmpty;
44import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_SYSTEMALARMS;
45import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_SYSTEMALARMS_XMLNS;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Polatis specific implementation to provide a list of current alarms.
50 */
51public class PolatisAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer {
52 private final Logger log = getLogger(getClass());
53
54 private static final String ALARM_TIME = "alarm-time";
55 private static final String ALARM_TYPE = "alarm-type";
Laszlo Papp23e0c512018-09-18 16:19:17 +000056 private static final String ALARM_TYPE_LOS = "NOTIF_PORT_POWER";
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010057 private static final String ALARM_MESSAGE = "alarm-message";
58
59 private DeviceId deviceId;
60
61 @Override
62 public List<Alarm> consumeAlarms() {
63 DriverHandler handler = handler();
64 NetconfController controller = handler.get(NetconfController.class);
65 checkNotNull(controller, "Netconf controller is null");
66
67 MastershipService mastershipService = handler.get(MastershipService.class);
68 deviceId = handler.data().deviceId();
69
70 List<Alarm> alarms = new ArrayList<>();
71 if (!mastershipService.isLocalMaster(deviceId)) {
72 log.warn("Not master for {} Use {} to execute command",
73 deviceId,
74 mastershipService.getMasterFor(deviceId));
75 return ImmutableList.copyOf(alarms);
76 }
77
78 try {
79 String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS);
80 String reply = controller.getDevicesMap()
81 .get(deviceId)
82 .getSession()
83 .get(request, null);
84 if (reply != null) {
85 alarms = parseAlarms(reply);
86 }
87 } catch (NetconfException e) {
88 log.error("Error reading alarms for device {} exception {}", deviceId, e);
89 }
90
91 return ImmutableList.copyOf(alarms);
92 }
93
94 private List<Alarm> parseAlarms(String content) {
95 List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_SYSTEMALARMS);
96 List<Alarm> alarms = new ArrayList<>();
97 for (HierarchicalConfiguration alarm : subtrees) {
98 alarms.add(parseAlarm(alarm));
99 }
100 return alarms;
101 }
102
103 private Alarm parseAlarm(HierarchicalConfiguration cfg) {
104 boolean cleared = false;
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100105 String alarmType = cfg.getString(ALARM_TYPE);
106 String alarmMessage = cfg.getString(ALARM_MESSAGE);
107 SeverityLevel alarmLevel = SeverityLevel.INDETERMINATE;
Laszlo Papp23e0c512018-09-18 16:19:17 +0000108 if (alarmType.equals(ALARM_TYPE_LOS)) {
109 alarmLevel = SeverityLevel.MAJOR;
110 }
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +0100111 long timeRaised = getTimeRaised(cfg);
112 DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder(
113 AlarmId.alarmId(deviceId, Long.toString(timeRaised)),
114 deviceId, alarmMessage, alarmLevel, timeRaised);
115 return alarmBuilder.build();
116 }
117
118 private long getTimeRaised(HierarchicalConfiguration cfg) {
119 long timeRaised;
120
121 String alarmTime = cfg.getString(ALARM_TIME);
122 try {
123 OffsetDateTime date = OffsetDateTime.parse(alarmTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
124 timeRaised = date.toInstant().toEpochMilli();
125 return timeRaised;
126 } catch (DateTimeException e) {
127 log.error("Cannot parse exception {} {}", alarmTime, e);
128 }
129 return System.currentTimeMillis();
130 }
131}