blob: 1a21907f71e0d67e86332a4f581b2326c0d4d43b [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Simon Hunt8a0429a2017-01-06 16:52:47 -080035import static org.onosproject.incubator.net.faultmanagement.alarm.AlarmId.alarmId;
kmcpeakeb172d5f2015-12-10 11:30:43 +000036
37/**
38 * Skeletal ONOS UI Table-View message handler.
39 */
40public class AlarmTableMessageHandler extends UiMessageHandler {
41
42 private static final String ALARM_TABLE_DATA_REQ = "alarmTableDataRequest";
43 private static final String ALARM_TABLE_DATA_RESP = "alarmTableDataResponse";
44 private static final String ALARM_TABLES = "alarmTables";
45
46 private static final String ALARM_TABLE_DETAIL_REQ = "alarmTableDetailsRequest";
47 private static final String ALARM_TABLE_DETAIL_RESP = "alarmTableDetailsResponse";
48 private static final String DETAILS = "details";
49
50 private static final String ID = "id";
51 private static final String DEVICE_ID_STR = "alarmDeviceId";
52 private static final String DESCRIPTION = "alarmDesc";
53 private static final String SOURCE = "alarmSource";
54 private static final String TIME_RAISED = "alarmTimeRaised";
55 private static final String TIME_UPDATED = "alarmTimeUpdated";
56 private static final String TIME_CLEARED = "alarmTimeCleared";
57 private static final String SEVERITY = "alarmSeverity";
58 private static final String RESULT = "result";
59
60 // TODO No need to show id column in ONOS-GUI
61
Simon Hunt8a0429a2017-01-06 16:52:47 -080062 // TODO Replace SEVERITY column by color-coding of row depending on severity
63 // e.g. red=critical, green=cleared etc
64
65 private static final String[] COLUMN_IDS = {
66 ID, DEVICE_ID_STR, DESCRIPTION, SOURCE, TIME_RAISED, SEVERITY
67 };
kmcpeakeb172d5f2015-12-10 11:30:43 +000068
69 private final Logger log = LoggerFactory.getLogger(getClass());
70
71 @Override
72 protected Collection<RequestHandler> createRequestHandlers() {
73 return ImmutableSet.of(
74 new AlarmTableDataRequestHandler(),
75 new AlarmTableDetailRequestHandler()
76 );
77 }
78
79 // handler for alarm table requests
80 private final class AlarmTableDataRequestHandler extends TableRequestHandler {
81
Jian Li69f66632016-01-15 12:27:42 -080082 private static final String NO_ROWS_MESSAGE = "No alarms found";
83
kmcpeakeb172d5f2015-12-10 11:30:43 +000084 private AlarmTableDataRequestHandler() {
85 super(ALARM_TABLE_DATA_REQ, ALARM_TABLE_DATA_RESP, ALARM_TABLES);
86 }
87
88 @Override
89 protected String defaultColumnId() {
kmcpeakeb172d5f2015-12-10 11:30:43 +000090 return ID;
91 }
92
93 @Override
94 protected String[] getColumnIds() {
95 return COLUMN_IDS;
96 }
97
98 @Override
Jian Li8baf4472016-01-15 15:08:09 -080099 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800100 return NO_ROWS_MESSAGE;
101 }
102
103 @Override
kmcpeakeb172d5f2015-12-10 11:30:43 +0000104 protected TableModel createTableModel() {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000105 TableModel tm = super.createTableModel();
106 tm.setFormatter(TIME_RAISED, new TimeFormatter());
107 return tm;
108 }
109
110 @Override
111 protected void populateTable(TableModel tm, ObjectNode payload) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800112 log.debug(" populateTable: tm = {}; payload = {}", tm, payload);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000113 String devId = string(payload, "devId");
114
115 Set<Alarm> alarms = Strings.isNullOrEmpty(devId) ?
116 AlarmServiceUtil.lookUpAlarms() :
117 AlarmServiceUtil.lookUpAlarms(DeviceId.deviceId(devId));
118
Simon Hunt8a0429a2017-01-06 16:52:47 -0800119 alarms.forEach((alarm) -> populateRow(tm.addRow(), alarm));
kmcpeakeb172d5f2015-12-10 11:30:43 +0000120 }
121
122 private void populateRow(TableModel.Row row, Alarm alarm) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800123 log.debug("populateRow: row = {} alarm = {}", row, alarm);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000124
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700125 row.cell(ID, alarm.id())
kmcpeakeb172d5f2015-12-10 11:30:43 +0000126 .cell(DEVICE_ID_STR, alarm.deviceId())
127 .cell(DESCRIPTION, alarm.description())
128 .cell(SOURCE, alarm.source())
129 .cell(TIME_RAISED, new DateTime(alarm.timeRaised()))
130 .cell(SEVERITY, alarm.severity());
131 }
132 }
133
134 // handler for alarm details requests
135 private final class AlarmTableDetailRequestHandler extends RequestHandler {
136
137 private AlarmTableDetailRequestHandler() {
138 super(ALARM_TABLE_DETAIL_REQ);
139 }
140
141 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800142 public void process(ObjectNode payload) {
143 log.debug("payload = {}", payload);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000144
145 String id = string(payload, ID, "(none)");
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700146 Alarm alarm = AlarmServiceUtil.lookupAlarm(alarmId(id));
kmcpeakeb172d5f2015-12-10 11:30:43 +0000147 ObjectNode rootNode = objectNode();
148 ObjectNode data = objectNode();
149 rootNode.set(DETAILS, data);
150
151 if (alarm == null) {
152 rootNode.put(RESULT, "Item with id '" + id + "' not found");
153 log.warn("attempted to get item detail for id '{}'", id);
154
155 } else {
156 rootNode.put(RESULT, "Found item with id '" + id + "'");
157
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700158 data.put(ID, alarm.id().toString());
kmcpeakeb172d5f2015-12-10 11:30:43 +0000159 data.put(DESCRIPTION, alarm.description());
160 data.put(DEVICE_ID_STR, alarm.deviceId().toString());
161 data.put(SOURCE, alarm.source().toString());
162 long timeRaised = alarm.timeRaised();
163 data.put(TIME_RAISED,
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700164 formatTime(timeRaised)
kmcpeakeb172d5f2015-12-10 11:30:43 +0000165 );
166 data.put(TIME_UPDATED, formatTime(alarm.timeUpdated()));
167 data.put(TIME_CLEARED, formatTime(alarm.timeCleared()));
168 data.put(SEVERITY, alarm.severity().toString());
169 }
Simon Hunt8a0429a2017-01-06 16:52:47 -0800170 log.debug("send = {}", rootNode);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000171
Simon Hunt8a0429a2017-01-06 16:52:47 -0800172 sendMessage(ALARM_TABLE_DETAIL_RESP, rootNode);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000173 }
174 }
175
176 private static String formatTime(Long msSinceStartOfEpoch) {
177 if (msSinceStartOfEpoch == null) {
178 return "-";
179 }
180 return new TimeFormatter().format(new DateTime(msSinceStartOfEpoch));
181 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000182}