blob: 1763048dbbca4b3e43b78f9ace3fd9fc314053bf [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
Simon Hunt22c35df2017-04-26 17:28:42 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
Simon Huntd5b96732016-07-08 13:22:27 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.osgi.ServiceDirectory;
23import org.onosproject.ui.RequestHandler;
24import org.onosproject.ui.UiConnection;
25import org.onosproject.ui.UiMessageHandler;
Simon Hunt22c35df2017-04-26 17:28:42 -070026import org.onosproject.ui.UiTopo2Overlay;
Simon Huntd5b96732016-07-08 13:22:27 -070027import org.onosproject.ui.impl.UiWebSocket;
28import org.onosproject.ui.model.topo.UiClusterMember;
Simon Hunt98189192016-07-29 19:02:27 -070029import org.onosproject.ui.model.topo.UiNode;
Simon Huntd5b96732016-07-08 13:22:27 -070030import org.onosproject.ui.model.topo.UiRegion;
Simon Huntc13082f2016-08-03 21:20:23 -070031import org.onosproject.ui.model.topo.UiSynthLink;
Simon Huntd5b96732016-07-08 13:22:27 -070032import org.onosproject.ui.model.topo.UiTopoLayout;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Collection;
37import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070038import java.util.Set;
39
Simon Huntd5b96732016-07-08 13:22:27 -070040/*
41 NOTES:
42
43 The original topology view message handler was broken into two classes
44 TopologyViewMessageHandler, and TopologyViewMessageHandlerBase.
45 We do not need to follow that model necessarily. Starting with a
46 single class, and breaking it apart later if necessary.
47
48 Need to figure out the connection between this message handler and the
49 new way of doing things with UiTopoSession...
50
51 */
52
53/**
54 * Server-side component for interacting with the new "Region aware" topology
55 * view in the Web UI.
56 */
57public class Topo2ViewMessageHandler extends UiMessageHandler {
58
59 private final Logger log = LoggerFactory.getLogger(getClass());
60
61 // === Inbound event identifiers
Simon Hunt98189192016-07-29 19:02:27 -070062 private static final String START = "topo2Start";
63 private static final String NAV_REGION = "topo2navRegion";
64 private static final String STOP = "topo2Stop";
Steven Burrowse7cc3082016-09-27 11:24:58 -070065 private static final String UPDATE_META2 = "updateMeta2";
Simon Huntd5b96732016-07-08 13:22:27 -070066
67 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070068 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070069 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
70 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070071 private static final String PEER_REGIONS = "topo2PeerRegions";
Simon Hunt22c35df2017-04-26 17:28:42 -070072 private static final String OVERLAYS = "topo2Overlays";
Simon Huntd5b96732016-07-08 13:22:27 -070073
Simon Hunt977aa052016-07-20 17:08:29 -070074
Simon Huntd5b96732016-07-08 13:22:27 -070075 private UiTopoSession topoSession;
76 private Topo2Jsonifier t2json;
Simon Hunt22c35df2017-04-26 17:28:42 -070077 private Topo2OverlayCache overlay2Cache;
Simon Huntd5b96732016-07-08 13:22:27 -070078
79
80 @Override
81 public void init(UiConnection connection, ServiceDirectory directory) {
82 super.init(connection, directory);
83
84 // get the topo session from the UiWebSocket
85 topoSession = ((UiWebSocket) connection).topoSession();
Simon Hunt95f4b422017-03-03 13:49:05 -080086 t2json = new Topo2Jsonifier(directory, connection.userName());
Simon Huntd5b96732016-07-08 13:22:27 -070087 }
88
Simon Hunt22c35df2017-04-26 17:28:42 -070089 /**
90 * Sets a reference to the overlay cache for interacting with registered
91 * overlays.
92 *
93 * @param overlay2Cache the overlay cache
94 */
95 public void setOverlayCache(Topo2OverlayCache overlay2Cache) {
96 this.overlay2Cache = overlay2Cache;
97 }
98
99
Simon Huntd5b96732016-07-08 13:22:27 -0700100 @Override
101 protected Collection<RequestHandler> createRequestHandlers() {
102 return ImmutableSet.of(
103 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -0700104 new Topo2NavRegion(),
Steven Burrowse7cc3082016-09-27 11:24:58 -0700105 new Topo2Stop(),
106 new Topo2UpdateMeta()
Simon Huntd5b96732016-07-08 13:22:27 -0700107 );
108 }
109
110 // ==================================================================
111
Simon Hunt377f5d22016-09-01 16:27:21 -0700112 private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) {
113 List<UiTopoLayout> crumbs = topoSession.breadCrumbs();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700114 return t2json.layout(currentLayout, crumbs);
Simon Hunt377f5d22016-09-01 16:27:21 -0700115 }
116
117 private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) {
118 UiRegion region = topoSession.getRegion(currentLayout);
119 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
120 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
121 return t2json.region(region, kids, links);
122 }
123
124 private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) {
125 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
126 ObjectNode peersPayload = objectNode();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700127 String rid = currentLayout.regionId().toString();
128 peersPayload.set("peers", t2json.closedNodes(rid, peers));
Simon Hunt377f5d22016-09-01 16:27:21 -0700129 return peersPayload;
130 }
131
Simon Hunt22c35df2017-04-26 17:28:42 -0700132 private ObjectNode mkOverlaysMessage() {
133 ArrayNode a = arrayNode();
134 for (UiTopo2Overlay ov : overlay2Cache.list()) {
135 a.add(json(ov));
136 }
137 ObjectNode payload = objectNode();
138 payload.set("overlays", a);
139 return payload;
140 }
141
142 private ObjectNode json(UiTopo2Overlay ov) {
143 return objectNode()
144 .put("id", ov.id())
145 .put("name", ov.name())
146 .put("gid", ov.glyphId());
147 }
148
149 // ==================================================================
150
Simon Hunt377f5d22016-09-01 16:27:21 -0700151
Simon Huntd5b96732016-07-08 13:22:27 -0700152 private final class Topo2Start extends RequestHandler {
153 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -0700154 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700155 }
156
157 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800158 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700159 // client view is ready to receive data to display; so start up
160 // server-side processing, and send over initial state
161
162 log.debug("topo2Start: {}", payload);
163
Simon Huntb1ce2602016-07-23 14:04:31 -0700164 // this may be a little heavyweight, but it might be safer to do
165 // this than make assumptions about the order in which devices
166 // and regions are added... and thus internal linkages set up
167 // correctly
168 topoSession.refreshModel();
169
Simon Hunt377f5d22016-09-01 16:27:21 -0700170 // start with the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700171 List<UiClusterMember> instances = topoSession.getAllInstances();
172 sendMessage(ALL_INSTANCES, t2json.instances(instances));
173
Simon Hunt377f5d22016-09-01 16:27:21 -0700174
175 // Send layout, region, peers data...
176
Simon Hunt977aa052016-07-20 17:08:29 -0700177 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700178 UiTopoLayout currentLayout = topoSession.currentLayout();
Simon Hunt377f5d22016-09-01 16:27:21 -0700179 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700180
Simon Hunt977aa052016-07-20 17:08:29 -0700181 // this is the region that is associated with the current layout
182 // this message includes details of the sub-regions, devices,
183 // hosts, and links within the region
184 // (as well as layer-order hints)
Simon Hunt377f5d22016-09-01 16:27:21 -0700185 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700186
Simon Hunt98189192016-07-29 19:02:27 -0700187 // these are the regions/devices that are siblings to this region
Simon Hunt377f5d22016-09-01 16:27:21 -0700188 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt22c35df2017-04-26 17:28:42 -0700189
190 // these are the registered overlays
191 sendMessage(OVERLAYS, mkOverlaysMessage());
Simon Huntd5b96732016-07-08 13:22:27 -0700192 }
Simon Hunt977aa052016-07-20 17:08:29 -0700193 }
194
Simon Hunt98189192016-07-29 19:02:27 -0700195 private final class Topo2NavRegion extends RequestHandler {
196 private Topo2NavRegion() {
197 super(NAV_REGION);
198 }
199
200 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800201 public void process(ObjectNode payload) {
Simon Hunt98189192016-07-29 19:02:27 -0700202 String rid = string(payload, "rid");
Simon Hunt377f5d22016-09-01 16:27:21 -0700203 log.debug("topo2navRegion: rid={}", rid);
204
205 // NOTE: we are NOT re-issuing information about the cluster nodes
206
207 // switch to the selected region...
208 topoSession.navToRegion(rid);
209
210 // re-send layout, region, peers data...
211 UiTopoLayout currentLayout = topoSession.currentLayout();
212 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
213 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
214 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt98189192016-07-29 19:02:27 -0700215 }
216 }
217
Simon Huntd5b96732016-07-08 13:22:27 -0700218 private final class Topo2Stop extends RequestHandler {
219 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700220 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700221 }
222
223 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800224 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700225 // client view has gone away; so shut down server-side processing
226 // TODO: implement...
227
228 log.debug("topo2Stop: {}", payload);
229
230 // OLD CODE DID THE FOLLOWING...
231// removeListeners();
232// stopSummaryMonitoring();
233// traffic.stopMonitoring();
234 }
235 }
236
Steven Burrowse7cc3082016-09-27 11:24:58 -0700237 private final class Topo2UpdateMeta extends RequestHandler {
238 private Topo2UpdateMeta() {
239 super(UPDATE_META2);
240 }
241
242 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800243 public void process(ObjectNode payload) {
Simon Hunt2521d5f2017-03-20 18:17:28 -0700244 // NOTE: metadata for a node is stored within the context of the
245 // current region.
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700246 String rid = topoSession.currentLayout().regionId().toString();
247 t2json.updateMeta(rid, payload);
Steven Burrowse7cc3082016-09-27 11:24:58 -0700248 }
249 }
250
Simon Huntd5b96732016-07-08 13:22:27 -0700251}