blob: b0ebbad00d96edf19fddd224adf709df1ac430ba [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
kmcpeakeb172d5f2015-12-10 11:30:43 +00003 *
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 */
16package org.onosproject.faultmanagement.alarms.gui;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.base.Strings;
20import com.google.common.collect.ImmutableSet;
Simon Hunt8a0429a2017-01-06 16:52:47 -080021import org.joda.time.DateTime;
22import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
23import org.onosproject.net.DeviceId;
kmcpeakeb172d5f2015-12-10 11:30:43 +000024import org.onosproject.ui.RequestHandler;
25import org.onosproject.ui.UiMessageHandler;
26import org.onosproject.ui.table.TableModel;
27import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt8a0429a2017-01-06 16:52:47 -080028import org.onosproject.ui.table.cell.TimeFormatter;
kmcpeakeb172d5f2015-12-10 11:30:43 +000029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Collection;
33import java.util.Set;
Simon Hunt8a0429a2017-01-06 16:52:47 -080034
35import static java.lang.Long.parseLong;
36import static org.onosproject.incubator.net.faultmanagement.alarm.AlarmId.alarmId;
kmcpeakeb172d5f2015-12-10 11:30:43 +000037
38/**
39 * Skeletal ONOS UI Table-View message handler.
40 */
41public class AlarmTableMessageHandler extends UiMessageHandler {
42
43 private static final String ALARM_TABLE_DATA_REQ = "alarmTableDataRequest";
44 private static final String ALARM_TABLE_DATA_RESP = "alarmTableDataResponse";
45 private static final String ALARM_TABLES = "alarmTables";
46
47 private static final String ALARM_TABLE_DETAIL_REQ = "alarmTableDetailsRequest";
48 private static final String ALARM_TABLE_DETAIL_RESP = "alarmTableDetailsResponse";
49 private static final String DETAILS = "details";
50
51 private static final String ID = "id";
52 private static final String DEVICE_ID_STR = "alarmDeviceId";
53 private static final String DESCRIPTION = "alarmDesc";
54 private static final String SOURCE = "alarmSource";
55 private static final String TIME_RAISED = "alarmTimeRaised";
56 private static final String TIME_UPDATED = "alarmTimeUpdated";
57 private static final String TIME_CLEARED = "alarmTimeCleared";
58 private static final String SEVERITY = "alarmSeverity";
59 private static final String RESULT = "result";
60
61 // TODO No need to show id column in ONOS-GUI
62
Simon Hunt8a0429a2017-01-06 16:52:47 -080063 // TODO Replace SEVERITY column by color-coding of row depending on severity
64 // e.g. red=critical, green=cleared etc
65
66 private static final String[] COLUMN_IDS = {
67 ID, DEVICE_ID_STR, DESCRIPTION, SOURCE, TIME_RAISED, SEVERITY
68 };
kmcpeakeb172d5f2015-12-10 11:30:43 +000069
70 private final Logger log = LoggerFactory.getLogger(getClass());
71
72 @Override
73 protected Collection<RequestHandler> createRequestHandlers() {
74 return ImmutableSet.of(
75 new AlarmTableDataRequestHandler(),
76 new AlarmTableDetailRequestHandler()
77 );
78 }
79
80 // handler for alarm table requests
81 private final class AlarmTableDataRequestHandler extends TableRequestHandler {
82
Jian Li69f66632016-01-15 12:27:42 -080083 private static final String NO_ROWS_MESSAGE = "No alarms found";
84
kmcpeakeb172d5f2015-12-10 11:30:43 +000085 private AlarmTableDataRequestHandler() {
86 super(ALARM_TABLE_DATA_REQ, ALARM_TABLE_DATA_RESP, ALARM_TABLES);
87 }
88
89 @Override
90 protected String defaultColumnId() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000091 return ID;
92 }
93
94 @Override
95 protected String[] getColumnIds() {
96 return COLUMN_IDS;
97 }
98
99 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800100 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800101 return NO_ROWS_MESSAGE;
102 }
103
104 @Override
kmcpeakeb172d5f2015-12-10 11:30:43 +0000105 protected TableModel createTableModel() {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000106 TableModel tm = super.createTableModel();
107 tm.setFormatter(TIME_RAISED, new TimeFormatter());
108 return tm;
109 }
110
111 @Override
112 protected void populateTable(TableModel tm, ObjectNode payload) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800113 log.debug(" populateTable: tm = {}; payload = {}", tm, payload);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000114 String devId = string(payload, "devId");
115
116 Set<Alarm> alarms = Strings.isNullOrEmpty(devId) ?
117 AlarmServiceUtil.lookUpAlarms() :
118 AlarmServiceUtil.lookUpAlarms(DeviceId.deviceId(devId));
119
Simon Hunt8a0429a2017-01-06 16:52:47 -0800120 alarms.forEach((alarm) -> populateRow(tm.addRow(), alarm));
kmcpeakeb172d5f2015-12-10 11:30:43 +0000121 }
122
123 private void populateRow(TableModel.Row row, Alarm alarm) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800124 log.debug("populateRow: row = {} alarm = {}", row, alarm);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000125
126 row.cell(ID, alarm.id().fingerprint())
127 .cell(DEVICE_ID_STR, alarm.deviceId())
128 .cell(DESCRIPTION, alarm.description())
129 .cell(SOURCE, alarm.source())
130 .cell(TIME_RAISED, new DateTime(alarm.timeRaised()))
131 .cell(SEVERITY, alarm.severity());
132 }
133 }
134
135 // handler for alarm details requests
136 private final class AlarmTableDetailRequestHandler extends RequestHandler {
137
138 private AlarmTableDetailRequestHandler() {
139 super(ALARM_TABLE_DETAIL_REQ);
140 }
141
142 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800143 public void process(ObjectNode payload) {
144 log.debug("payload = {}", payload);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000145
146 String id = string(payload, ID, "(none)");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800147 Alarm alarm = AlarmServiceUtil.lookupAlarm(alarmId(parseLong(id)));
kmcpeakeb172d5f2015-12-10 11:30:43 +0000148 ObjectNode rootNode = objectNode();
149 ObjectNode data = objectNode();
150 rootNode.set(DETAILS, data);
151
152 if (alarm == null) {
153 rootNode.put(RESULT, "Item with id '" + id + "' not found");
154 log.warn("attempted to get item detail for id '{}'", id);
155
156 } else {
157 rootNode.put(RESULT, "Found item with id '" + id + "'");
158
159 data.put(ID, alarm.id().fingerprint());
160 data.put(DESCRIPTION, alarm.description());
161 data.put(DEVICE_ID_STR, alarm.deviceId().toString());
162 data.put(SOURCE, alarm.source().toString());
163 long timeRaised = alarm.timeRaised();
164 data.put(TIME_RAISED,
165 formatTime(timeRaised)
166 );
167 data.put(TIME_UPDATED, formatTime(alarm.timeUpdated()));
168 data.put(TIME_CLEARED, formatTime(alarm.timeCleared()));
169 data.put(SEVERITY, alarm.severity().toString());
170 }
Simon Hunt8a0429a2017-01-06 16:52:47 -0800171 log.debug("send = {}", rootNode);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000172
Simon Hunt8a0429a2017-01-06 16:52:47 -0800173 sendMessage(ALARM_TABLE_DETAIL_RESP, rootNode);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000174 }
175 }
176
177 private static String formatTime(Long msSinceStartOfEpoch) {
178 if (msSinceStartOfEpoch == null) {
179 return "-";
180 }
181 return new TimeFormatter().format(new DateTime(msSinceStartOfEpoch));
182 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000183}