blob: df5318f7e6806925cf5592cc1c972259c6822f33 [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";
56 private static final String ALARM_MESSAGE = "alarm-message";
57
58 private DeviceId deviceId;
59
60 @Override
61 public List<Alarm> consumeAlarms() {
62 DriverHandler handler = handler();
63 NetconfController controller = handler.get(NetconfController.class);
64 checkNotNull(controller, "Netconf controller is null");
65
66 MastershipService mastershipService = handler.get(MastershipService.class);
67 deviceId = handler.data().deviceId();
68
69 List<Alarm> alarms = new ArrayList<>();
70 if (!mastershipService.isLocalMaster(deviceId)) {
71 log.warn("Not master for {} Use {} to execute command",
72 deviceId,
73 mastershipService.getMasterFor(deviceId));
74 return ImmutableList.copyOf(alarms);
75 }
76
77 try {
78 String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS);
79 String reply = controller.getDevicesMap()
80 .get(deviceId)
81 .getSession()
82 .get(request, null);
83 if (reply != null) {
84 alarms = parseAlarms(reply);
85 }
86 } catch (NetconfException e) {
87 log.error("Error reading alarms for device {} exception {}", deviceId, e);
88 }
89
90 return ImmutableList.copyOf(alarms);
91 }
92
93 private List<Alarm> parseAlarms(String content) {
94 List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_SYSTEMALARMS);
95 List<Alarm> alarms = new ArrayList<>();
96 for (HierarchicalConfiguration alarm : subtrees) {
97 alarms.add(parseAlarm(alarm));
98 }
99 return alarms;
100 }
101
102 private Alarm parseAlarm(HierarchicalConfiguration cfg) {
103 boolean cleared = false;
104 // TODO: Use the type for severity or in the description?
105 String alarmType = cfg.getString(ALARM_TYPE);
106 String alarmMessage = cfg.getString(ALARM_MESSAGE);
107 SeverityLevel alarmLevel = SeverityLevel.INDETERMINATE;
108 long timeRaised = getTimeRaised(cfg);
109 DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder(
110 AlarmId.alarmId(deviceId, Long.toString(timeRaised)),
111 deviceId, alarmMessage, alarmLevel, timeRaised);
112 return alarmBuilder.build();
113 }
114
115 private long getTimeRaised(HierarchicalConfiguration cfg) {
116 long timeRaised;
117
118 String alarmTime = cfg.getString(ALARM_TIME);
119 try {
120 OffsetDateTime date = OffsetDateTime.parse(alarmTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
121 timeRaised = date.toInstant().toEpochMilli();
122 return timeRaised;
123 } catch (DateTimeException e) {
124 log.error("Cannot parse exception {} {}", alarmTime, e);
125 }
126 return System.currentTimeMillis();
127 }
128}