Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.ui.impl.topo; |
| 18 | |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiConnection; |
| 25 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 26 | import org.onosproject.ui.UiTopo2Overlay; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.impl.UiWebSocket; |
| 28 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.model.topo.UiNode; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.List; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 38 | import java.util.Set; |
| 39 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 40 | /* |
| 41 | NOTES: |
| 42 | |
| 43 | The original topology view message handler was broken into two classes |
| 44 | TopologyViewMessageHandler, and TopologyViewMessageHandlerBase. |
| 45 | We do not need to follow that model necessarily. Starting with a |
| 46 | single class, and breaking it apart later if necessary. |
| 47 | |
| 48 | Need to figure out the connection between this message handler and the |
| 49 | new way of doing things with UiTopoSession... |
| 50 | |
| 51 | */ |
| 52 | |
| 53 | /** |
| 54 | * Server-side component for interacting with the new "Region aware" topology |
| 55 | * view in the Web UI. |
| 56 | */ |
| 57 | public class Topo2ViewMessageHandler extends UiMessageHandler { |
| 58 | |
| 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
| 61 | // === Inbound event identifiers |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 62 | private static final String START = "topo2Start"; |
| 63 | private static final String NAV_REGION = "topo2navRegion"; |
| 64 | private static final String STOP = "topo2Stop"; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 65 | private static final String UPDATE_META2 = "updateMeta2"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 66 | |
| 67 | // === Outbound event identifiers |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 68 | private static final String ALL_INSTANCES = "topo2AllInstances"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 69 | private static final String CURRENT_LAYOUT = "topo2CurrentLayout"; |
| 70 | private static final String CURRENT_REGION = "topo2CurrentRegion"; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 71 | private static final String PEER_REGIONS = "topo2PeerRegions"; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 72 | private static final String OVERLAYS = "topo2Overlays"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 73 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 74 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 75 | private UiTopoSession topoSession; |
| 76 | private Topo2Jsonifier t2json; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 77 | private Topo2OverlayCache overlay2Cache; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 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(); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 86 | t2json = new Topo2Jsonifier(directory, connection.userName()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 89 | /** |
| 90 | * Sets a reference to the overlay cache for interacting with registered |
| 91 | * overlays. |
| 92 | * |
| 93 | * @param overlay2Cache the overlay cache |
| 94 | */ |
| 95 | public void setOverlayCache(Topo2OverlayCache overlay2Cache) { |
| 96 | this.overlay2Cache = overlay2Cache; |
| 97 | } |
| 98 | |
| 99 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 100 | @Override |
| 101 | protected Collection<RequestHandler> createRequestHandlers() { |
| 102 | return ImmutableSet.of( |
| 103 | new Topo2Start(), |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 104 | new Topo2NavRegion(), |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 105 | new Topo2Stop(), |
| 106 | new Topo2UpdateMeta() |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 107 | ); |
| 108 | } |
| 109 | |
| 110 | // ================================================================== |
| 111 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 112 | private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) { |
| 113 | List<UiTopoLayout> crumbs = topoSession.breadCrumbs(); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 114 | return t2json.layout(currentLayout, crumbs); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) { |
| 118 | UiRegion region = topoSession.getRegion(currentLayout); |
| 119 | Set<UiRegion> kids = topoSession.getSubRegions(currentLayout); |
| 120 | List<UiSynthLink> links = topoSession.getLinks(currentLayout); |
| 121 | return t2json.region(region, kids, links); |
| 122 | } |
| 123 | |
| 124 | private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) { |
| 125 | Set<UiNode> peers = topoSession.getPeerNodes(currentLayout); |
| 126 | ObjectNode peersPayload = objectNode(); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 127 | String rid = currentLayout.regionId().toString(); |
| 128 | peersPayload.set("peers", t2json.closedNodes(rid, peers)); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 129 | return peersPayload; |
| 130 | } |
| 131 | |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 132 | private ObjectNode mkOverlaysMessage() { |
| 133 | ArrayNode a = arrayNode(); |
| 134 | for (UiTopo2Overlay ov : overlay2Cache.list()) { |
| 135 | a.add(json(ov)); |
| 136 | } |
| 137 | ObjectNode payload = objectNode(); |
| 138 | payload.set("overlays", a); |
| 139 | return payload; |
| 140 | } |
| 141 | |
| 142 | private ObjectNode json(UiTopo2Overlay ov) { |
| 143 | return objectNode() |
| 144 | .put("id", ov.id()) |
| 145 | .put("name", ov.name()) |
| 146 | .put("gid", ov.glyphId()); |
| 147 | } |
| 148 | |
| 149 | // ================================================================== |
| 150 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 151 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 152 | private final class Topo2Start extends RequestHandler { |
| 153 | private Topo2Start() { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 154 | super(START); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 158 | public void process(ObjectNode payload) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 159 | // client view is ready to receive data to display; so start up |
| 160 | // server-side processing, and send over initial state |
| 161 | |
| 162 | log.debug("topo2Start: {}", payload); |
| 163 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 164 | // this may be a little heavyweight, but it might be safer to do |
| 165 | // this than make assumptions about the order in which devices |
| 166 | // and regions are added... and thus internal linkages set up |
| 167 | // correctly |
| 168 | topoSession.refreshModel(); |
| 169 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 170 | // start with the list of ONOS cluster members |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 171 | List<UiClusterMember> instances = topoSession.getAllInstances(); |
| 172 | sendMessage(ALL_INSTANCES, t2json.instances(instances)); |
| 173 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 174 | |
| 175 | // Send layout, region, peers data... |
| 176 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 177 | // this is the layout that the user has chosen to display |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 178 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 179 | sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 180 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 181 | // this is the region that is associated with the current layout |
| 182 | // this message includes details of the sub-regions, devices, |
| 183 | // hosts, and links within the region |
| 184 | // (as well as layer-order hints) |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 185 | sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 186 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 187 | // these are the regions/devices that are siblings to this region |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 188 | sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout)); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame^] | 189 | |
| 190 | // these are the registered overlays |
| 191 | sendMessage(OVERLAYS, mkOverlaysMessage()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 192 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 195 | private final class Topo2NavRegion extends RequestHandler { |
| 196 | private Topo2NavRegion() { |
| 197 | super(NAV_REGION); |
| 198 | } |
| 199 | |
| 200 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 201 | public void process(ObjectNode payload) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 202 | String rid = string(payload, "rid"); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 203 | log.debug("topo2navRegion: rid={}", rid); |
| 204 | |
| 205 | // NOTE: we are NOT re-issuing information about the cluster nodes |
| 206 | |
| 207 | // switch to the selected region... |
| 208 | topoSession.navToRegion(rid); |
| 209 | |
| 210 | // re-send layout, region, peers data... |
| 211 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
| 212 | sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout)); |
| 213 | sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout)); |
| 214 | sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout)); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 218 | private final class Topo2Stop extends RequestHandler { |
| 219 | private Topo2Stop() { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 220 | super(STOP); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 224 | public void process(ObjectNode payload) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 225 | // client view has gone away; so shut down server-side processing |
| 226 | // TODO: implement... |
| 227 | |
| 228 | log.debug("topo2Stop: {}", payload); |
| 229 | |
| 230 | // OLD CODE DID THE FOLLOWING... |
| 231 | // removeListeners(); |
| 232 | // stopSummaryMonitoring(); |
| 233 | // traffic.stopMonitoring(); |
| 234 | } |
| 235 | } |
| 236 | |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 237 | private final class Topo2UpdateMeta extends RequestHandler { |
| 238 | private Topo2UpdateMeta() { |
| 239 | super(UPDATE_META2); |
| 240 | } |
| 241 | |
| 242 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 243 | public void process(ObjectNode payload) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 244 | // NOTE: metadata for a node is stored within the context of the |
| 245 | // current region. |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 246 | String rid = topoSession.currentLayout().regionId().toString(); |
| 247 | t2json.updateMeta(rid, payload); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 251 | } |