blob: ea774d970a5a1c6877afef2c6e1f4c2bbf8e0f4f [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 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;
74
75
76 @Override
77 public void init(UiConnection connection, ServiceDirectory directory) {
78 super.init(connection, directory);
79
80 // get the topo session from the UiWebSocket
81 topoSession = ((UiWebSocket) connection).topoSession();
82 t2json = new Topo2Jsonifier(directory);
83 }
84
85 @Override
86 protected Collection<RequestHandler> createRequestHandlers() {
87 return ImmutableSet.of(
88 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -070089 new Topo2NavRegion(),
Steven Burrowse7cc3082016-09-27 11:24:58 -070090 new Topo2Stop(),
91 new Topo2UpdateMeta()
Simon Huntd5b96732016-07-08 13:22:27 -070092 );
93 }
94
95 // ==================================================================
96
97
Simon Hunt377f5d22016-09-01 16:27:21 -070098 private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) {
99 List<UiTopoLayout> crumbs = topoSession.breadCrumbs();
100 return t2json.layout(currentLayout, crumbs);
101 }
102
103 private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) {
104 UiRegion region = topoSession.getRegion(currentLayout);
105 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
106 List<UiSynthLink> links = topoSession.getLinks(currentLayout);
107 return t2json.region(region, kids, links);
108 }
109
110 private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) {
111 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
112 ObjectNode peersPayload = objectNode();
113 peersPayload.set("peers", t2json.closedNodes(peers));
114 return peersPayload;
115 }
116
117
Simon Huntd5b96732016-07-08 13:22:27 -0700118 private final class Topo2Start extends RequestHandler {
119 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -0700120 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -0700121 }
122
123 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800124 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700125 // client view is ready to receive data to display; so start up
126 // server-side processing, and send over initial state
127
128 log.debug("topo2Start: {}", payload);
129
Simon Huntb1ce2602016-07-23 14:04:31 -0700130 // this may be a little heavyweight, but it might be safer to do
131 // this than make assumptions about the order in which devices
132 // and regions are added... and thus internal linkages set up
133 // correctly
134 topoSession.refreshModel();
135
Simon Hunt377f5d22016-09-01 16:27:21 -0700136 // start with the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700137 List<UiClusterMember> instances = topoSession.getAllInstances();
138 sendMessage(ALL_INSTANCES, t2json.instances(instances));
139
Simon Hunt377f5d22016-09-01 16:27:21 -0700140
141 // Send layout, region, peers data...
142
Simon Hunt977aa052016-07-20 17:08:29 -0700143 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700144 UiTopoLayout currentLayout = topoSession.currentLayout();
Simon Hunt377f5d22016-09-01 16:27:21 -0700145 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700146
Simon Hunt977aa052016-07-20 17:08:29 -0700147 // this is the region that is associated with the current layout
148 // this message includes details of the sub-regions, devices,
149 // hosts, and links within the region
150 // (as well as layer-order hints)
Simon Hunt377f5d22016-09-01 16:27:21 -0700151 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700152
Simon Hunt98189192016-07-29 19:02:27 -0700153 // these are the regions/devices that are siblings to this region
Simon Hunt377f5d22016-09-01 16:27:21 -0700154 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Huntd5b96732016-07-08 13:22:27 -0700155 }
Simon Hunt977aa052016-07-20 17:08:29 -0700156 }
157
Simon Hunt98189192016-07-29 19:02:27 -0700158 private final class Topo2NavRegion extends RequestHandler {
159 private Topo2NavRegion() {
160 super(NAV_REGION);
161 }
162
163 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800164 public void process(ObjectNode payload) {
Simon Hunt98189192016-07-29 19:02:27 -0700165 String rid = string(payload, "rid");
Simon Hunt377f5d22016-09-01 16:27:21 -0700166 log.debug("topo2navRegion: rid={}", rid);
167
168 // NOTE: we are NOT re-issuing information about the cluster nodes
169
170 // switch to the selected region...
171 topoSession.navToRegion(rid);
172
173 // re-send layout, region, peers data...
174 UiTopoLayout currentLayout = topoSession.currentLayout();
175 sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout));
176 sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout));
177 sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout));
Simon Hunt98189192016-07-29 19:02:27 -0700178 }
179 }
180
Simon Huntd5b96732016-07-08 13:22:27 -0700181 private final class Topo2Stop extends RequestHandler {
182 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700183 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700184 }
185
186 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800187 public void process(ObjectNode payload) {
Simon Huntd5b96732016-07-08 13:22:27 -0700188 // client view has gone away; so shut down server-side processing
189 // TODO: implement...
190
191 log.debug("topo2Stop: {}", payload);
192
193 // OLD CODE DID THE FOLLOWING...
194// removeListeners();
195// stopSummaryMonitoring();
196// traffic.stopMonitoring();
197 }
198 }
199
Steven Burrowse7cc3082016-09-27 11:24:58 -0700200 private final class Topo2UpdateMeta extends RequestHandler {
201 private Topo2UpdateMeta() {
202 super(UPDATE_META2);
203 }
204
205 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800206 public void process(ObjectNode payload) {
Steven Burrowse7cc3082016-09-27 11:24:58 -0700207 t2json.updateMeta(payload);
208 }
209 }
210
Simon Huntd5b96732016-07-08 13:22:27 -0700211}