blob: 5449d9327ab2f266f2007beab7776e80d2e44c65 [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;
27import org.onosproject.ui.model.topo.UiRegion;
28import org.onosproject.ui.model.topo.UiTopoLayout;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Collection;
33import java.util.List;
Simon Hunt977aa052016-07-20 17:08:29 -070034import java.util.Set;
35
Simon Huntd5b96732016-07-08 13:22:27 -070036/*
37 NOTES:
38
39 The original topology view message handler was broken into two classes
40 TopologyViewMessageHandler, and TopologyViewMessageHandlerBase.
41 We do not need to follow that model necessarily. Starting with a
42 single class, and breaking it apart later if necessary.
43
44 Need to figure out the connection between this message handler and the
45 new way of doing things with UiTopoSession...
46
47 */
48
49/**
50 * Server-side component for interacting with the new "Region aware" topology
51 * view in the Web UI.
52 */
53public class Topo2ViewMessageHandler extends UiMessageHandler {
54
55 private final Logger log = LoggerFactory.getLogger(getClass());
56
57 // === Inbound event identifiers
58 private static final String TOPO2_START = "topo2Start";
59 private static final String TOPO2_STOP = "topo2Stop";
60
61 // === Outbound event identifiers
Simon Hunt977aa052016-07-20 17:08:29 -070062 private static final String ALL_INSTANCES = "topo2AllInstances";
Simon Huntd5b96732016-07-08 13:22:27 -070063 private static final String CURRENT_LAYOUT = "topo2CurrentLayout";
64 private static final String CURRENT_REGION = "topo2CurrentRegion";
Simon Hunt977aa052016-07-20 17:08:29 -070065 private static final String PEER_REGIONS = "topo2PeerRegions";
Simon Huntd5b96732016-07-08 13:22:27 -070066 private static final String TOPO_START_DONE = "topo2StartDone";
67
Simon Hunt977aa052016-07-20 17:08:29 -070068
Simon Huntd5b96732016-07-08 13:22:27 -070069 private UiTopoSession topoSession;
70 private Topo2Jsonifier t2json;
71
72
73 @Override
74 public void init(UiConnection connection, ServiceDirectory directory) {
75 super.init(connection, directory);
76
77 // get the topo session from the UiWebSocket
78 topoSession = ((UiWebSocket) connection).topoSession();
79 t2json = new Topo2Jsonifier(directory);
80 }
81
82 @Override
83 protected Collection<RequestHandler> createRequestHandlers() {
84 return ImmutableSet.of(
85 new Topo2Start(),
86 new Topo2Stop()
87 );
88 }
89
90 // ==================================================================
91
92
93 private final class Topo2Start extends RequestHandler {
94 private Topo2Start() {
95 super(TOPO2_START);
96 }
97
98 @Override
99 public void process(long sid, ObjectNode payload) {
100 // client view is ready to receive data to display; so start up
101 // server-side processing, and send over initial state
102
103 log.debug("topo2Start: {}", payload);
104
Simon Huntb1ce2602016-07-23 14:04:31 -0700105 // this may be a little heavyweight, but it might be safer to do
106 // this than make assumptions about the order in which devices
107 // and regions are added... and thus internal linkages set up
108 // correctly
109 topoSession.refreshModel();
110
Simon Hunt977aa052016-07-20 17:08:29 -0700111 // this is the list of ONOS cluster members
Simon Huntd5b96732016-07-08 13:22:27 -0700112 List<UiClusterMember> instances = topoSession.getAllInstances();
113 sendMessage(ALL_INSTANCES, t2json.instances(instances));
114
Simon Hunt977aa052016-07-20 17:08:29 -0700115 // this is the layout that the user has chosen to display
Simon Huntd5b96732016-07-08 13:22:27 -0700116 UiTopoLayout currentLayout = topoSession.currentLayout();
117 sendMessage(CURRENT_LAYOUT, t2json.layout(currentLayout));
118
Simon Hunt977aa052016-07-20 17:08:29 -0700119 // this is the region that is associated with the current layout
120 // this message includes details of the sub-regions, devices,
121 // hosts, and links within the region
122 // (as well as layer-order hints)
Simon Huntd5b96732016-07-08 13:22:27 -0700123 UiRegion region = topoSession.getRegion(currentLayout);
Simon Hunt977aa052016-07-20 17:08:29 -0700124 Set<UiRegion> kids = topoSession.getSubRegions(currentLayout);
125 sendMessage(CURRENT_REGION, t2json.region(region, kids));
Simon Huntd5b96732016-07-08 13:22:27 -0700126
Simon Hunt977aa052016-07-20 17:08:29 -0700127 // these are the regions that are siblings to this one
128 Set<UiRegion> peers = topoSession.getPeerRegions(currentLayout);
129 ObjectNode peersPayload = objectNode();
130 peersPayload.set("peers", t2json.closedRegions(peers));
131 sendMessage(PEER_REGIONS, peersPayload);
Simon Huntd5b96732016-07-08 13:22:27 -0700132
Simon Huntb1ce2602016-07-23 14:04:31 -0700133 // finally, tell the UI that we are done : TODO review / delete??
Simon Huntd5b96732016-07-08 13:22:27 -0700134 sendMessage(TOPO_START_DONE, null);
135
136
137 // OLD CODE DID THE FOLLOWING...
138// addListeners();
139// sendAllInstances(null);
140// sendAllDevices();
141// sendAllLinks();
142// sendAllHosts();
143// sendTopoStartDone();
144 }
Simon Hunt977aa052016-07-20 17:08:29 -0700145
146
147 }
148
Simon Huntd5b96732016-07-08 13:22:27 -0700149 private final class Topo2Stop extends RequestHandler {
150 private Topo2Stop() {
151 super(TOPO2_STOP);
152 }
153
154 @Override
155 public void process(long sid, ObjectNode payload) {
156 // client view has gone away; so shut down server-side processing
157 // TODO: implement...
158
159 log.debug("topo2Stop: {}", payload);
160
161 // OLD CODE DID THE FOLLOWING...
162// removeListeners();
163// stopSummaryMonitoring();
164// traffic.stopMonitoring();
165 }
166 }
167
168}