blob: 7795271b54994cc90f9d7b6d6c576b7cf2bd280d [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;
29import org.onosproject.ui.model.topo.UiTopoLayout;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Collection;
34import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070035import java.util.Set;
36
Simon Huntd5b96732016-07-08 13:22:27 -070037/*
38 NOTES:
39
40 The original topology view message handler was broken into two classes
41 TopologyViewMessageHandler, and TopologyViewMessageHandlerBase.
42 We do not need to follow that model necessarily. Starting with a
43 single class, and breaking it apart later if necessary.
44
45 Need to figure out the connection between this message handler and the
46 new way of doing things with UiTopoSession...
47
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";
Simon Huntd5b96732016-07-08 13:22:27 -070062
63 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070064 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070065 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
66 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070067 private static final String PEER_REGIONS = "topo2PeerRegions";
Simon Huntd5b96732016-07-08 13:22:27 -070068 private static final String TOPO_START_DONE = "topo2StartDone";
69
Simon Hunt977aa052016-07-20 17:08:29 -070070
Simon Huntd5b96732016-07-08 13:22:27 -070071 private UiTopoSession topoSession;
72 private Topo2Jsonifier t2json;
73
74
75 @Override
76 public void init(UiConnection connection, ServiceDirectory directory) {
77 super.init(connection, directory);
78
79 // get the topo session from the UiWebSocket
80 topoSession = ((UiWebSocket) connection).topoSession();
81 t2json = new Topo2Jsonifier(directory);
82 }
83
84 @Override
85 protected Collection<RequestHandler> createRequestHandlers() {
86 return ImmutableSet.of(
87 new Topo2Start(),
Simon Hunt98189192016-07-29 19:02:27 -070088 new Topo2NavRegion(),
Simon Huntd5b96732016-07-08 13:22:27 -070089 new Topo2Stop()
90 );
91 }
92
93 // ==================================================================
94
95
96 private final class Topo2Start extends RequestHandler {
97 private Topo2Start() {
Simon Hunt98189192016-07-29 19:02:27 -070098 super(START);
Simon Huntd5b96732016-07-08 13:22:27 -070099 }
100
101 @Override
102 public void process(long sid, ObjectNode payload) {
103 // client view is ready to receive data to display; so start up
104 // server-side processing, and send over initial state
105
106 log.debug("topo2Start: {}", payload);
107
Simon Huntb1ce2602016-07-23 14:04:31 -0700108 // this may be a little heavyweight, but it might be safer to do
109 // this than make assumptions about the order in which devices
110 // and regions are added... and thus internal linkages set up
111 // correctly
112 topoSession.refreshModel();
113
Simon Hunt977aa052016-07-20 17:08:29 -0700114 // this is the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700115 List<UiClusterMember> instances = topoSession.getAllInstances();
116 sendMessage(ALL_INSTANCES, t2json.instances(instances));
117
Simon Hunt977aa052016-07-20 17:08:29 -0700118 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700119 UiTopoLayout currentLayout = topoSession.currentLayout();
120 sendMessage(CURRENT_LAYOUT, t2json.layout(currentLayout));
121
Simon Hunt977aa052016-07-20 17:08:29 -0700122 // this is the region that is associated with the current layout
123 // this message includes details of the sub-regions, devices,
124 // hosts, and links within the region
125 // (as well as layer-order hints)
Simon Huntd5b96732016-07-08 13:22:27 -0700126 UiRegion region = topoSession.getRegion(currentLayout);
Simon Hunt977aa052016-07-20 17:08:29 -0700127 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
128 sendMessage(CURRENT_REGION, t2json.region(region, kids));
Simon Huntd5b96732016-07-08 13:22:27 -0700129
Simon Hunt98189192016-07-29 19:02:27 -0700130 // these are the regions/devices that are siblings to this region
131 Set<UiNode> peers = topoSession.getPeerNodes(currentLayout);
Simon Hunt977aa052016-07-20 17:08:29 -0700132 ObjectNode peersPayload = objectNode();
Simon Hunt98189192016-07-29 19:02:27 -0700133 peersPayload.set("peers", t2json.closedNodes(peers));
Simon Hunt977aa052016-07-20 17:08:29 -0700134 sendMessage(PEER_REGIONS, peersPayload);
Simon Huntd5b96732016-07-08 13:22:27 -0700135
Simon Huntb1ce2602016-07-23 14:04:31 -0700136 // finally, tell the UI that we are done : TODO review / delete??
Simon Huntd5b96732016-07-08 13:22:27 -0700137 sendMessage(TOPO_START_DONE, null);
138
139
140 // OLD CODE DID THE FOLLOWING...
141// addListeners();
142// sendAllInstances(null);
143// sendAllDevices();
144// sendAllLinks();
145// sendAllHosts();
146// sendTopoStartDone();
147 }
Simon Hunt977aa052016-07-20 17:08:29 -0700148
149
150 }
151
Simon Hunt98189192016-07-29 19:02:27 -0700152 private final class Topo2NavRegion extends RequestHandler {
153 private Topo2NavRegion() {
154 super(NAV_REGION);
155 }
156
157 @Override
158 public void process(long sid, ObjectNode payload) {
159 String dir = string(payload, "dir");
160 String rid = string(payload, "rid");
161 log.debug("NavRegion: dir={}, rid={}", dir, rid);
162 }
163 }
164
Simon Huntd5b96732016-07-08 13:22:27 -0700165 private final class Topo2Stop extends RequestHandler {
166 private Topo2Stop() {
Simon Hunt98189192016-07-29 19:02:27 -0700167 super(STOP);
Simon Huntd5b96732016-07-08 13:22:27 -0700168 }
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}