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; |
| 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. |
| 43 | We do not need to follow that model necessarily. Starting with a |
| 44 | single class, and breaking it apart later if necessary. |
| 45 | |
| 46 | Need to figure out the connection between this message handler and the |
| 47 | new way of doing things with UiTopoSession... |
| 48 | |
| 49 | */ |
| 50 | |
| 51 | /** |
| 52 | * Server-side component for interacting with the new "Region aware" topology |
| 53 | * view in the Web UI. |
| 54 | */ |
| 55 | public class Topo2ViewMessageHandler extends UiMessageHandler { |
| 56 | |
| 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
| 59 | // === Inbound event identifiers |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 60 | private static final String START = "topo2Start"; |
| 61 | private static final String NAV_REGION = "topo2navRegion"; |
| 62 | private static final String STOP = "topo2Stop"; |
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 | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 69 | private static final String TOPO_START_DONE = "topo2StartDone"; |
| 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; |
| 74 | |
| 75 | |
| 76 | @Override |
| 77 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 78 | super.init(connection, directory); |
| 79 | |
| 80 | // get the topo session from the UiWebSocket |
| 81 | topoSession = ((UiWebSocket) connection).topoSession(); |
| 82 | t2json = new Topo2Jsonifier(directory); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected Collection<RequestHandler> createRequestHandlers() { |
| 87 | return ImmutableSet.of( |
| 88 | new Topo2Start(), |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 89 | new Topo2NavRegion(), |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 90 | new Topo2Stop() |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | // ================================================================== |
| 95 | |
| 96 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 97 | private ObjectNode mkLayoutMessage(UiTopoLayout currentLayout) { |
| 98 | List<UiTopoLayout> crumbs = topoSession.breadCrumbs(); |
| 99 | return t2json.layout(currentLayout, crumbs); |
| 100 | } |
| 101 | |
| 102 | private ObjectNode mkRegionMessage(UiTopoLayout currentLayout) { |
| 103 | UiRegion region = topoSession.getRegion(currentLayout); |
| 104 | Set<UiRegion> kids = topoSession.getSubRegions(currentLayout); |
| 105 | List<UiSynthLink> links = topoSession.getLinks(currentLayout); |
| 106 | return t2json.region(region, kids, links); |
| 107 | } |
| 108 | |
| 109 | private ObjectNode mkPeersMessage(UiTopoLayout currentLayout) { |
| 110 | Set<UiNode> peers = topoSession.getPeerNodes(currentLayout); |
| 111 | ObjectNode peersPayload = objectNode(); |
| 112 | peersPayload.set("peers", t2json.closedNodes(peers)); |
| 113 | return peersPayload; |
| 114 | } |
| 115 | |
| 116 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 117 | private final class Topo2Start extends RequestHandler { |
| 118 | private Topo2Start() { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 119 | super(START); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void process(long sid, ObjectNode payload) { |
| 124 | // client view is ready to receive data to display; so start up |
| 125 | // server-side processing, and send over initial state |
| 126 | |
| 127 | log.debug("topo2Start: {}", payload); |
| 128 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 129 | // this may be a little heavyweight, but it might be safer to do |
| 130 | // this than make assumptions about the order in which devices |
| 131 | // and regions are added... and thus internal linkages set up |
| 132 | // correctly |
| 133 | topoSession.refreshModel(); |
| 134 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 135 | // start with the list of ONOS cluster members |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 136 | List<UiClusterMember> instances = topoSession.getAllInstances(); |
| 137 | sendMessage(ALL_INSTANCES, t2json.instances(instances)); |
| 138 | |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 139 | |
| 140 | // Send layout, region, peers data... |
| 141 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 142 | // this is the layout that the user has chosen to display |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 143 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 144 | sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 145 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 146 | // this is the region that is associated with the current layout |
| 147 | // this message includes details of the sub-regions, devices, |
| 148 | // hosts, and links within the region |
| 149 | // (as well as layer-order hints) |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 150 | sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 151 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 152 | // these are the regions/devices that are siblings to this region |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 153 | sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 154 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 157 | private final class Topo2NavRegion extends RequestHandler { |
| 158 | private Topo2NavRegion() { |
| 159 | super(NAV_REGION); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 164 | String rid = string(payload, "rid"); |
Simon Hunt | 377f5d2 | 2016-09-01 16:27:21 -0700 | [diff] [blame^] | 165 | log.debug("topo2navRegion: rid={}", rid); |
| 166 | |
| 167 | // NOTE: we are NOT re-issuing information about the cluster nodes |
| 168 | |
| 169 | // switch to the selected region... |
| 170 | topoSession.navToRegion(rid); |
| 171 | |
| 172 | // re-send layout, region, peers data... |
| 173 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
| 174 | sendMessage(CURRENT_LAYOUT, mkLayoutMessage(currentLayout)); |
| 175 | sendMessage(CURRENT_REGION, mkRegionMessage(currentLayout)); |
| 176 | sendMessage(PEER_REGIONS, mkPeersMessage(currentLayout)); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 180 | private final class Topo2Stop extends RequestHandler { |
| 181 | private Topo2Stop() { |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 182 | super(STOP); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void process(long sid, ObjectNode payload) { |
| 187 | // client view has gone away; so shut down server-side processing |
| 188 | // TODO: implement... |
| 189 | |
| 190 | log.debug("topo2Stop: {}", payload); |
| 191 | |
| 192 | // OLD CODE DID THE FOLLOWING... |
| 193 | // removeListeners(); |
| 194 | // stopSummaryMonitoring(); |
| 195 | // traffic.stopMonitoring(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | } |