blob: 5fbb444f28436fa73d71ca82dab15d68a43dde69 [file] [log] [blame]
Simon Huntd5b96732016-07-08 13:22:27 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntd5b96732016-07-08 13:22:27 -07003 *
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.
Simon Huntd5b96732016-07-08 13:22:27 -070043
Simon Hunta7aea842017-05-03 19:42:50 -070044 We do not need to follow that model necessarily. Instead, we have this
45 class and Topo2Jsonifier, which takes UiModel objects and renders them
46 as JSON objects.
Simon Huntd5b96732016-07-08 13:22:27 -070047
48 */
49
50/**
51 * Server-side component for interacting with the new "Region aware" topology
52 * view in the Web UI.
53 */
54public class Topo2ViewMessageHandler extends UiMessageHandler {
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
58 // === Inbound event identifiers
Simon Hunt98189192016-07-29 19:02:27 -070059 private static final String START = "topo2Start";
60 private static final String NAV_REGION = "topo2navRegion";
61 private static final String STOP = "topo2Stop";
Steven Burrowse7cc3082016-09-27 11:24:58 -070062 private static final String UPDATE_META2 = "updateMeta2";
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 Hunt22c35df2017-04-26 17:28:42 -070069 private static final String OVERLAYS = "topo2Overlays";
Simon Huntd5b96732016-07-08 13:22:27 -070070
Simon Hunt977aa052016-07-20 17:08:29 -070071
Simon Huntd5b96732016-07-08 13:22:27 -070072 private UiTopoSession topoSession;
73 private Topo2Jsonifier t2json;
Simon Hunt22c35df2017-04-26 17:28:42 -070074 private Topo2OverlayCache overlay2Cache;
Simon Hunt2d7cd6f2017-05-04 13:04:50 -070075 private Topo2TrafficMessageHandler trafficHandler;
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
Simon Hunt2d7cd6f2017-05-04 13:04:50 -070097 /**
98 * Sets a reference to the traffic message handler.
99 *
100 * @param traffic the traffic message handler instance
101 */
102 public void setTrafficHandler(Topo2TrafficMessageHandler traffic) {
103 trafficHandler = traffic;
104 }
105
Simon Hunt22c35df2017-04-26 17:28:42 -0700106
Simon Huntd5b96732016-07-08 13:22:27 -0700107 @Override
108 protected Collection<RequestHandler> createRequestHandlers() {
109 return ImmutableSet.of(
110 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -0700111 new Topo2NavRegion(),
Steven Burrowse7cc3082016-09-27 11:24:58 -0700112 new Topo2Stop(),
113 new Topo2UpdateMeta()
Simon Huntd5b96732016-07-08 13:22:27 -0700114 );
115 }
116
117 // ==================================================================
118
Simon Hunt377f5d22016-09-01 16:27:21 -0700119 private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) {
120 List<UiTopoLayout> crumbs = topoSession.breadCrumbs();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700121 return t2json.layout(currentLayout, crumbs);
Simon Hunt377f5d22016-09-01 16:27:21 -0700122 }
123
124 private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) {
125 UiRegion region = topoSession.getRegion(currentLayout);
126 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
127 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
128 return t2json.region(region, kids, links);
129 }
130
131 private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) {
132 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
133 ObjectNode peersPayload = objectNode();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700134 String rid = currentLayout.regionId().toString();
135 peersPayload.set("peers", t2json.closedNodes(rid, peers));
Simon Hunt377f5d22016-09-01 16:27:21 -0700136 return peersPayload;
137 }
138
Simon Hunt22c35df2017-04-26 17:28:42 -0700139 // ==================================================================
140
Simon Hunt377f5d22016-09-01 16:27:21 -0700141
Simon Huntd5b96732016-07-08 13:22:27 -0700142 private final class Topo2Start extends RequestHandler {
143 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -0700144 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700145 }
146
147 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800148 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700149 // client view is ready to receive data to display; so start up
150 // server-side processing, and send over initial state
151
152 log.debug("topo2Start: {}", payload);
153
Simon Huntb1ce2602016-07-23 14:04:31 -0700154 // this may be a little heavyweight, but it might be safer to do
155 // this than make assumptions about the order in which devices
156 // and regions are added... and thus internal linkages set up
157 // correctly
158 topoSession.refreshModel();
159
Simon Hunt377f5d22016-09-01 16:27:21 -0700160 // start with the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700161 List<UiClusterMember> instances = topoSession.getAllInstances();
162 sendMessage(ALL_INSTANCES, t2json.instances(instances));
163
Simon Hunt377f5d22016-09-01 16:27:21 -0700164
165 // Send layout, region, peers data...
166
Simon Hunt977aa052016-07-20 17:08:29 -0700167 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700168 UiTopoLayout currentLayout = topoSession.currentLayout();
Simon Hunt377f5d22016-09-01 16:27:21 -0700169 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700170
Simon Hunt977aa052016-07-20 17:08:29 -0700171 // this is the region that is associated with the current layout
172 // this message includes details of the sub-regions, devices,
173 // hosts, and links within the region
174 // (as well as layer-order hints)
Simon Hunt377f5d22016-09-01 16:27:21 -0700175 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700176
Simon Hunt98189192016-07-29 19:02:27 -0700177 // these are the regions/devices that are siblings to this region
Simon Hunt377f5d22016-09-01 16:27:21 -0700178 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700179 }
Simon Hunt977aa052016-07-20 17:08:29 -0700180 }
181
Simon Hunt98189192016-07-29 19:02:27 -0700182 private final class Topo2NavRegion extends RequestHandler {
183 private Topo2NavRegion() {
184 super(NAV_REGION);
185 }
186
187 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800188 public void process(ObjectNode payload) {
Simon Hunt98189192016-07-29 19:02:27 -0700189 String rid = string(payload, "rid");
Simon Hunt377f5d22016-09-01 16:27:21 -0700190 log.debug("topo2navRegion: rid={}", rid);
191
192 // NOTE: we are NOT re-issuing information about the cluster nodes
193
194 // switch to the selected region...
195 topoSession.navToRegion(rid);
196
197 // re-send layout, region, peers data...
198 UiTopoLayout currentLayout = topoSession.currentLayout();
199 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
200 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
201 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt98189192016-07-29 19:02:27 -0700202 }
203 }
204
Simon Huntd5b96732016-07-08 13:22:27 -0700205 private final class Topo2Stop extends RequestHandler {
206 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700207 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700208 }
209
210 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800211 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700212 // client view has gone away; so shut down server-side processing
Simon Huntd5b96732016-07-08 13:22:27 -0700213
214 log.debug("topo2Stop: {}", payload);
Simon Hunt2d7cd6f2017-05-04 13:04:50 -0700215 trafficHandler.ceaseAndDesist();
Simon Huntd5b96732016-07-08 13:22:27 -0700216
217 // OLD CODE DID THE FOLLOWING...
Simon Huntd5b96732016-07-08 13:22:27 -0700218// stopSummaryMonitoring();
Simon Huntd5b96732016-07-08 13:22:27 -0700219 }
220 }
221
Steven Burrowse7cc3082016-09-27 11:24:58 -0700222 private final class Topo2UpdateMeta extends RequestHandler {
223 private Topo2UpdateMeta() {
224 super(UPDATE_META2);
225 }
226
227 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800228 public void process(ObjectNode payload) {
Simon Hunt2521d5f2017-03-20 18:17:28 -0700229 // NOTE: metadata for a node is stored within the context of the
230 // current region.
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700231 String rid = topoSession.currentLayout().regionId().toString();
232 t2json.updateMeta(rid, payload);
Steven Burrowse7cc3082016-09-27 11:24:58 -0700233 }
234 }
235
Simon Huntd5b96732016-07-08 13:22:27 -0700236}