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