blob: b30ff11b53e742ac3ab5b94529741f709e828286 [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";
Steven Burrowse7cc3082016-09-27 11:24:58 -070063 private static final String UPDATE_META2 = "updateMeta2";
Simon Huntd5b96732016-07-08 13:22:27 -070064
65 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070066 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070067 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
68 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070069 private static final String PEER_REGIONS = "topo2PeerRegions";
Simon Hunt22c35df2017-04-26 17:28:42 -070070 private static final String OVERLAYS = "topo2Overlays";
Simon Huntd5b96732016-07-08 13:22:27 -070071
Simon Hunt977aa052016-07-20 17:08:29 -070072
Simon Huntd5b96732016-07-08 13:22:27 -070073 private UiTopoSession topoSession;
74 private Topo2Jsonifier t2json;
Simon Hunt22c35df2017-04-26 17:28:42 -070075 private Topo2OverlayCache overlay2Cache;
Simon Huntd5b96732016-07-08 13:22:27 -070076
77
78 @Override
79 public void init(UiConnection connection, ServiceDirectory directory) {
80 super.init(connection, directory);
81
82 // get the topo session from the UiWebSocket
83 topoSession = ((UiWebSocket) connection).topoSession();
Simon Hunt95f4b422017-03-03 13:49:05 -080084 t2json = new Topo2Jsonifier(directory, connection.userName());
Simon Huntd5b96732016-07-08 13:22:27 -070085 }
86
Simon Hunt22c35df2017-04-26 17:28:42 -070087 /**
88 * Sets a reference to the overlay cache for interacting with registered
89 * overlays.
90 *
91 * @param overlay2Cache the overlay cache
92 */
93 public void setOverlayCache(Topo2OverlayCache overlay2Cache) {
94 this.overlay2Cache = overlay2Cache;
95 }
96
97
Simon Huntd5b96732016-07-08 13:22:27 -070098 @Override
99 protected Collection<RequestHandler> createRequestHandlers() {
100 return ImmutableSet.of(
101 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -0700102 new Topo2NavRegion(),
Steven Burrowse7cc3082016-09-27 11:24:58 -0700103 new Topo2Stop(),
104 new Topo2UpdateMeta()
Simon Huntd5b96732016-07-08 13:22:27 -0700105 );
106 }
107
108 // ==================================================================
109
Simon Hunt377f5d22016-09-01 16:27:21 -0700110 private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) {
111 List<UiTopoLayout> crumbs = topoSession.breadCrumbs();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700112 return t2json.layout(currentLayout, crumbs);
Simon Hunt377f5d22016-09-01 16:27:21 -0700113 }
114
115 private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) {
116 UiRegion region = topoSession.getRegion(currentLayout);
117 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
118 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
119 return t2json.region(region, kids, links);
120 }
121
122 private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) {
123 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
124 ObjectNode peersPayload = objectNode();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700125 String rid = currentLayout.regionId().toString();
126 peersPayload.set("peers", t2json.closedNodes(rid, peers));
Simon Hunt377f5d22016-09-01 16:27:21 -0700127 return peersPayload;
128 }
129
Simon Hunt22c35df2017-04-26 17:28:42 -0700130 // ==================================================================
131
Simon Hunt377f5d22016-09-01 16:27:21 -0700132
Simon Huntd5b96732016-07-08 13:22:27 -0700133 private final class Topo2Start extends RequestHandler {
134 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -0700135 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700136 }
137
138 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800139 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700140 // client view is ready to receive data to display; so start up
141 // server-side processing, and send over initial state
142
143 log.debug("topo2Start: {}", payload);
144
Simon Huntb1ce2602016-07-23 14:04:31 -0700145 // this may be a little heavyweight, but it might be safer to do
146 // this than make assumptions about the order in which devices
147 // and regions are added... and thus internal linkages set up
148 // correctly
149 topoSession.refreshModel();
150
Simon Hunt377f5d22016-09-01 16:27:21 -0700151 // start with the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700152 List<UiClusterMember> instances = topoSession.getAllInstances();
153 sendMessage(ALL_INSTANCES, t2json.instances(instances));
154
Simon Hunt377f5d22016-09-01 16:27:21 -0700155
156 // Send layout, region, peers data...
157
Simon Hunt977aa052016-07-20 17:08:29 -0700158 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700159 UiTopoLayout currentLayout = topoSession.currentLayout();
Simon Hunt377f5d22016-09-01 16:27:21 -0700160 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700161
Simon Hunt977aa052016-07-20 17:08:29 -0700162 // this is the region that is associated with the current layout
163 // this message includes details of the sub-regions, devices,
164 // hosts, and links within the region
165 // (as well as layer-order hints)
Simon Hunt377f5d22016-09-01 16:27:21 -0700166 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700167
Simon Hunt98189192016-07-29 19:02:27 -0700168 // these are the regions/devices that are siblings to this region
Simon Hunt377f5d22016-09-01 16:27:21 -0700169 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700170 }
Simon Hunt977aa052016-07-20 17:08:29 -0700171 }
172
Simon Hunt98189192016-07-29 19:02:27 -0700173 private final class Topo2NavRegion extends RequestHandler {
174 private Topo2NavRegion() {
175 super(NAV_REGION);
176 }
177
178 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800179 public void process(ObjectNode payload) {
Simon Hunt98189192016-07-29 19:02:27 -0700180 String rid = string(payload, "rid");
Simon Hunt377f5d22016-09-01 16:27:21 -0700181 log.debug("topo2navRegion: rid={}", rid);
182
183 // NOTE: we are NOT re-issuing information about the cluster nodes
184
185 // switch to the selected region...
186 topoSession.navToRegion(rid);
187
188 // re-send layout, region, peers data...
189 UiTopoLayout currentLayout = topoSession.currentLayout();
190 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
191 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
192 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt98189192016-07-29 19:02:27 -0700193 }
194 }
195
Simon Huntd5b96732016-07-08 13:22:27 -0700196 private final class Topo2Stop extends RequestHandler {
197 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700198 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700199 }
200
201 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800202 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700203 // client view has gone away; so shut down server-side processing
204 // TODO: implement...
205
206 log.debug("topo2Stop: {}", payload);
207
208 // OLD CODE DID THE FOLLOWING...
209// removeListeners();
210// stopSummaryMonitoring();
211// traffic.stopMonitoring();
212 }
213 }
214
Steven Burrowse7cc3082016-09-27 11:24:58 -0700215 private final class Topo2UpdateMeta extends RequestHandler {
216 private Topo2UpdateMeta() {
217 super(UPDATE_META2);
218 }
219
220 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800221 public void process(ObjectNode payload) {
Simon Hunt2521d5f2017-03-20 18:17:28 -0700222 // NOTE: metadata for a node is stored within the context of the
223 // current region.
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700224 String rid = topoSession.currentLayout().regionId().toString();
225 t2json.updateMeta(rid, payload);
Steven Burrowse7cc3082016-09-27 11:24:58 -0700226 }
227 }
228
Simon Huntd5b96732016-07-08 13:22:27 -0700229}