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; |
| 27 | import org.onosproject.ui.model.topo.UiRegion; |
| 28 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.Collection; |
| 33 | import java.util.List; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 36 | /* |
| 37 | NOTES: |
| 38 | |
| 39 | The original topology view message handler was broken into two classes |
| 40 | TopologyViewMessageHandler, and TopologyViewMessageHandlerBase. |
| 41 | We do not need to follow that model necessarily. Starting with a |
| 42 | single class, and breaking it apart later if necessary. |
| 43 | |
| 44 | Need to figure out the connection between this message handler and the |
| 45 | new way of doing things with UiTopoSession... |
| 46 | |
| 47 | */ |
| 48 | |
| 49 | /** |
| 50 | * Server-side component for interacting with the new "Region aware" topology |
| 51 | * view in the Web UI. |
| 52 | */ |
| 53 | public class Topo2ViewMessageHandler extends UiMessageHandler { |
| 54 | |
| 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
| 57 | // === Inbound event identifiers |
| 58 | private static final String TOPO2_START = "topo2Start"; |
| 59 | private static final String TOPO2_STOP = "topo2Stop"; |
| 60 | |
| 61 | // === Outbound event identifiers |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 62 | private static final String ALL_INSTANCES = "topo2AllInstances"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 63 | private static final String CURRENT_LAYOUT = "topo2CurrentLayout"; |
| 64 | private static final String CURRENT_REGION = "topo2CurrentRegion"; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 65 | private static final String PEER_REGIONS = "topo2PeerRegions"; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 66 | private static final String TOPO_START_DONE = "topo2StartDone"; |
| 67 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 68 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 69 | private UiTopoSession topoSession; |
| 70 | private Topo2Jsonifier t2json; |
| 71 | |
| 72 | |
| 73 | @Override |
| 74 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 75 | super.init(connection, directory); |
| 76 | |
| 77 | // get the topo session from the UiWebSocket |
| 78 | topoSession = ((UiWebSocket) connection).topoSession(); |
| 79 | t2json = new Topo2Jsonifier(directory); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected Collection<RequestHandler> createRequestHandlers() { |
| 84 | return ImmutableSet.of( |
| 85 | new Topo2Start(), |
| 86 | new Topo2Stop() |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | // ================================================================== |
| 91 | |
| 92 | |
| 93 | private final class Topo2Start extends RequestHandler { |
| 94 | private Topo2Start() { |
| 95 | super(TOPO2_START); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void process(long sid, ObjectNode payload) { |
| 100 | // client view is ready to receive data to display; so start up |
| 101 | // server-side processing, and send over initial state |
| 102 | |
| 103 | log.debug("topo2Start: {}", payload); |
| 104 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 105 | // this may be a little heavyweight, but it might be safer to do |
| 106 | // this than make assumptions about the order in which devices |
| 107 | // and regions are added... and thus internal linkages set up |
| 108 | // correctly |
| 109 | topoSession.refreshModel(); |
| 110 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 111 | // this is the list of ONOS cluster members |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 112 | List<UiClusterMember> instances = topoSession.getAllInstances(); |
| 113 | sendMessage(ALL_INSTANCES, t2json.instances(instances)); |
| 114 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 115 | // this is the layout that the user has chosen to display |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 116 | UiTopoLayout currentLayout = topoSession.currentLayout(); |
| 117 | sendMessage(CURRENT_LAYOUT, t2json.layout(currentLayout)); |
| 118 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 119 | // this is the region that is associated with the current layout |
| 120 | // this message includes details of the sub-regions, devices, |
| 121 | // hosts, and links within the region |
| 122 | // (as well as layer-order hints) |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 123 | UiRegion region = topoSession.getRegion(currentLayout); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 124 | Set<UiRegion> kids = topoSession.getSubRegions(currentLayout); |
| 125 | sendMessage(CURRENT_REGION, t2json.region(region, kids)); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 126 | |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 127 | // these are the regions that are siblings to this one |
| 128 | Set<UiRegion> peers = topoSession.getPeerRegions(currentLayout); |
| 129 | ObjectNode peersPayload = objectNode(); |
| 130 | peersPayload.set("peers", t2json.closedRegions(peers)); |
| 131 | sendMessage(PEER_REGIONS, peersPayload); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 132 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 133 | // finally, tell the UI that we are done : TODO review / delete?? |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 134 | sendMessage(TOPO_START_DONE, null); |
| 135 | |
| 136 | |
| 137 | // OLD CODE DID THE FOLLOWING... |
| 138 | // addListeners(); |
| 139 | // sendAllInstances(null); |
| 140 | // sendAllDevices(); |
| 141 | // sendAllLinks(); |
| 142 | // sendAllHosts(); |
| 143 | // sendTopoStartDone(); |
| 144 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 145 | |
| 146 | |
| 147 | } |
| 148 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 149 | private final class Topo2Stop extends RequestHandler { |
| 150 | private Topo2Stop() { |
| 151 | super(TOPO2_STOP); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void process(long sid, ObjectNode payload) { |
| 156 | // client view has gone away; so shut down server-side processing |
| 157 | // TODO: implement... |
| 158 | |
| 159 | log.debug("topo2Stop: {}", payload); |
| 160 | |
| 161 | // OLD CODE DID THE FOLLOWING... |
| 162 | // removeListeners(); |
| 163 | // stopSummaryMonitoring(); |
| 164 | // traffic.stopMonitoring(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } |