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