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