Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -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"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 70 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 71 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 72 | private UiTopoSession topoSession; |
| 73 | private Topo2Jsonifier t2json; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 74 | private Topo2OverlayCache overlay2Cache; |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 75 | private Topo2TrafficMessageHandler trafficHandler; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 76 | |
| 77 | |
| 78 | @Override |
| 79 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 80 | super.init(connection, directory); |
| 81 | |
| 82 | // get the topo session from the UiWebSocket |
| 83 | topoSession = ((UiWebSocket) connection).topoSession(); |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 84 | t2json = new Topo2Jsonifier(directory, connection.userName()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Sets a reference to the overlay cache for interacting with registered |
| 89 | * overlays. |
| 90 | * |
| 91 | * @param overlay2Cache the overlay cache |
| 92 | */ |
| 93 | public void setOverlayCache(Topo2OverlayCache overlay2Cache) { |
| 94 | this.overlay2Cache = overlay2Cache; |
| 95 | } |
| 96 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Sets a reference to the traffic message handler. |
| 99 | * |
| 100 | * @param traffic the traffic message handler instance |
| 101 | */ |
| 102 | public void setTrafficHandler(Topo2TrafficMessageHandler traffic) { |
| 103 | trafficHandler = traffic; |
| 104 | } |
| 105 | |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 106 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 107 | @Override |
| 108 | protected Collection<RequestHandler> createRequestHandlers() { |
| 109 | return ImmutableSet.of( |
| 110 | new Topo2Start(), |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 111 | new Topo2NavRegion(), |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 112 | new Topo2Stop(), |
| 113 | new Topo2UpdateMeta() |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 114 | ); |
| 115 | } |
| 116 | |
| 117 | // ================================================================== |
| 118 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 119 | private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) { |
| 120 | List<UiTopoLayout> crumbs = topoSession.breadCrumbs(); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 121 | return t2json.layout(currentLayout, crumbs); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) { |
| 125 | UiRegion region = topoSession.getRegion(currentLayout); |
| 126 | Set<UiRegion> kids = topoSession.getSubRegions(currentLayout); |
| 127 | List<UiSynthLink> links = topoSession.getLinks(currentLayout); |
| 128 | return t2json.region(region, kids, links); |
| 129 | } |
| 130 | |
| 131 | private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) { |
| 132 | Set<UiNode> peers = topoSession.getPeerNodes(currentLayout); |
| 133 | ObjectNode peersPayload = objectNode(); |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 134 | String rid = currentLayout.regionId().toString(); |
| 135 | peersPayload.set("peers", t2json.closedNodes(rid, peers)); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 136 | return peersPayload; |
| 137 | } |
| 138 | |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 139 | // ================================================================== |
| 140 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 141 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 142 | private final class Topo2Start extends RequestHandler { |
| 143 | private Topo2Start() { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 144 | super(START); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 148 | public void process(ObjectNode payload) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 149 | // client view is ready to receive data to display; so start up |
| 150 | // server-side processing, and send over initial state |
| 151 | |
| 152 | log.debug("topo2Start: {}", payload); |
| 153 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 154 | // this may be a little heavyweight, but it might be safer to do |
| 155 | // this than make assumptions about the order in which devices |
| 156 | // and regions are added... and thus internal linkages set up |
| 157 | // correctly |
| 158 | topoSession.refreshModel(); |
| 159 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 160 | // start with the list of ONOS cluster members |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 161 | List<UiClusterMember> instances = topoSession.getAllInstances(); |
| 162 | sendMessage(ALL_INSTANCES, t2json.instances(instances)); |
| 163 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 164 | |
| 165 | // Send layout, region, peers data... |
| 166 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 167 | // this is the layout that the user has chosen to display |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 168 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 169 | sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 170 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 171 | // this is the region that is associated with the current layout |
| 172 | // this message includes details of the sub-regions, devices, |
| 173 | // hosts, and links within the region |
| 174 | // (as well as layer-order hints) |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 175 | sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 176 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 177 | // these are the regions/devices that are siblings to this region |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 178 | sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 179 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 182 | private final class Topo2NavRegion extends RequestHandler { |
| 183 | private Topo2NavRegion() { |
| 184 | super(NAV_REGION); |
| 185 | } |
| 186 | |
| 187 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 188 | public void process(ObjectNode payload) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 189 | String rid = string(payload, "rid"); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame] | 190 | log.debug("topo2navRegion: rid={}", rid); |
| 191 | |
| 192 | // NOTE: we are NOT re-issuing information about the cluster nodes |
| 193 | |
| 194 | // switch to the selected region... |
| 195 | topoSession.navToRegion(rid); |
| 196 | |
| 197 | // re-send layout, region, peers data... |
| 198 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
| 199 | sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout)); |
| 200 | sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout)); |
| 201 | sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout)); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 205 | private final class Topo2Stop extends RequestHandler { |
| 206 | private Topo2Stop() { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 207 | super(STOP); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 211 | public void process(ObjectNode payload) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 212 | // client view has gone away; so shut down server-side processing |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 213 | |
| 214 | log.debug("topo2Stop: {}", payload); |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 215 | trafficHandler.ceaseAndDesist(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 216 | |
| 217 | // OLD CODE DID THE FOLLOWING... |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 218 | // stopSummaryMonitoring(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 222 | private final class Topo2UpdateMeta extends RequestHandler { |
| 223 | private Topo2UpdateMeta() { |
| 224 | super(UPDATE_META2); |
| 225 | } |
| 226 | |
| 227 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 228 | public void process(ObjectNode payload) { |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 229 | // NOTE: metadata for a node is stored within the context of the |
| 230 | // current region. |
Simon Hunt | f7e7d4a | 2017-03-24 15:22:20 -0700 | [diff] [blame] | 231 | String rid = topoSession.currentLayout().regionId().toString(); |
| 232 | t2json.updateMeta(rid, payload); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 236 | } |