kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 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 | package org.onosproject.faultmanagement.alarms.gui; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onosproject.ui.RequestHandler; |
| 22 | import org.onosproject.ui.UiMessageHandler; |
| 23 | import org.onosproject.ui.table.TableModel; |
| 24 | import org.onosproject.ui.table.TableRequestHandler; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.Set; |
| 30 | import org.joda.time.DateTime; |
| 31 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 32 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.ui.table.cell.TimeFormatter; |
| 35 | |
| 36 | /** |
| 37 | * Skeletal ONOS UI Table-View message handler. |
| 38 | */ |
| 39 | public class AlarmTableMessageHandler extends UiMessageHandler { |
| 40 | |
| 41 | private static final String ALARM_TABLE_DATA_REQ = "alarmTableDataRequest"; |
| 42 | private static final String ALARM_TABLE_DATA_RESP = "alarmTableDataResponse"; |
| 43 | private static final String ALARM_TABLES = "alarmTables"; |
| 44 | |
| 45 | private static final String ALARM_TABLE_DETAIL_REQ = "alarmTableDetailsRequest"; |
| 46 | private static final String ALARM_TABLE_DETAIL_RESP = "alarmTableDetailsResponse"; |
| 47 | private static final String DETAILS = "details"; |
| 48 | |
| 49 | private static final String ID = "id"; |
| 50 | private static final String DEVICE_ID_STR = "alarmDeviceId"; |
| 51 | private static final String DESCRIPTION = "alarmDesc"; |
| 52 | private static final String SOURCE = "alarmSource"; |
| 53 | private static final String TIME_RAISED = "alarmTimeRaised"; |
| 54 | private static final String TIME_UPDATED = "alarmTimeUpdated"; |
| 55 | private static final String TIME_CLEARED = "alarmTimeCleared"; |
| 56 | private static final String SEVERITY = "alarmSeverity"; |
| 57 | private static final String RESULT = "result"; |
| 58 | |
| 59 | // TODO No need to show id column in ONOS-GUI |
| 60 | |
| 61 | // TODO Replace SEVERITY column by color-coding of row depending on severity ie. red=critical, green=cleared etc |
| 62 | private static final String[] COLUMN_IDS = {ID, DEVICE_ID_STR, DESCRIPTION, SOURCE, TIME_RAISED, SEVERITY}; |
| 63 | |
| 64 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 65 | |
| 66 | @Override |
| 67 | protected Collection<RequestHandler> createRequestHandlers() { |
| 68 | return ImmutableSet.of( |
| 69 | new AlarmTableDataRequestHandler(), |
| 70 | new AlarmTableDetailRequestHandler() |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | // handler for alarm table requests |
| 75 | private final class AlarmTableDataRequestHandler extends TableRequestHandler { |
| 76 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 77 | private static final String NO_ROWS_MESSAGE = "No alarms found"; |
| 78 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 79 | private AlarmTableDataRequestHandler() { |
| 80 | super(ALARM_TABLE_DATA_REQ, ALARM_TABLE_DATA_RESP, ALARM_TABLES); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected String defaultColumnId() { |
| 85 | // if necessary, override defaultColumnId() -- if it isn't "id" |
| 86 | return ID; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected String[] getColumnIds() { |
| 91 | return COLUMN_IDS; |
| 92 | } |
| 93 | |
| 94 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 95 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 96 | return NO_ROWS_MESSAGE; |
| 97 | } |
| 98 | |
| 99 | @Override |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 100 | protected TableModel createTableModel() { |
| 101 | // if required, override createTableModel() to set column formatters / comparators |
| 102 | TableModel tm = super.createTableModel(); |
| 103 | tm.setFormatter(TIME_RAISED, new TimeFormatter()); |
| 104 | return tm; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 109 | log.debug(" populateTable tm={} payload ={}", tm, payload); |
| 110 | String devId = string(payload, "devId"); |
| 111 | |
| 112 | Set<Alarm> alarms = Strings.isNullOrEmpty(devId) ? |
| 113 | AlarmServiceUtil.lookUpAlarms() : |
| 114 | AlarmServiceUtil.lookUpAlarms(DeviceId.deviceId(devId)); |
| 115 | |
| 116 | alarms.stream().forEach((alarm) -> { |
| 117 | populateRow(tm.addRow(), alarm); |
| 118 | }); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | private void populateRow(TableModel.Row row, Alarm alarm) { |
| 123 | log.debug("populate table Row row={} item ={}", row, alarm); |
| 124 | |
| 125 | row.cell(ID, alarm.id().fingerprint()) |
| 126 | .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 |
| 142 | public void process(long sid, ObjectNode payload) { |
| 143 | log.debug("sid={}, payload ={}", sid, payload); |
| 144 | |
| 145 | String id = string(payload, ID, "(none)"); |
| 146 | Alarm alarm = AlarmServiceUtil.lookupAlarm(AlarmId.alarmId(Long.parseLong(id))); |
| 147 | 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 | |
| 158 | data.put(ID, alarm.id().fingerprint()); |
| 159 | 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, |
| 164 | formatTime(timeRaised) |
| 165 | ); |
| 166 | data.put(TIME_UPDATED, formatTime(alarm.timeUpdated())); |
| 167 | data.put(TIME_CLEARED, formatTime(alarm.timeCleared())); |
| 168 | data.put(SEVERITY, alarm.severity().toString()); |
| 169 | } |
| 170 | log.debug("send ={}", rootNode); |
| 171 | |
| 172 | sendMessage(ALARM_TABLE_DETAIL_RESP, 0, rootNode); |
| 173 | } |
| 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 | } |
| 182 | |
| 183 | |
| 184 | } |