blob: 86d54ccc9ee52d6f049305bad3a40a001973f4fd [file] [log] [blame]
Simon Huntd5b96732016-07-08 13:22:27 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.ui.impl.topo;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.onlab.osgi.ServiceDirectory;
22import org.onosproject.ui.RequestHandler;
23import org.onosproject.ui.UiConnection;
24import org.onosproject.ui.UiMessageHandler;
25import org.onosproject.ui.impl.UiWebSocket;
26import org.onosproject.ui.model.topo.UiClusterMember;
Simon Hunt98189192016-07-29 19:02:27 -070027import org.onosproject.ui.model.topo.UiNode;
Simon Huntd5b96732016-07-08 13:22:27 -070028import org.onosproject.ui.model.topo.UiRegion;
Simon Huntc13082f2016-08-03 21:20:23 -070029import org.onosproject.ui.model.topo.UiSynthLink;
Simon Huntd5b96732016-07-08 13:22:27 -070030import org.onosproject.ui.model.topo.UiTopoLayout;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Collection;
35import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070036import java.util.Set;
37
Simon Huntd5b96732016-07-08 13:22:27 -070038/*
39 NOTES:
40
41 The original topology view message handler was broken into two classes
42 TopologyViewMessageHandler, and TopologyViewMessageHandlerBase.
43 We do not need to follow that model necessarily. Starting with a
44 single class, and breaking it apart later if necessary.
45
46 Need to figure out the connection between this message handler and the
47 new way of doing things with UiTopoSession...
48
49 */
50
51/**
52 * Server-side component for interacting with the new "Region aware" topology
53 * view in the Web UI.
54 */
55public class Topo2ViewMessageHandler extends UiMessageHandler {
56
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
59 // === Inbound event identifiers
Simon Hunt98189192016-07-29 19:02:27 -070060 private static final String START = "topo2Start";
61 private static final String NAV_REGION = "topo2navRegion";
62 private static final String STOP = "topo2Stop";
Simon Huntd5b96732016-07-08 13:22:27 -070063
64 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070065 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070066 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
67 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070068 private static final String PEER_REGIONS = "topo2PeerRegions";
Simon Huntd5b96732016-07-08 13:22:27 -070069 private static final String TOPO_START_DONE = "topo2StartDone";
70
Simon Hunt977aa052016-07-20 17:08:29 -070071
Simon Huntd5b96732016-07-08 13:22:27 -070072 private UiTopoSession topoSession;
73 private Topo2Jsonifier t2json;
74
75
76 @Override
77 public void init(UiConnection connection, ServiceDirectory directory) {
78 super.init(connection, directory);
79
80 // get the topo session from the UiWebSocket
81 topoSession = ((UiWebSocket) connection).topoSession();
82 t2json = new Topo2Jsonifier(directory);
83 }
84
85 @Override
86 protected Collection<RequestHandler> createRequestHandlers() {
87 return ImmutableSet.of(
88 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -070089 new Topo2NavRegion(),
Simon Huntd5b96732016-07-08 13:22:27 -070090 new Topo2Stop()
91 );
92 }
93
94 // ==================================================================
95
96
97 private final class Topo2Start extends RequestHandler {
98 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -070099 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700100 }
101
102 @Override
103 public void process(long sid, ObjectNode payload) {
104 // client view is ready to receive data to display; so start up
105 // server-side processing, and send over initial state
106
107 log.debug("topo2Start: {}", payload);
108
Simon Huntb1ce2602016-07-23 14:04:31 -0700109 // this may be a little heavyweight, but it might be safer to do
110 // this than make assumptions about the order in which devices
111 // and regions are added... and thus internal linkages set up
112 // correctly
113 topoSession.refreshModel();
114
Simon Hunt977aa052016-07-20 17:08:29 -0700115 // this is the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700116 List<UiClusterMember> instances = topoSession.getAllInstances();
117 sendMessage(ALL_INSTANCES, t2json.instances(instances));
118
Simon Hunt977aa052016-07-20 17:08:29 -0700119 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700120 UiTopoLayout currentLayout = topoSession.currentLayout();
121 sendMessage(CURRENT_LAYOUT, t2json.layout(currentLayout));
122
Simon Hunt977aa052016-07-20 17:08:29 -0700123 // this is the region that is associated with the current layout
124 // this message includes details of the sub-regions, devices,
125 // hosts, and links within the region
126 // (as well as layer-order hints)
Simon Huntd5b96732016-07-08 13:22:27 -0700127 UiRegion region = topoSession.getRegion(currentLayout);
Simon Hunt977aa052016-07-20 17:08:29 -0700128 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
Simon Huntc13082f2016-08-03 21:20:23 -0700129 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
130 sendMessage(CURRENT_REGION, t2json.region(region, kids, links));
Simon Huntd5b96732016-07-08 13:22:27 -0700131
Simon Hunt98189192016-07-29 19:02:27 -0700132 // these are the regions/devices that are siblings to this region
133 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
Simon Hunt977aa052016-07-20 17:08:29 -0700134 ObjectNode peersPayload = objectNode();
Simon Hunt98189192016-07-29 19:02:27 -0700135 peersPayload.set("peers", t2json.closedNodes(peers));
Simon Hunt977aa052016-07-20 17:08:29 -0700136 sendMessage(PEER_REGIONS, peersPayload);
Simon Huntd5b96732016-07-08 13:22:27 -0700137
Simon Huntc13082f2016-08-03 21:20:23 -0700138 // TODO: send breadcrumb message
139
Simon Huntb1ce2602016-07-23 14:04:31 -0700140 // finally, tell the UI that we are done : TODO review / delete??
Simon Huntd5b96732016-07-08 13:22:27 -0700141 sendMessage(TOPO_START_DONE, null);
142
143
144 // OLD CODE DID THE FOLLOWING...
145// addListeners();
146// sendAllInstances(null);
147// sendAllDevices();
148// sendAllLinks();
149// sendAllHosts();
150// sendTopoStartDone();
151 }
Simon Hunt977aa052016-07-20 17:08:29 -0700152
153
154 }
155
Simon Hunt98189192016-07-29 19:02:27 -0700156 private final class Topo2NavRegion extends RequestHandler {
157 private Topo2NavRegion() {
158 super(NAV_REGION);
159 }
160
161 @Override
162 public void process(long sid, ObjectNode payload) {
163 String dir = string(payload, "dir");
164 String rid = string(payload, "rid");
165 log.debug("NavRegion: dir={}, rid={}", dir, rid);
166 }
167 }
168
Simon Huntd5b96732016-07-08 13:22:27 -0700169 private final class Topo2Stop extends RequestHandler {
170 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700171 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700172 }
173
174 @Override
175 public void process(long sid, ObjectNode payload) {
176 // client view has gone away; so shut down server-side processing
177 // TODO: implement...
178
179 log.debug("topo2Stop: {}", payload);
180
181 // OLD CODE DID THE FOLLOWING...
182// removeListeners();
183// stopSummaryMonitoring();
184// traffic.stopMonitoring();
185 }
186 }
187
188}