Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 18 | package org.onosproject.ui.impl; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onosproject.core.CoreService; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.JsonUtils; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.RequestHandler; |
| 27 | import org.onosproject.ui.UiConnection; |
| 28 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.impl.topo.OverlayService; |
| 30 | import org.onosproject.ui.impl.topo.SummaryData; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.impl.topo.TopoUiEvent; |
| 32 | import org.onosproject.ui.impl.topo.TopoUiListener; |
| 33 | import org.onosproject.ui.impl.topo.TopoUiModelService; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.impl.topo.overlay.AbstractSummaryGenerator; |
| 35 | import org.onosproject.ui.impl.topo.overlay.SummaryGenerator; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.Collection; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 40 | import java.util.HashMap; |
| 41 | import java.util.Map; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 44 | import static java.lang.System.currentTimeMillis; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 45 | import static org.onosproject.ui.impl.topo.TopoUiEvent.Type.SUMMARY_UPDATE; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Facility for handling inbound messages from the topology view, and |
| 49 | * generating outbound messages for the same. |
| 50 | */ |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 51 | public class AltTopoViewMessageHandler extends UiMessageHandler |
| 52 | implements OverlayService { |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 53 | |
| 54 | private static final String TOPO_START = "topoStart"; |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 55 | private static final String TOPO_HEARTBEAT = "topoHeartbeat"; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 56 | private static final String TOPO_STOP = "topoStop"; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 57 | private static final String REQ_SUMMARY = "requestSummary"; |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 58 | private static final String CANCEL_SUMMARY = "cancelSummary"; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 59 | |
| 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 61 | |
| 62 | protected ServiceDirectory directory; |
| 63 | protected TopoUiModelService modelService; |
| 64 | |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 65 | private ModelListener modelListener; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 66 | private String version; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 67 | private SummaryGenerator defaultSummaryGenerator; |
| 68 | private SummaryGenerator currentSummaryGenerator; |
| 69 | |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 70 | |
| 71 | private boolean topoActive = false; |
| 72 | |
| 73 | @Override |
| 74 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 75 | super.init(connection, directory); |
| 76 | this.directory = checkNotNull(directory, "Directory cannot be null"); |
| 77 | modelService = directory.get(TopoUiModelService.class); |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 78 | defaultSummaryGenerator = new DefSummaryGenerator("node", "ONOS Summary"); |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 79 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 80 | bindEventHandlers(); |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 81 | modelListener = new ModelListener(); |
| 82 | version = getVersion(); |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 83 | currentSummaryGenerator = defaultSummaryGenerator; |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public void destroy() { |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 88 | cancelAllMonitoring(); |
| 89 | stopListeningToModel(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 90 | super.destroy(); |
| 91 | } |
| 92 | |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 93 | |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 94 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 95 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 96 | return ImmutableSet.of( |
| 97 | new TopoStart(), |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 98 | new TopoHeartbeat(), |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 99 | new TopoStop(), |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 100 | new ReqSummary(), |
| 101 | new CancelSummary() |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 102 | // TODO: add more handlers here..... |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 103 | ); |
| 104 | } |
| 105 | |
| 106 | // ===================================================================== |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 107 | |
| 108 | private void cancelAllMonitoring() { |
| 109 | // TODO: |
| 110 | } |
| 111 | |
| 112 | private void startListeningToModel() { |
| 113 | topoActive = true; |
| 114 | modelService.addListener(modelListener); |
| 115 | } |
| 116 | |
| 117 | private void stopListeningToModel() { |
| 118 | topoActive = false; |
| 119 | modelService.removeListener(modelListener); |
| 120 | } |
| 121 | |
| 122 | private String getVersion() { |
| 123 | String ver = directory.get(CoreService.class).version().toString(); |
| 124 | return ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", ""); |
| 125 | } |
| 126 | |
| 127 | // ===================================================================== |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 128 | // Overlay Service |
| 129 | // TODO: figure out how we are going to switch overlays in and out... |
| 130 | |
| 131 | private final Map<String, SummaryGenerator> summGenCache = Maps.newHashMap(); |
| 132 | |
| 133 | @Override |
| 134 | public void addSummaryGenerator(String overlayId, SummaryGenerator generator) { |
| 135 | log.info("Adding custom Summary Generator for overlay [{}]", overlayId); |
| 136 | summGenCache.put(overlayId, generator); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void removeSummaryGenerator(String overlayId) { |
| 141 | summGenCache.remove(overlayId); |
| 142 | log.info("Custom Summary Generator for overlay [{}] removed", overlayId); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | // ===================================================================== |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 148 | // Request Handlers for (topo view) events from the UI... |
| 149 | |
| 150 | private final class TopoStart extends RequestHandler { |
| 151 | private TopoStart() { |
| 152 | super(TOPO_START); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 157 | startListeningToModel(); |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 158 | sendMessages(modelService.getInitialState()); |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 162 | private final class TopoHeartbeat extends RequestHandler { |
| 163 | private TopoHeartbeat() { |
| 164 | super(TOPO_HEARTBEAT); |
| 165 | } |
| 166 | @Override |
| 167 | public void process(long sid, ObjectNode payload) { |
| 168 | modelListener.nudge(); |
| 169 | } |
| 170 | } |
| 171 | |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 172 | private final class TopoStop extends RequestHandler { |
| 173 | private TopoStop() { |
| 174 | super(TOPO_STOP); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 179 | stopListeningToModel(); |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | private final class ReqSummary extends RequestHandler { |
| 184 | private ReqSummary() { |
| 185 | super(REQ_SUMMARY); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void process(long sid, ObjectNode payload) { |
| 190 | modelService.startSummaryMonitoring(); |
| 191 | // NOTE: showSummary messages forwarded through the model listener |
| 192 | } |
| 193 | } |
| 194 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 195 | private final class CancelSummary extends RequestHandler { |
| 196 | private CancelSummary() { |
| 197 | super(CANCEL_SUMMARY); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void process(long sid, ObjectNode payload) { |
| 202 | modelService.stopSummaryMonitoring(); |
| 203 | } |
| 204 | } |
| 205 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 206 | // ===================================================================== |
| 207 | |
| 208 | private final class DefSummaryGenerator extends AbstractSummaryGenerator { |
| 209 | public DefSummaryGenerator(String iconId, String title) { |
| 210 | super(iconId, title); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public ObjectNode generateSummary() { |
| 215 | SummaryData data = modelService.getSummaryData(); |
| 216 | iconId("node"); |
| 217 | title("ONOS Summary"); |
| 218 | clearProps(); |
| 219 | prop("Devices", format(data.deviceCount())); |
| 220 | prop("Links", format(data.linkCount())); |
| 221 | prop("Hosts", format(data.hostCount())); |
| 222 | prop("Topology SCCs", format(data.clusterCount())); |
| 223 | separator(); |
| 224 | prop("Intents", format(data.intentCount())); |
| 225 | prop("Flows", format(data.flowRuleCount())); |
| 226 | prop("Version", version); |
| 227 | return buildObjectNode(); |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | // ===================================================================== |
| 232 | // Private Helper Methods... |
| 233 | |
| 234 | private void sendMessages(Collection<ObjectNode> messages) { |
| 235 | if (topoActive) { |
| 236 | UiConnection connection = connection(); |
| 237 | if (connection != null) { |
| 238 | messages.forEach(connection::sendMessage); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 243 | private void sendMessages(ObjectNode message) { |
| 244 | if (topoActive) { |
| 245 | UiConnection connection = connection(); |
| 246 | if (connection != null) { |
| 247 | connection.sendMessage(message); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 252 | // ===================================================================== |
| 253 | // Our listener for model events so we can push changes out to the UI... |
| 254 | |
| 255 | private class ModelListener implements TopoUiListener { |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 256 | private static final long AWAKE_THRESHOLD_MS = 6000; |
| 257 | |
| 258 | private long lastNudged = currentTimeMillis(); |
| 259 | |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 260 | @Override |
| 261 | public void event(TopoUiEvent event) { |
| 262 | log.debug("Handle Event: {}", event); |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 263 | ModelEventHandler handler = eventHandlerBinding.get(event.type()); |
| 264 | |
| 265 | // any handlers not bound explicitly are assumed to be pass-thru... |
| 266 | if (handler == null) { |
| 267 | handler = passThruHandler; |
| 268 | } |
| 269 | handler.handleEvent(event); |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 270 | } |
Simon Hunt | 732bb2e | 2015-05-13 18:32:16 -0700 | [diff] [blame] | 271 | |
| 272 | @Override |
| 273 | public boolean isAwake() { |
| 274 | return currentTimeMillis() - lastNudged < AWAKE_THRESHOLD_MS; |
| 275 | } |
| 276 | |
| 277 | public void nudge() { |
| 278 | lastNudged = currentTimeMillis(); |
| 279 | } |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 280 | } |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 281 | |
| 282 | |
| 283 | // ===================================================================== |
| 284 | // Model Event Handler definitions and bindings... |
| 285 | |
| 286 | private interface ModelEventHandler { |
| 287 | void handleEvent(TopoUiEvent event); |
| 288 | } |
| 289 | |
| 290 | private ModelEventHandler passThruHandler = event -> { |
| 291 | // simply forward the event message as is |
| 292 | ObjectNode message = event.subject(); |
| 293 | if (message != null) { |
| 294 | sendMessages(event.subject()); |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | private ModelEventHandler summaryHandler = event -> { |
| 299 | // use the currently selected summary generator to create the body.. |
| 300 | ObjectNode payload = currentSummaryGenerator.generateSummary(); |
| 301 | sendMessages(JsonUtils.envelope("showSummary", payload)); |
| 302 | }; |
| 303 | |
| 304 | |
| 305 | // TopoUiEvent type binding of handlers |
| 306 | private final Map<TopoUiEvent.Type, ModelEventHandler> |
| 307 | eventHandlerBinding = new HashMap<>(); |
| 308 | |
| 309 | private void bindEventHandlers() { |
| 310 | eventHandlerBinding.put(SUMMARY_UPDATE, summaryHandler); |
| 311 | // NOTE: no need to bind pass-thru handlers |
| 312 | } |
Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 313 | } |