blob: 7f44244cb8c7915dcd408bb97c46cceae9ffc050 [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;
Simon Hunt977aa052016-07-20 17:08:29 -070020import com.google.common.collect.ImmutableList;
Simon Huntd5b96732016-07-08 13:22:27 -070021import com.google.common.collect.ImmutableSet;
22import org.onlab.osgi.ServiceDirectory;
23import 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 Hunt977aa052016-07-20 17:08:29 -070028import org.onosproject.ui.model.topo.UiDevice;
29import org.onosproject.ui.model.topo.UiHost;
30import org.onosproject.ui.model.topo.UiLink;
Simon Huntd5b96732016-07-08 13:22:27 -070031import org.onosproject.ui.model.topo.UiRegion;
32import org.onosproject.ui.model.topo.UiTopoLayout;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Collection;
37import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070038import java.util.Set;
39
40import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT;
Simon Huntd5b96732016-07-08 13:22:27 -070041
42/*
43 NOTES:
44
45 The original topology view message handler was broken into two classes
46 TopologyViewMessageHandler, and TopologyViewMessageHandlerBase.
47 We do not need to follow that model necessarily. Starting with a
48 single class, and breaking it apart later if necessary.
49
50 Need to figure out the connection between this message handler and the
51 new way of doing things with UiTopoSession...
52
53 */
54
55/**
56 * Server-side component for interacting with the new "Region aware" topology
57 * view in the Web UI.
58 */
59public class Topo2ViewMessageHandler extends UiMessageHandler {
60
61 private final Logger log = LoggerFactory.getLogger(getClass());
62
63 // === Inbound event identifiers
64 private static final String TOPO2_START = "topo2Start";
65 private static final String TOPO2_STOP = "topo2Stop";
66
67 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070068 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070069 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
70 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070071 private static final String PEER_REGIONS = "topo2PeerRegions";
72 private static final String ORPHANS = "topo2Orphans";
Simon Huntd5b96732016-07-08 13:22:27 -070073 private static final String TOPO_START_DONE = "topo2StartDone";
74
Simon Hunt977aa052016-07-20 17:08:29 -070075
Simon Huntd5b96732016-07-08 13:22:27 -070076 private UiTopoSession topoSession;
77 private Topo2Jsonifier t2json;
78
79
80 @Override
81 public void init(UiConnection connection, ServiceDirectory directory) {
82 super.init(connection, directory);
83
84 // get the topo session from the UiWebSocket
85 topoSession = ((UiWebSocket) connection).topoSession();
86 t2json = new Topo2Jsonifier(directory);
87 }
88
89 @Override
90 protected Collection<RequestHandler> createRequestHandlers() {
91 return ImmutableSet.of(
92 new Topo2Start(),
93 new Topo2Stop()
94 );
95 }
96
97 // ==================================================================
98
99
100 private final class Topo2Start extends RequestHandler {
101 private Topo2Start() {
102 super(TOPO2_START);
103 }
104
105 @Override
106 public void process(long sid, ObjectNode payload) {
107 // client view is ready to receive data to display; so start up
108 // server-side processing, and send over initial state
109
110 log.debug("topo2Start: {}", payload);
111
Simon Hunt977aa052016-07-20 17:08:29 -0700112 // this is the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700113 List<UiClusterMember> instances = topoSession.getAllInstances();
114 sendMessage(ALL_INSTANCES, t2json.instances(instances));
115
Simon Hunt977aa052016-07-20 17:08:29 -0700116 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700117 UiTopoLayout currentLayout = topoSession.currentLayout();
118 sendMessage(CURRENT_LAYOUT, t2json.layout(currentLayout));
119
Simon Hunt977aa052016-07-20 17:08:29 -0700120 // this is the region that is associated with the current layout
121 // this message includes details of the sub-regions, devices,
122 // hosts, and links within the region
123 // (as well as layer-order hints)
Simon Huntd5b96732016-07-08 13:22:27 -0700124 UiRegion region = topoSession.getRegion(currentLayout);
Simon Hunt977aa052016-07-20 17:08:29 -0700125 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
126 sendMessage(CURRENT_REGION, t2json.region(region, kids));
Simon Huntd5b96732016-07-08 13:22:27 -0700127
Simon Hunt977aa052016-07-20 17:08:29 -0700128 // these are the regions that are siblings to this one
129 Set<UiRegion> peers = topoSession.getPeerRegions(currentLayout);
130 ObjectNode peersPayload = objectNode();
131 peersPayload.set("peers", t2json.closedRegions(peers));
132 sendMessage(PEER_REGIONS, peersPayload);
Simon Huntd5b96732016-07-08 13:22:27 -0700133
Simon Hunt977aa052016-07-20 17:08:29 -0700134 // return devices, hosts, links belonging to no region
135 Set<UiDevice> oDevices = topoSession.getOrphanDevices();
136 Set<UiHost> oHosts = topoSession.getOrphanHosts();
137 Set<UiLink> oLinks = topoSession.getOrphanLinks();
138 List<String> oLayers = getOrphanLayerOrder();
139 sendMessage(ORPHANS, t2json.orphans(oDevices, oHosts, oLinks, oLayers));
140
141 // finally, tell the UI that we are done
Simon Huntd5b96732016-07-08 13:22:27 -0700142 sendMessage(TOPO_START_DONE, null);
143
144
145 // OLD CODE DID THE FOLLOWING...
146// addListeners();
147// sendAllInstances(null);
148// sendAllDevices();
149// sendAllLinks();
150// sendAllHosts();
151// sendTopoStartDone();
152 }
Simon Hunt977aa052016-07-20 17:08:29 -0700153
154
155 }
156
157 // TODO: we need to decide on how this should really get populated.
158 // For example, to be "backward compatible", this should really be
159 // [ LAYER_OPTICAL, LAYER_PACKET, LAYER_DEFAULT ]
160 private List<String> getOrphanLayerOrder() {
161 // NOTE that LAYER_DEFAULT must always be last in the array
162 return ImmutableList.of(LAYER_DEFAULT);
Simon Huntd5b96732016-07-08 13:22:27 -0700163 }
164
165 private final class Topo2Stop extends RequestHandler {
166 private Topo2Stop() {
167 super(TOPO2_STOP);
168 }
169
170 @Override
171 public void process(long sid, ObjectNode payload) {
172 // client view has gone away; so shut down server-side processing
173 // TODO: implement...
174
175 log.debug("topo2Stop: {}", payload);
176
177 // OLD CODE DID THE FOLLOWING...
178// removeListeners();
179// stopSummaryMonitoring();
180// traffic.stopMonitoring();
181 }
182 }
183
184}