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; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 20 | import com.google.common.collect.ImmutableList; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 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; |
| 26 | import org.onosproject.ui.impl.UiWebSocket; |
| 27 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 28 | import org.onosproject.ui.model.topo.UiDevice; |
| 29 | import org.onosproject.ui.model.topo.UiHost; |
| 30 | import org.onosproject.ui.model.topo.UiLink; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiRegion; |
| 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 | |
| 40 | import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | NOTES: |
| 44 | |
| 45 | The original topology view message handler was broken into two classes |
| 46 | TopologyViewMessageHandler, and TopologyViewMessageHandlerBase. |
| 47 | We do not need to follow that model necessarily. Starting with a |
| 48 | single class, and breaking it apart later if necessary. |
| 49 | |
| 50 | Need to figure out the connection between this message handler and the |
| 51 | new way of doing things with UiTopoSession... |
| 52 | |
| 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * Server-side component for interacting with the new "Region aware" topology |
| 57 | * view in the Web UI. |
| 58 | */ |
| 59 | public class Topo2ViewMessageHandler extends UiMessageHandler { |
| 60 | |
| 61 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 62 | |
| 63 | // === Inbound event identifiers |
| 64 | private static final String TOPO2_START = "topo2Start"; |
| 65 | private static final String TOPO2_STOP = "topo2Stop"; |
| 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"; |
| 72 | private static final String ORPHANS = "topo2Orphans"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 73 | private static final String TOPO_START_DONE = "topo2StartDone"; |
| 74 | |
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; |
| 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(); |
| 86 | t2json = new Topo2Jsonifier(directory); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected Collection<RequestHandler> createRequestHandlers() { |
| 91 | return ImmutableSet.of( |
| 92 | new Topo2Start(), |
| 93 | new Topo2Stop() |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | // ================================================================== |
| 98 | |
| 99 | |
| 100 | private final class Topo2Start extends RequestHandler { |
| 101 | private Topo2Start() { |
| 102 | super(TOPO2_START); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void process(long sid, ObjectNode payload) { |
| 107 | // client view is ready to receive data to display; so start up |
| 108 | // server-side processing, and send over initial state |
| 109 | |
| 110 | log.debug("topo2Start: {}", payload); |
| 111 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 112 | // this is the list of ONOS cluster members |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 113 | List<UiClusterMember> instances = topoSession.getAllInstances(); |
| 114 | sendMessage(ALL_INSTANCES, t2json.instances(instances)); |
| 115 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 116 | // this is the layout that the user has chosen to display |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 117 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
| 118 | sendMessage(CURRENT_LAYOUT, t2json.layout(currentLayout)); |
| 119 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 120 | // this is the region that is associated with the current layout |
| 121 | // this message includes details of the sub-regions, devices, |
| 122 | // hosts, and links within the region |
| 123 | // (as well as layer-order hints) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 124 | UiRegion region = topoSession.getRegion(currentLayout); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 125 | Set<UiRegion> kids = topoSession.getSubRegions(currentLayout); |
| 126 | sendMessage(CURRENT_REGION, t2json.region(region, kids)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 127 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 128 | // these are the regions that are siblings to this one |
| 129 | Set<UiRegion> peers = topoSession.getPeerRegions(currentLayout); |
| 130 | ObjectNode peersPayload = objectNode(); |
| 131 | peersPayload.set("peers", t2json.closedRegions(peers)); |
| 132 | sendMessage(PEER_REGIONS, peersPayload); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 133 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 134 | // return devices, hosts, links belonging to no region |
| 135 | Set<UiDevice> oDevices = topoSession.getOrphanDevices(); |
| 136 | Set<UiHost> oHosts = topoSession.getOrphanHosts(); |
| 137 | Set<UiLink> oLinks = topoSession.getOrphanLinks(); |
| 138 | List<String> oLayers = getOrphanLayerOrder(); |
| 139 | sendMessage(ORPHANS, t2json.orphans(oDevices, oHosts, oLinks, oLayers)); |
| 140 | |
| 141 | // finally, tell the UI that we are done |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 142 | sendMessage(TOPO_START_DONE, null); |
| 143 | |
| 144 | |
| 145 | // OLD CODE DID THE FOLLOWING... |
| 146 | // addListeners(); |
| 147 | // sendAllInstances(null); |
| 148 | // sendAllDevices(); |
| 149 | // sendAllLinks(); |
| 150 | // sendAllHosts(); |
| 151 | // sendTopoStartDone(); |
| 152 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame^] | 153 | |
| 154 | |
| 155 | } |
| 156 | |
| 157 | // TODO: we need to decide on how this should really get populated. |
| 158 | // For example, to be "backward compatible", this should really be |
| 159 | // [ LAYER_OPTICAL, LAYER_PACKET, LAYER_DEFAULT ] |
| 160 | private List<String> getOrphanLayerOrder() { |
| 161 | // NOTE that LAYER_DEFAULT must always be last in the array |
| 162 | return ImmutableList.of(LAYER_DEFAULT); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private final class Topo2Stop extends RequestHandler { |
| 166 | private Topo2Stop() { |
| 167 | super(TOPO2_STOP); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void process(long sid, ObjectNode payload) { |
| 172 | // client view has gone away; so shut down server-side processing |
| 173 | // TODO: implement... |
| 174 | |
| 175 | log.debug("topo2Stop: {}", payload); |
| 176 | |
| 177 | // OLD CODE DID THE FOLLOWING... |
| 178 | // removeListeners(); |
| 179 | // stopSummaryMonitoring(); |
| 180 | // traffic.stopMonitoring(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } |