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.onlab.osgi.ServiceDirectory; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Element; |
| 25 | import org.onosproject.net.HostId; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.host.HostService; |
| 28 | import org.onosproject.ui.RequestHandler; |
| 29 | import org.onosproject.ui.UiConnection; |
| 30 | import org.onosproject.ui.UiMessageHandler; |
| 31 | import org.onosproject.ui.topo.DeviceHighlight; |
| 32 | import org.onosproject.ui.topo.Highlights; |
| 33 | import org.onosproject.ui.topo.NodeBadge; |
| 34 | import org.onosproject.ui.topo.NodeBadge.Status; |
| 35 | import org.onosproject.ui.topo.TopoJson; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.Collection; |
| 40 | import java.util.Set; |
| 41 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 42 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; |
| 43 | |
| 44 | /** |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame^] | 45 | * FaultManagement UI Topology-Overlay message handler. |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 46 | */ |
| 47 | public class AlarmTopovMessageHandler extends UiMessageHandler { |
| 48 | |
| 49 | private static final String ALARM_TOPOV_DISPLAY_START = "alarmTopovDisplayStart"; |
| 50 | private static final String ALARM_TOPOV_DISPLAY_UPDATE = "alarmTopovDisplayUpdate"; |
| 51 | private static final String ALARM_TOPOV_DISPLAY_STOP = "alarmTopovDisplayStop"; |
| 52 | |
| 53 | private static final String ID = "id"; |
| 54 | private static final String MODE = "mode"; |
| 55 | |
| 56 | private enum Mode { |
| 57 | |
| 58 | IDLE, MOUSE |
| 59 | } |
| 60 | |
| 61 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 62 | |
| 63 | private DeviceService deviceService; |
| 64 | private HostService hostService; |
| 65 | private AlarmService alarmService; |
| 66 | |
| 67 | private Mode currentMode = Mode.IDLE; |
| 68 | private Element elementOfNote; |
| 69 | |
| 70 | // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================ |
| 71 | @Override |
| 72 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 73 | super.init(connection, directory); |
| 74 | deviceService = directory.get(DeviceService.class); |
| 75 | hostService = directory.get(HostService.class); |
| 76 | alarmService = directory.get(AlarmService.class); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected Collection<RequestHandler> createRequestHandlers() { |
| 81 | return ImmutableSet.of( |
| 82 | new DisplayStartHandler(), |
| 83 | new DisplayUpdateHandler(), |
| 84 | new DisplayStopHandler() |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | // === ------------------------- |
| 89 | // === Handler classes |
| 90 | private final class DisplayStartHandler extends RequestHandler { |
| 91 | |
| 92 | public DisplayStartHandler() { |
| 93 | super(ALARM_TOPOV_DISPLAY_START); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void process(long sid, ObjectNode payload) { |
| 98 | String mode = string(payload, MODE); |
| 99 | |
| 100 | log.debug("Start Display: mode [{}]", mode); |
| 101 | clearState(); |
| 102 | clearForMode(); |
| 103 | |
| 104 | switch (mode) { |
| 105 | case "mouse": |
| 106 | currentMode = Mode.MOUSE; |
| 107 | |
| 108 | sendMouseData(); |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | currentMode = Mode.IDLE; |
| 113 | |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private final class DisplayUpdateHandler extends RequestHandler { |
| 120 | |
| 121 | public DisplayUpdateHandler() { |
| 122 | super(ALARM_TOPOV_DISPLAY_UPDATE); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void process(long sid, ObjectNode payload) { |
| 127 | String id = string(payload, ID); |
| 128 | log.debug("Update Display: id [{}]", id); |
| 129 | if (!Strings.isNullOrEmpty(id)) { |
| 130 | updateForMode(id); |
| 131 | } else { |
| 132 | clearForMode(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private final class DisplayStopHandler extends RequestHandler { |
| 138 | |
| 139 | public DisplayStopHandler() { |
| 140 | super(ALARM_TOPOV_DISPLAY_STOP); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void process(long sid, ObjectNode payload) { |
| 145 | log.debug("Stop Display"); |
| 146 | clearState(); |
| 147 | clearForMode(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // === ------------ |
| 152 | private void clearState() { |
| 153 | currentMode = Mode.IDLE; |
| 154 | elementOfNote = null; |
| 155 | } |
| 156 | |
| 157 | private void updateForMode(String id) { |
| 158 | log.debug("host service: {}", hostService); |
| 159 | log.debug("device service: {}", deviceService); |
| 160 | |
| 161 | try { |
| 162 | HostId hid = HostId.hostId(id); |
| 163 | log.debug("host id {}", hid); |
| 164 | elementOfNote = hostService.getHost(hid); |
| 165 | log.debug("host element {}", elementOfNote); |
| 166 | |
| 167 | } catch (RuntimeException e) { |
| 168 | try { |
| 169 | DeviceId did = DeviceId.deviceId(id); |
| 170 | log.debug("device id {}", did); |
| 171 | elementOfNote = deviceService.getDevice(did); |
| 172 | log.debug("device element {}", elementOfNote); |
| 173 | |
| 174 | } catch (RuntimeException e2) { |
| 175 | log.debug("Unable to process ID [{}]", id); |
| 176 | elementOfNote = null; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | switch (currentMode) { |
| 181 | case MOUSE: |
| 182 | sendMouseData(); |
| 183 | break; |
| 184 | |
| 185 | default: |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | |
| 191 | private void clearForMode() { |
| 192 | sendHighlights(new Highlights()); |
| 193 | } |
| 194 | |
| 195 | private void sendHighlights(Highlights highlights) { |
| 196 | sendMessage(TopoJson.highlightsMessage(highlights)); |
| 197 | } |
| 198 | |
| 199 | private void sendMouseData() { |
| 200 | if (elementOfNote != null && elementOfNote instanceof Device) { |
| 201 | DeviceId devId = (DeviceId) elementOfNote.id(); |
| 202 | Set<Alarm> alarmsOnDevice = alarmService.getAlarms(devId); |
| 203 | Highlights highlights = new Highlights(); |
| 204 | |
| 205 | addDeviceBadge(highlights, devId, alarmsOnDevice.size()); |
| 206 | sendHighlights(highlights); |
| 207 | } |
| 208 | // Note: could also process Host, if available |
| 209 | } |
| 210 | |
| 211 | private void addDeviceBadge(Highlights h, DeviceId devId, int n) { |
| 212 | DeviceHighlight dh = new DeviceHighlight(devId.toString()); |
| 213 | dh.setBadge(createBadge(n)); |
| 214 | h.add(dh); |
| 215 | } |
| 216 | |
| 217 | private NodeBadge createBadge(int n) { |
| 218 | Status status = n > 0 ? Status.ERROR : Status.INFO; |
| 219 | String noun = n > 0 ? "(Alarmed)" : "(Normal)"; |
| 220 | String msg = "Alarms: " + n + " " + noun; |
| 221 | return NodeBadge.number(status, n, msg); |
| 222 | } |
| 223 | |
| 224 | } |