blob: e5946f946a47a86b4b5655823a08727082fafc4a [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;
Simon Hunt2521d5f2017-03-20 18:17:28 -070022import org.onosproject.net.region.Region;
Simon Huntd5b96732016-07-08 13:22:27 -070023import org.onosproject.ui.RequestHandler;
24import org.onosproject.ui.UiConnection;
25import org.onosproject.ui.UiMessageHandler;
26import org.onosproject.ui.impl.UiWebSocket;
27import org.onosproject.ui.model.topo.UiClusterMember;
Simon Hunt98189192016-07-29 19:02:27 -070028import org.onosproject.ui.model.topo.UiNode;
Simon Huntd5b96732016-07-08 13:22:27 -070029import org.onosproject.ui.model.topo.UiRegion;
Simon Huntc13082f2016-08-03 21:20:23 -070030import org.onosproject.ui.model.topo.UiSynthLink;
Simon Huntd5b96732016-07-08 13:22:27 -070031import org.onosproject.ui.model.topo.UiTopoLayout;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.Collection;
36import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070037import java.util.Set;
38
Simon Huntd5b96732016-07-08 13:22:27 -070039/*
40 NOTES:
41
42 The original topology view message handler was broken into two classes
43 TopologyViewMessageHandler, and TopologyViewMessageHandlerBase.
44 We do not need to follow that model necessarily. Starting with a
45 single class, and breaking it apart later if necessary.
46
47 Need to figure out the connection between this message handler and the
48 new way of doing things with UiTopoSession...
49
50 */
51
52/**
53 * Server-side component for interacting with the new "Region aware" topology
54 * view in the Web UI.
55 */
56public class Topo2ViewMessageHandler extends UiMessageHandler {
57
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
60 // === Inbound event identifiers
Simon Hunt98189192016-07-29 19:02:27 -070061 private static final String START = "topo2Start";
62 private static final String NAV_REGION = "topo2navRegion";
63 private static final String STOP = "topo2Stop";
Steven Burrowse7cc3082016-09-27 11:24:58 -070064 private static final String UPDATE_META2 = "updateMeta2";
Simon Huntd5b96732016-07-08 13:22:27 -070065
66 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070067 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070068 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
69 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070070 private static final String PEER_REGIONS = "topo2PeerRegions";
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;
75
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
86 @Override
87 protected Collection<RequestHandler> createRequestHandlers() {
88 return ImmutableSet.of(
89 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -070090 new Topo2NavRegion(),
Steven Burrowse7cc3082016-09-27 11:24:58 -070091 new Topo2Stop(),
92 new Topo2UpdateMeta()
Simon Huntd5b96732016-07-08 13:22:27 -070093 );
94 }
95
96 // ==================================================================
97
Simon Huntf0c6f542017-03-22 18:31:18 -070098 private String currentRegionId() {
99 Region current = topoSession.currentLayout().region();
100 return current == null ? "(root)" : current.id().toString();
Simon Hunt2521d5f2017-03-20 18:17:28 -0700101 }
102
Simon Hunt377f5d22016-09-01 16:27:21 -0700103 private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) {
104 List<UiTopoLayout> crumbs = topoSession.breadCrumbs();
Simon Huntf0c6f542017-03-22 18:31:18 -0700105 return t2json.layout(currentLayout, crumbs, currentRegionId());
Simon Hunt377f5d22016-09-01 16:27:21 -0700106 }
107
108 private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) {
109 UiRegion region = topoSession.getRegion(currentLayout);
110 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
111 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
112 return t2json.region(region, kids, links);
113 }
114
115 private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) {
116 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
117 ObjectNode peersPayload = objectNode();
Simon Huntf0c6f542017-03-22 18:31:18 -0700118 peersPayload.set("peers", t2json.closedNodes(currentRegionId(), peers));
Simon Hunt377f5d22016-09-01 16:27:21 -0700119 return peersPayload;
120 }
121
122
Simon Huntd5b96732016-07-08 13:22:27 -0700123 private final class Topo2Start extends RequestHandler {
124 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -0700125 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700126 }
127
128 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800129 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700130 // client view is ready to receive data to display; so start up
131 // server-side processing, and send over initial state
132
133 log.debug("topo2Start: {}", payload);
134
Simon Huntb1ce2602016-07-23 14:04:31 -0700135 // this may be a little heavyweight, but it might be safer to do
136 // this than make assumptions about the order in which devices
137 // and regions are added... and thus internal linkages set up
138 // correctly
139 topoSession.refreshModel();
140
Simon Hunt377f5d22016-09-01 16:27:21 -0700141 // start with the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700142 List<UiClusterMember> instances = topoSession.getAllInstances();
143 sendMessage(ALL_INSTANCES, t2json.instances(instances));
144
Simon Hunt377f5d22016-09-01 16:27:21 -0700145
146 // Send layout, region, peers data...
147
Simon Hunt977aa052016-07-20 17:08:29 -0700148 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700149 UiTopoLayout currentLayout = topoSession.currentLayout();
Simon Hunt377f5d22016-09-01 16:27:21 -0700150 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700151
Simon Hunt977aa052016-07-20 17:08:29 -0700152 // this is the region that is associated with the current layout
153 // this message includes details of the sub-regions, devices,
154 // hosts, and links within the region
155 // (as well as layer-order hints)
Simon Hunt377f5d22016-09-01 16:27:21 -0700156 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700157
Simon Hunt98189192016-07-29 19:02:27 -0700158 // these are the regions/devices that are siblings to this region
Simon Hunt377f5d22016-09-01 16:27:21 -0700159 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700160 }
Simon Hunt977aa052016-07-20 17:08:29 -0700161 }
162
Simon Hunt98189192016-07-29 19:02:27 -0700163 private final class Topo2NavRegion extends RequestHandler {
164 private Topo2NavRegion() {
165 super(NAV_REGION);
166 }
167
168 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800169 public void process(ObjectNode payload) {
Simon Hunt98189192016-07-29 19:02:27 -0700170 String rid = string(payload, "rid");
Simon Hunt377f5d22016-09-01 16:27:21 -0700171 log.debug("topo2navRegion: rid={}", rid);
172
173 // NOTE: we are NOT re-issuing information about the cluster nodes
174
175 // switch to the selected region...
176 topoSession.navToRegion(rid);
177
178 // re-send layout, region, peers data...
179 UiTopoLayout currentLayout = topoSession.currentLayout();
180 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
181 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
182 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt98189192016-07-29 19:02:27 -0700183 }
184 }
185
Simon Huntd5b96732016-07-08 13:22:27 -0700186 private final class Topo2Stop extends RequestHandler {
187 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700188 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700189 }
190
191 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800192 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700193 // client view has gone away; so shut down server-side processing
194 // TODO: implement...
195
196 log.debug("topo2Stop: {}", payload);
197
198 // OLD CODE DID THE FOLLOWING...
199// removeListeners();
200// stopSummaryMonitoring();
201// traffic.stopMonitoring();
202 }
203 }
204
Steven Burrowse7cc3082016-09-27 11:24:58 -0700205 private final class Topo2UpdateMeta extends RequestHandler {
206 private Topo2UpdateMeta() {
207 super(UPDATE_META2);
208 }
209
210 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800211 public void process(ObjectNode payload) {
Simon Hunt2521d5f2017-03-20 18:17:28 -0700212 // NOTE: metadata for a node is stored within the context of the
213 // current region.
Simon Huntf0c6f542017-03-22 18:31:18 -0700214 t2json.updateMeta(currentRegionId(), payload);
Steven Burrowse7cc3082016-09-27 11:24:58 -0700215 }
216 }
217
Simon Huntd5b96732016-07-08 13:22:27 -0700218}