blob: 2fa370161349f23ed8d9061e5959700d0b61f558 [file] [log] [blame]
Simon Hunte6f64612017-04-28 00:01:48 -07001/*
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
18package org.onosproject.ui.impl.topo;
19
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.osgi.ServiceDirectory;
23import org.onosproject.ui.RequestHandler;
24import org.onosproject.ui.UiConnection;
25import org.onosproject.ui.UiMessageHandler;
Simon Hunt9e2413e2017-05-03 14:25:40 -070026import org.onosproject.ui.impl.TrafficMonitorBase.Mode;
Simon Hunt1911fe42017-05-02 18:25:58 -070027import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunte6f64612017-04-28 00:01:48 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.util.Collection;
32
33/**
34 * Server-side component to handle messages pertaining to topo-2 traffic.
35 */
36public 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 Hunt9e2413e2017-05-03 14:25:40 -070047 // 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 Hunt1911fe42017-05-02 18:25:58 -070052
Simon Hunt9e2413e2017-05-03 14:25:40 -070053 // configuration parameters
Simon Hunt1911fe42017-05-02 18:25:58 -070054 private static final long TRAFFIC_PERIOD = 5000;
55
Simon Hunte6f64612017-04-28 00:01:48 -070056// private UiTopoSession topoSession;
57// private Topo2Jsonifier t2json;
58
Simon Hunt1911fe42017-05-02 18:25:58 -070059 protected ServicesBundle services;
60 private String version;
61
62
63 private Traffic2Monitor traffic;
64
65
Simon Hunte6f64612017-04-28 00:01:48 -070066 @Override
67 public void init(UiConnection connection, ServiceDirectory directory) {
68 super.init(connection, directory);
69
Simon Hunt1911fe42017-05-02 18:25:58 -070070 services = new ServicesBundle(directory);
71
Simon Hunt9e2413e2017-05-03 14:25:40 -070072 traffic = new Traffic2Monitor(TRAFFIC_PERIOD, services, this);
73
Simon Hunte6f64612017-04-28 00:01:48 -070074 // 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
88 // ==================================================================
89
90 private final class Topo2AllTraffic extends RequestHandler {
Simon Hunt9e2413e2017-05-03 14:25:40 -070091
Simon Hunte6f64612017-04-28 00:01:48 -070092 private Topo2AllTraffic() {
93 super(REQUEST_ALL_TRAFFIC);
94 }
95
96 @Override
97 public void process(ObjectNode payload) {
Simon Hunt9e2413e2017-05-03 14:25:40 -070098 String mode = string(payload, TRAFFIC_TYPE);
99 log.debug("SHOW TRAFFIC: {}", mode);
100
Simon Hunte6f64612017-04-28 00:01:48 -0700101 switch (mode) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700102 case FLOW_STATS_BYTES:
103 traffic.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
Simon Hunte6f64612017-04-28 00:01:48 -0700104 break;
105
Simon Hunt9e2413e2017-05-03 14:25:40 -0700106 case PORT_STATS_BIT_SEC:
107 traffic.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700108 break;
109
Simon Hunt9e2413e2017-05-03 14:25:40 -0700110 case PORT_STATS_PKT_SEC:
111 traffic.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700112 break;
113
114 default:
115 log.warn("Unknown traffic monitor type: " + mode);
116 break;
117 }
118 }
119 }
120
121 private final class Topo2CancelTraffic extends RequestHandler {
122 private Topo2CancelTraffic() {
123 super(CANCEL_TRAFFIC);
124 }
125
126 @Override
127 public void process(ObjectNode payload) {
128 log.debug("CANCEL TRAFFIC");
Simon Hunt9e2413e2017-05-03 14:25:40 -0700129 traffic.stopMonitoring();
Simon Hunte6f64612017-04-28 00:01:48 -0700130 }
131 }
132}
133
134