blob: dfb15ff35f34276cd16d1ef0fe56e5261c00776f [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;
kmcpeakeb172d5f2015-12-10 11:30:43 +000019import com.google.common.collect.ImmutableSet;
20import org.onlab.osgi.ServiceDirectory;
Simon Hunt8a0429a2017-01-06 16:52:47 -080021import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
22import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
kmcpeakeb172d5f2015-12-10 11:30:43 +000023import org.onosproject.net.DeviceId;
kmcpeakeb172d5f2015-12-10 11:30:43 +000024import org.onosproject.net.device.DeviceService;
kmcpeakeb172d5f2015-12-10 11:30:43 +000025import org.onosproject.ui.RequestHandler;
26import org.onosproject.ui.UiConnection;
27import org.onosproject.ui.UiMessageHandler;
28import org.onosproject.ui.topo.DeviceHighlight;
29import org.onosproject.ui.topo.Highlights;
30import org.onosproject.ui.topo.NodeBadge;
31import org.onosproject.ui.topo.NodeBadge.Status;
32import org.onosproject.ui.topo.TopoJson;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Collection;
37import java.util.Set;
kmcpeakeb172d5f2015-12-10 11:30:43 +000038
39/**
Andrea Campanellae72ac552016-04-11 10:04:52 -070040 * FaultManagement UI Topology-Overlay message handler.
kmcpeakeb172d5f2015-12-10 11:30:43 +000041 */
42public class AlarmTopovMessageHandler extends UiMessageHandler {
43
44 private static final String ALARM_TOPOV_DISPLAY_START = "alarmTopovDisplayStart";
kmcpeakeb172d5f2015-12-10 11:30:43 +000045 private static final String ALARM_TOPOV_DISPLAY_STOP = "alarmTopovDisplayStop";
Andrea Campanella49f88382017-01-30 10:37:32 -080046 private static final int DELAY_MS = 500;
kmcpeakeb172d5f2015-12-10 11:30:43 +000047
kmcpeakeb172d5f2015-12-10 11:30:43 +000048
49 private final Logger log = LoggerFactory.getLogger(getClass());
50
Andrea Campanella49f88382017-01-30 10:37:32 -080051 protected AlarmService alarmService;
kmcpeakeb172d5f2015-12-10 11:30:43 +000052 private DeviceService deviceService;
Andrea Campanella49f88382017-01-30 10:37:32 -080053 private AlarmMonitor alarmMonitor;
kmcpeakeb172d5f2015-12-10 11:30:43 +000054
Simon Hunt8a0429a2017-01-06 16:52:47 -080055 // =======================================================================
kmcpeakeb172d5f2015-12-10 11:30:43 +000056 @Override
57 public void init(UiConnection connection, ServiceDirectory directory) {
58 super.init(connection, directory);
59 deviceService = directory.get(DeviceService.class);
kmcpeakeb172d5f2015-12-10 11:30:43 +000060 alarmService = directory.get(AlarmService.class);
Andrea Campanella49f88382017-01-30 10:37:32 -080061 alarmMonitor = new AlarmMonitor(this);
kmcpeakeb172d5f2015-12-10 11:30:43 +000062 }
63
64 @Override
65 protected Collection<RequestHandler> createRequestHandlers() {
66 return ImmutableSet.of(
67 new DisplayStartHandler(),
kmcpeakeb172d5f2015-12-10 11:30:43 +000068 new DisplayStopHandler()
69 );
70 }
71
72 // === -------------------------
73 // === Handler classes
74 private final class DisplayStartHandler extends RequestHandler {
75
76 public DisplayStartHandler() {
77 super(ALARM_TOPOV_DISPLAY_START);
78 }
79
80 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -080081 public void process(ObjectNode payload) {
Andrea Campanella49f88382017-01-30 10:37:32 -080082 log.debug("Start Display");
83 sendDelayedAlarmHighlights();
84 alarmMonitor.startMonitorig();
kmcpeakeb172d5f2015-12-10 11:30:43 +000085 }
86 }
87
88 private final class DisplayStopHandler extends RequestHandler {
89
90 public DisplayStopHandler() {
91 super(ALARM_TOPOV_DISPLAY_STOP);
92 }
93
94 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -080095 public void process(ObjectNode payload) {
kmcpeakeb172d5f2015-12-10 11:30:43 +000096 log.debug("Stop Display");
Andrea Campanella49f88382017-01-30 10:37:32 -080097 alarmMonitor.stopMonitoring();
98 clearHighlights();
kmcpeakeb172d5f2015-12-10 11:30:43 +000099 }
100 }
101
Andrea Campanella49f88382017-01-30 10:37:32 -0800102 /**
103 * Sends the highlights with a delay to the client side on the browser.
104 */
105 protected void sendDelayedAlarmHighlights() {
106 createAndSendHighlights(true);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000107 }
108
Andrea Campanella49f88382017-01-30 10:37:32 -0800109 /**
110 * Sends the highlights to the client side on the browser.
111 */
112 protected void sendAlarmHighlights() {
113 createAndSendHighlights(false);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000114 }
115
Andrea Campanella49f88382017-01-30 10:37:32 -0800116 private void createAndSendHighlights(boolean toDelay) {
117 Highlights highlights = new Highlights();
118 createBadges(highlights);
119 if (toDelay) {
120 highlights.delay(DELAY_MS);
121 }
122 sendHighlights(highlights);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000123 }
124
125 private void sendHighlights(Highlights highlights) {
126 sendMessage(TopoJson.highlightsMessage(highlights));
127 }
128
Andrea Campanella49f88382017-01-30 10:37:32 -0800129 private void createBadges(Highlights highlights) {
130 deviceService.getAvailableDevices().forEach(d -> {
131 Set<Alarm> alarmsOnDevice = alarmService.getAlarms(d.id());
132 int alarmSize = alarmsOnDevice.size();
133 log.debug("{} Alarms on device {}", alarmSize, d.id());
134 if (alarmSize > 0) {
135 addDeviceBadge(highlights, d.id(), alarmSize);
136 }
137 });
kmcpeakeb172d5f2015-12-10 11:30:43 +0000138 }
139
Andrea Campanella49f88382017-01-30 10:37:32 -0800140 private void clearHighlights() {
141 sendHighlights(new Highlights());
142 }
143
144
kmcpeakeb172d5f2015-12-10 11:30:43 +0000145 private void addDeviceBadge(Highlights h, DeviceId devId, int n) {
146 DeviceHighlight dh = new DeviceHighlight(devId.toString());
147 dh.setBadge(createBadge(n));
148 h.add(dh);
149 }
150
151 private NodeBadge createBadge(int n) {
152 Status status = n > 0 ? Status.ERROR : Status.INFO;
153 String noun = n > 0 ? "(Alarmed)" : "(Normal)";
154 String msg = "Alarms: " + n + " " + noun;
155 return NodeBadge.number(status, n, msg);
156 }
157
158}