1 #set( $symbol_pound = '#' )
2 #set( $symbol_dollar = '$' )
3 #set( $symbol_escape = '\' )
5 * Copyright 2014,2015 Open Networking Laboratory
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import com.google.common.base.Strings;
23 import com.google.common.collect.ImmutableSet;
24 import org.onlab.osgi.ServiceDirectory;
25 import org.onosproject.net.Device;
26 import org.onosproject.net.DeviceId;
27 import org.onosproject.net.Element;
28 import org.onosproject.net.HostId;
29 import org.onosproject.net.Link;
30 import org.onosproject.net.device.DeviceService;
31 import org.onosproject.net.host.HostService;
32 import org.onosproject.net.link.LinkService;
33 import org.onosproject.ui.RequestHandler;
34 import org.onosproject.ui.UiConnection;
35 import org.onosproject.ui.UiMessageHandler;
36 import org.onosproject.ui.topo.Highlights;
37 import org.onosproject.ui.topo.TopoJson;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import java.util.Collection;
42 import java.util.HashSet;
44 import java.util.Timer;
45 import java.util.TimerTask;
48 * Skeletal ONOS UI Topology-Overlay message handler.
50 public class AppUiTopovMessageHandler extends UiMessageHandler {
52 private static final String SAMPLE_TOPOV_DISPLAY_START = "sampleTopovDisplayStart";
53 private static final String SAMPLE_TOPOV_DISPLAY_UPDATE = "sampleTopovDisplayUpdate";
54 private static final String SAMPLE_TOPOV_DISPLAY_STOP = "sampleTopovDisplayStop";
56 private static final String ID = "id";
57 private static final String MODE = "mode";
59 private static final long UPDATE_PERIOD_MS = 1000;
61 private static final Link[] EMPTY_LINK_SET = new Link[0];
63 private enum Mode { IDLE, MOUSE, LINK }
65 private final Logger log = LoggerFactory.getLogger(getClass());
67 private DeviceService deviceService;
68 private HostService hostService;
69 private LinkService linkService;
71 private final Timer timer = new Timer("sample-overlay");
72 private TimerTask demoTask = null;
73 private Mode currentMode = Mode.IDLE;
74 private Element elementOfNote;
75 private Link[] linkSet = EMPTY_LINK_SET;
76 private int linkIndex;
79 // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================
83 public void init(UiConnection connection, ServiceDirectory directory) {
84 super.init(connection, directory);
85 deviceService = directory.get(DeviceService.class);
86 hostService = directory.get(HostService.class);
87 linkService = directory.get(LinkService.class);
91 protected Collection<RequestHandler> createRequestHandlers() {
92 return ImmutableSet.of(
93 new DisplayStartHandler(),
94 new DisplayUpdateHandler(),
95 new DisplayStopHandler()
99 // === -------------------------
100 // === Handler classes
102 private final class DisplayStartHandler extends RequestHandler {
103 public DisplayStartHandler() {
104 super(SAMPLE_TOPOV_DISPLAY_START);
108 public void process(long sid, ObjectNode payload) {
109 String mode = string(payload, MODE);
111 log.debug("Start Display: mode [{}]", mode);
117 currentMode = Mode.MOUSE;
123 currentMode = Mode.LINK;
130 currentMode = Mode.IDLE;
137 private final class DisplayUpdateHandler extends RequestHandler {
138 public DisplayUpdateHandler() {
139 super(SAMPLE_TOPOV_DISPLAY_UPDATE);
143 public void process(long sid, ObjectNode payload) {
144 String id = string(payload, ID);
145 log.debug("Update Display: id [{}]", id);
146 if (!Strings.isNullOrEmpty(id)) {
154 private final class DisplayStopHandler extends RequestHandler {
155 public DisplayStopHandler() {
156 super(SAMPLE_TOPOV_DISPLAY_STOP);
160 public void process(long sid, ObjectNode payload) {
161 log.debug("Stop Display");
170 private void clearState() {
171 currentMode = Mode.IDLE;
172 elementOfNote = null;
173 linkSet = EMPTY_LINK_SET;
176 private void updateForMode(String id) {
177 log.debug("host service: {}", hostService);
178 log.debug("device service: {}", deviceService);
181 HostId hid = HostId.hostId(id);
182 log.debug("host id {}", hid);
183 elementOfNote = hostService.getHost(hid);
184 log.debug("host element {}", elementOfNote);
186 } catch (Exception e) {
188 DeviceId did = DeviceId.deviceId(id);
189 log.debug("device id {}", did);
190 elementOfNote = deviceService.getDevice(did);
191 log.debug("device element {}", elementOfNote);
193 } catch (Exception e2) {
194 log.debug("Unable to process ID [{}]", id);
195 elementOfNote = null;
199 switch (currentMode) {
214 private void clearForMode() {
215 sendHighlights(new Highlights());
218 private void sendHighlights(Highlights highlights) {
219 sendMessage(TopoJson.highlightsMessage(highlights));
223 private void sendMouseData() {
224 if (elementOfNote != null && elementOfNote instanceof Device) {
225 DeviceId devId = (DeviceId) elementOfNote.id();
226 Set<Link> links = linkService.getDeviceEgressLinks(devId);
227 sendHighlights(fromLinks(links, devId));
229 // Note: could also process Host, if available
232 private Highlights fromLinks(Set<Link> links, DeviceId devId) {
233 DemoLinkMap linkMap = new DemoLinkMap();
235 log.debug("Processing {} links", links.size());
236 links.forEach(linkMap::add);
238 log.debug("No egress links found for device {}", devId);
241 Highlights highlights = new Highlights();
243 for (DemoLink dlink : linkMap.biLinks()) {
244 dlink.makeImportant().setLabel("Yo!");
245 highlights.add(dlink.highlight(null));
250 private void initLinkSet() {
251 Set<Link> links = new HashSet<>();
252 for (Link link : linkService.getActiveLinks()) {
255 linkSet = links.toArray(new Link[links.size()]);
257 log.debug("initialized link set to {}", linkSet.length);
260 private void sendLinkData() {
261 DemoLinkMap linkMap = new DemoLinkMap();
262 for (Link link : linkSet) {
265 DemoLink dl = linkMap.add(linkSet[linkIndex]);
266 dl.makeImportant().setLabel(Integer.toString(linkIndex));
267 log.debug("sending link data (index {})", linkIndex);
270 if (linkIndex >= linkSet.length) {
274 Highlights highlights = new Highlights();
275 for (DemoLink dlink : linkMap.biLinks()) {
276 highlights.add(dlink.highlight(null));
279 sendHighlights(highlights);
282 private synchronized void scheduleTask() {
283 if (demoTask == null) {
284 log.debug("Starting up demo task...");
285 demoTask = new DisplayUpdateTask();
286 timer.schedule(demoTask, UPDATE_PERIOD_MS, UPDATE_PERIOD_MS);
288 log.debug("(demo task already running");
292 private synchronized void cancelTask() {
293 if (demoTask != null) {
300 private class DisplayUpdateTask extends TimerTask {
304 switch (currentMode) {
312 } catch (Exception e) {
313 log.warn("Unable to process demo task: {}", e.getMessage());
314 log.debug("Oops", e);