blob: 182fbfb7f0ad43bf1d797014422b61e3679553cd [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
Simon Hunt2d7cd6f2017-05-04 13:04:50 -070088 /**
89 * Shuts down the background traffic monitoring task.
90 */
91 void ceaseAndDesist() {
92 traffic.stopMonitoring();
93 }
94
Simon Hunte6f64612017-04-28 00:01:48 -070095 // ==================================================================
96
97 private final class Topo2AllTraffic extends RequestHandler {
Simon Hunt9e2413e2017-05-03 14:25:40 -070098
Simon Hunte6f64612017-04-28 00:01:48 -070099 private Topo2AllTraffic() {
100 super(REQUEST_ALL_TRAFFIC);
101 }
102
103 @Override
104 public void process(ObjectNode payload) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700105 String mode = string(payload, TRAFFIC_TYPE);
106 log.debug("SHOW TRAFFIC: {}", mode);
107
Simon Hunte6f64612017-04-28 00:01:48 -0700108 switch (mode) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700109 case FLOW_STATS_BYTES:
110 traffic.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
Simon Hunte6f64612017-04-28 00:01:48 -0700111 break;
112
Simon Hunt9e2413e2017-05-03 14:25:40 -0700113 case PORT_STATS_BIT_SEC:
114 traffic.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700115 break;
116
Simon Hunt9e2413e2017-05-03 14:25:40 -0700117 case PORT_STATS_PKT_SEC:
118 traffic.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700119 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 Hunt9e2413e2017-05-03 14:25:40 -0700136 traffic.stopMonitoring();
Simon Hunte6f64612017-04-28 00:01:48 -0700137 }
138 }
139}
140
141