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