Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 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.topo; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 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; |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.impl.TrafficMonitorBase.Mode; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.impl.UiWebSocket; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.impl.topo.util.ServicesBundle; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.model.topo.UiLinkId; |
| 30 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.topo.Highlights; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.util.Collection; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 36 | import java.util.Map; |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 37 | |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 38 | import static org.onosproject.ui.topo.TopoJson.topo2HighlightsMessage; |
| 39 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Server-side component to handle messages pertaining to topo-2 traffic. |
| 42 | */ |
| 43 | public class Topo2TrafficMessageHandler extends UiMessageHandler { |
| 44 | |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | |
| 47 | // === Inbound event identifiers |
| 48 | private static final String REQUEST_ALL_TRAFFIC = "topo2RequestAllTraffic"; |
| 49 | private static final String CANCEL_TRAFFIC = "topo2CancelTraffic"; |
| 50 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 51 | // field values |
| 52 | private static final String TRAFFIC_TYPE = "trafficType"; |
| 53 | private static final String FLOW_STATS_BYTES = "flowStatsBytes"; |
| 54 | private static final String PORT_STATS_BIT_SEC = "portStatsBitSec"; |
| 55 | private static final String PORT_STATS_PKT_SEC = "portStatsPktSec"; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 56 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 57 | // configuration parameters |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 58 | private static final long TRAFFIC_PERIOD = 5000; |
| 59 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 60 | protected ServicesBundle services; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 61 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 62 | private UiTopoSession topoSession; |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 63 | private Traffic2Monitor traffic; |
| 64 | |
| 65 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 66 | @Override |
| 67 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 68 | super.init(connection, directory); |
| 69 | |
Simon Hunt | 1911fe4 | 2017-05-02 18:25:58 -0700 | [diff] [blame] | 70 | services = new ServicesBundle(directory); |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 71 | traffic = new Traffic2Monitor(TRAFFIC_PERIOD, services, this); |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 72 | topoSession = ((UiWebSocket) connection).topoSession(); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected Collection<RequestHandler> createRequestHandlers() { |
| 77 | return ImmutableSet.of( |
| 78 | new Topo2AllTraffic(), |
| 79 | new Topo2CancelTraffic() |
| 80 | ); |
| 81 | } |
| 82 | |
Simon Hunt | 2d7cd6f | 2017-05-04 13:04:50 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Shuts down the background traffic monitoring task. |
| 85 | */ |
| 86 | void ceaseAndDesist() { |
| 87 | traffic.stopMonitoring(); |
| 88 | } |
| 89 | |
Simon Hunt | 8e25811 | 2017-05-05 13:19:04 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Sends a highlights message back to the client. |
| 92 | * |
| 93 | * @param highlights the highlights for transmission |
| 94 | */ |
| 95 | void sendHighlights(Highlights highlights) { |
| 96 | sendMessage(topo2HighlightsMessage(highlights)); |
| 97 | } |
| 98 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Asks the topo session for the relevant synth links for current region. |
| 101 | * The returned map is keyed by "original" link. |
| 102 | * |
| 103 | * @return synth link map |
| 104 | */ |
| 105 | Map<UiLinkId, UiSynthLink> retrieveRelevantSynthLinks() { |
| 106 | return topoSession.relevantSynthLinks(); |
| 107 | } |
| 108 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 109 | // ================================================================== |
| 110 | |
| 111 | private final class Topo2AllTraffic extends RequestHandler { |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 112 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 113 | private Topo2AllTraffic() { |
| 114 | super(REQUEST_ALL_TRAFFIC); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void process(ObjectNode payload) { |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 119 | String mode = string(payload, TRAFFIC_TYPE); |
| 120 | log.debug("SHOW TRAFFIC: {}", mode); |
| 121 | |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 122 | switch (mode) { |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 123 | case FLOW_STATS_BYTES: |
| 124 | traffic.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 125 | break; |
| 126 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 127 | case PORT_STATS_BIT_SEC: |
| 128 | traffic.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 129 | break; |
| 130 | |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 131 | case PORT_STATS_PKT_SEC: |
| 132 | traffic.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 133 | break; |
| 134 | |
| 135 | default: |
| 136 | log.warn("Unknown traffic monitor type: " + mode); |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private final class Topo2CancelTraffic extends RequestHandler { |
| 143 | private Topo2CancelTraffic() { |
| 144 | super(CANCEL_TRAFFIC); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void process(ObjectNode payload) { |
| 149 | log.debug("CANCEL TRAFFIC"); |
Simon Hunt | 9e2413e | 2017-05-03 14:25:40 -0700 | [diff] [blame] | 150 | traffic.stopMonitoring(); |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |