blob: ce7175bb9e8e9b0149b55b1e6a7bbc661b694889 [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 Hunt8e258112017-05-05 13:19:04 -070028import org.onosproject.ui.topo.Highlights;
Simon Hunte6f64612017-04-28 00:01:48 -070029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Collection;
33
Simon Hunt8e258112017-05-05 13:19:04 -070034import static org.onosproject.ui.topo.TopoJson.topo2HighlightsMessage;
35
Simon Hunte6f64612017-04-28 00:01:48 -070036/**
37 * Server-side component to handle messages pertaining to topo-2 traffic.
38 */
39public 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 Hunt9e2413e2017-05-03 14:25:40 -070050 // 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 Hunt1911fe42017-05-02 18:25:58 -070055
Simon Hunt9e2413e2017-05-03 14:25:40 -070056 // configuration parameters
Simon Hunt1911fe42017-05-02 18:25:58 -070057 private static final long TRAFFIC_PERIOD = 5000;
58
Simon Hunte6f64612017-04-28 00:01:48 -070059// private UiTopoSession topoSession;
60// private Topo2Jsonifier t2json;
61
Simon Hunt1911fe42017-05-02 18:25:58 -070062 protected ServicesBundle services;
63 private String version;
64
65
66 private Traffic2Monitor traffic;
67
68
Simon Hunte6f64612017-04-28 00:01:48 -070069 @Override
70 public void init(UiConnection connection, ServiceDirectory directory) {
71 super.init(connection, directory);
72
Simon Hunt1911fe42017-05-02 18:25:58 -070073 services = new ServicesBundle(directory);
74
Simon Hunt9e2413e2017-05-03 14:25:40 -070075 traffic = new Traffic2Monitor(TRAFFIC_PERIOD, services, this);
76
Simon Hunte6f64612017-04-28 00:01:48 -070077 // 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 Hunt2d7cd6f2017-05-04 13:04:50 -070091 /**
92 * Shuts down the background traffic monitoring task.
93 */
94 void ceaseAndDesist() {
95 traffic.stopMonitoring();
96 }
97
Simon Hunt8e258112017-05-05 13:19:04 -070098 /**
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 Hunte6f64612017-04-28 00:01:48 -0700107 // ==================================================================
108
109 private final class Topo2AllTraffic extends RequestHandler {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700110
Simon Hunte6f64612017-04-28 00:01:48 -0700111 private Topo2AllTraffic() {
112 super(REQUEST_ALL_TRAFFIC);
113 }
114
115 @Override
116 public void process(ObjectNode payload) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700117 String mode = string(payload, TRAFFIC_TYPE);
118 log.debug("SHOW TRAFFIC: {}", mode);
119
Simon Hunte6f64612017-04-28 00:01:48 -0700120 switch (mode) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700121 case FLOW_STATS_BYTES:
122 traffic.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
Simon Hunte6f64612017-04-28 00:01:48 -0700123 break;
124
Simon Hunt9e2413e2017-05-03 14:25:40 -0700125 case PORT_STATS_BIT_SEC:
126 traffic.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700127 break;
128
Simon Hunt9e2413e2017-05-03 14:25:40 -0700129 case PORT_STATS_PKT_SEC:
130 traffic.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700131 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 Hunt9e2413e2017-05-03 14:25:40 -0700148 traffic.stopMonitoring();
Simon Hunte6f64612017-04-28 00:01:48 -0700149 }
150 }
151}
152
153