blob: e577f45261ddaecbd132dc0222bef08c3dc5cc87 [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.
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 Huntd5b96732016-07-08 13:22:27 -070075
76
77 @Override
78 public void init(UiConnection connection, ServiceDirectory directory) {
79 super.init(connection, directory);
80
81 // get the topo session from the UiWebSocket
82 topoSession = ((UiWebSocket) connection).topoSession();
Simon Hunt95f4b422017-03-03 13:49:05 -080083 t2json = new Topo2Jsonifier(directory, connection.userName());
Simon Huntd5b96732016-07-08 13:22:27 -070084 }
85
Simon Hunt22c35df2017-04-26 17:28:42 -070086 /**
87 * Sets a reference to the overlay cache for interacting with registered
88 * overlays.
89 *
90 * @param overlay2Cache the overlay cache
91 */
92 public void setOverlayCache(Topo2OverlayCache overlay2Cache) {
93 this.overlay2Cache = overlay2Cache;
94 }
95
96
Simon Huntd5b96732016-07-08 13:22:27 -070097 @Override
98 protected Collection<RequestHandler> createRequestHandlers() {
99 return ImmutableSet.of(
100 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -0700101 new Topo2NavRegion(),
Steven Burrowse7cc3082016-09-27 11:24:58 -0700102 new Topo2Stop(),
103 new Topo2UpdateMeta()
Simon Huntd5b96732016-07-08 13:22:27 -0700104 );
105 }
106
107 // ==================================================================
108
Simon Hunt377f5d22016-09-01 16:27:21 -0700109 private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) {
110 List<UiTopoLayout> crumbs = topoSession.breadCrumbs();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700111 return t2json.layout(currentLayout, crumbs);
Simon Hunt377f5d22016-09-01 16:27:21 -0700112 }
113
114 private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) {
115 UiRegion region = topoSession.getRegion(currentLayout);
116 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
117 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
118 return t2json.region(region, kids, links);
119 }
120
121 private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) {
122 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
123 ObjectNode peersPayload = objectNode();
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700124 String rid = currentLayout.regionId().toString();
125 peersPayload.set("peers", t2json.closedNodes(rid, peers));
Simon Hunt377f5d22016-09-01 16:27:21 -0700126 return peersPayload;
127 }
128
Simon Hunt22c35df2017-04-26 17:28:42 -0700129 // ==================================================================
130
Simon Hunt377f5d22016-09-01 16:27:21 -0700131
Simon Huntd5b96732016-07-08 13:22:27 -0700132 private final class Topo2Start extends RequestHandler {
133 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -0700134 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700135 }
136
137 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800138 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700139 // client view is ready to receive data to display; so start up
140 // server-side processing, and send over initial state
141
142 log.debug("topo2Start: {}", payload);
143
Simon Huntb1ce2602016-07-23 14:04:31 -0700144 // this may be a little heavyweight, but it might be safer to do
145 // this than make assumptions about the order in which devices
146 // and regions are added... and thus internal linkages set up
147 // correctly
148 topoSession.refreshModel();
149
Simon Hunt377f5d22016-09-01 16:27:21 -0700150 // start with the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700151 List<UiClusterMember> instances = topoSession.getAllInstances();
152 sendMessage(ALL_INSTANCES, t2json.instances(instances));
153
Simon Hunt377f5d22016-09-01 16:27:21 -0700154
155 // Send layout, region, peers data...
156
Simon Hunt977aa052016-07-20 17:08:29 -0700157 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700158 UiTopoLayout currentLayout = topoSession.currentLayout();
Simon Hunt377f5d22016-09-01 16:27:21 -0700159 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700160
Simon Hunt977aa052016-07-20 17:08:29 -0700161 // this is the region that is associated with the current layout
162 // this message includes details of the sub-regions, devices,
163 // hosts, and links within the region
164 // (as well as layer-order hints)
Simon Hunt377f5d22016-09-01 16:27:21 -0700165 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700166
Simon Hunt98189192016-07-29 19:02:27 -0700167 // these are the regions/devices that are siblings to this region
Simon Hunt377f5d22016-09-01 16:27:21 -0700168 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700169 }
Simon Hunt977aa052016-07-20 17:08:29 -0700170 }
171
Simon Hunt98189192016-07-29 19:02:27 -0700172 private final class Topo2NavRegion extends RequestHandler {
173 private Topo2NavRegion() {
174 super(NAV_REGION);
175 }
176
177 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800178 public void process(ObjectNode payload) {
Simon Hunt98189192016-07-29 19:02:27 -0700179 String rid = string(payload, "rid");
Simon Hunt377f5d22016-09-01 16:27:21 -0700180 log.debug("topo2navRegion: rid={}", rid);
181
182 // NOTE: we are NOT re-issuing information about the cluster nodes
183
184 // switch to the selected region...
185 topoSession.navToRegion(rid);
186
187 // re-send layout, region, peers data...
188 UiTopoLayout currentLayout = topoSession.currentLayout();
189 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
190 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
191 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt98189192016-07-29 19:02:27 -0700192 }
193 }
194
Simon Huntd5b96732016-07-08 13:22:27 -0700195 private final class Topo2Stop extends RequestHandler {
196 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700197 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700198 }
199
200 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800201 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700202 // client view has gone away; so shut down server-side processing
Simon Huntd5b96732016-07-08 13:22:27 -0700203
204 log.debug("topo2Stop: {}", payload);
Simon Hunta7aea842017-05-03 19:42:50 -0700205 // TODO: tell traffic monitor to stop monitoring...
206 // this requires this handler to know about traffic handler!!
Simon Huntd5b96732016-07-08 13:22:27 -0700207
208 // OLD CODE DID THE FOLLOWING...
Simon Huntd5b96732016-07-08 13:22:27 -0700209// stopSummaryMonitoring();
210// traffic.stopMonitoring();
211 }
212 }
213
Steven Burrowse7cc3082016-09-27 11:24:58 -0700214 private final class Topo2UpdateMeta extends RequestHandler {
215 private Topo2UpdateMeta() {
216 super(UPDATE_META2);
217 }
218
219 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800220 public void process(ObjectNode payload) {
Simon Hunt2521d5f2017-03-20 18:17:28 -0700221 // NOTE: metadata for a node is stored within the context of the
222 // current region.
Simon Huntf7e7d4a2017-03-24 15:22:20 -0700223 String rid = topoSession.currentLayout().regionId().toString();
224 t2json.updateMeta(rid, payload);
Steven Burrowse7cc3082016-09-27 11:24:58 -0700225 }
226 }
227
Simon Huntd5b96732016-07-08 13:22:27 -0700228}