blob: 586ff8c91398b36993f1b653d3b3345b2cffb09a [file] [log] [blame]
Simon Hunte6f64612017-04-28 00:01:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Hunte6f64612017-04-28 00:01:48 -07003 *
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 Hunt0e161092017-05-08 17:41:38 -070027import org.onosproject.ui.impl.UiWebSocket;
Simon Hunt1911fe42017-05-02 18:25:58 -070028import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunt0e161092017-05-08 17:41:38 -070029import org.onosproject.ui.model.topo.UiLinkId;
30import org.onosproject.ui.model.topo.UiSynthLink;
Simon Hunt8e258112017-05-05 13:19:04 -070031import org.onosproject.ui.topo.Highlights;
Simon Hunte6f64612017-04-28 00:01:48 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.Collection;
Simon Hunt0e161092017-05-08 17:41:38 -070036import java.util.Map;
Simon Hunte6f64612017-04-28 00:01:48 -070037
Simon Hunt8e258112017-05-05 13:19:04 -070038import static org.onosproject.ui.topo.TopoJson.topo2HighlightsMessage;
39
Simon Hunte6f64612017-04-28 00:01:48 -070040/**
41 * Server-side component to handle messages pertaining to topo-2 traffic.
42 */
43public class Topo2TrafficMessageHandler extends UiMessageHandler {
44
45 private final Logger log = LoggerFactory.getLogger(getClass());
46
47 // === Inbound event identifiers
48 private static final String REQUEST_ALL_TRAFFIC = "topo2RequestAllTraffic";
49 private static final String CANCEL_TRAFFIC = "topo2CancelTraffic";
50
Simon Hunt9e2413e2017-05-03 14:25:40 -070051 // field values
52 private static final String TRAFFIC_TYPE = "trafficType";
53 private static final String FLOW_STATS_BYTES = "flowStatsBytes";
54 private static final String PORT_STATS_BIT_SEC = "portStatsBitSec";
55 private static final String PORT_STATS_PKT_SEC = "portStatsPktSec";
Simon Hunt1911fe42017-05-02 18:25:58 -070056
Simon Hunt9e2413e2017-05-03 14:25:40 -070057 // configuration parameters
Simon Hunt1911fe42017-05-02 18:25:58 -070058 private static final long TRAFFIC_PERIOD = 5000;
59
Simon Hunt1911fe42017-05-02 18:25:58 -070060 protected ServicesBundle services;
Simon Hunt1911fe42017-05-02 18:25:58 -070061
Simon Hunt0e161092017-05-08 17:41:38 -070062 private UiTopoSession topoSession;
Simon Hunt1911fe42017-05-02 18:25:58 -070063 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);
Simon Hunt9e2413e2017-05-03 14:25:40 -070071 traffic = new Traffic2Monitor(TRAFFIC_PERIOD, services, this);
Simon Hunt0e161092017-05-08 17:41:38 -070072 topoSession = ((UiWebSocket) connection).topoSession();
Simon Hunte6f64612017-04-28 00:01:48 -070073 }
74
75 @Override
76 protected Collection<RequestHandler> createRequestHandlers() {
77 return ImmutableSet.of(
78 new Topo2AllTraffic(),
79 new Topo2CancelTraffic()
80 );
81 }
82
Simon Hunt2d7cd6f2017-05-04 13:04:50 -070083 /**
84 * Shuts down the background traffic monitoring task.
85 */
86 void ceaseAndDesist() {
87 traffic.stopMonitoring();
88 }
89
Simon Hunt8e258112017-05-05 13:19:04 -070090 /**
91 * Sends a highlights message back to the client.
92 *
93 * @param highlights the highlights for transmission
94 */
95 void sendHighlights(Highlights highlights) {
96 sendMessage(topo2HighlightsMessage(highlights));
97 }
98
Simon Hunt0e161092017-05-08 17:41:38 -070099 /**
100 * Asks the topo session for the relevant synth links for current region.
101 * The returned map is keyed by "original" link.
102 *
103 * @return synth link map
104 */
105 Map<UiLinkId, UiSynthLink> retrieveRelevantSynthLinks() {
106 return topoSession.relevantSynthLinks();
107 }
108
Simon Hunte6f64612017-04-28 00:01:48 -0700109 // ==================================================================
110
111 private final class Topo2AllTraffic extends RequestHandler {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700112
Simon Hunte6f64612017-04-28 00:01:48 -0700113 private Topo2AllTraffic() {
114 super(REQUEST_ALL_TRAFFIC);
115 }
116
117 @Override
118 public void process(ObjectNode payload) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700119 String mode = string(payload, TRAFFIC_TYPE);
120 log.debug("SHOW TRAFFIC: {}", mode);
121
Simon Hunte6f64612017-04-28 00:01:48 -0700122 switch (mode) {
Simon Hunt9e2413e2017-05-03 14:25:40 -0700123 case FLOW_STATS_BYTES:
124 traffic.monitor(Mode.ALL_FLOW_TRAFFIC_BYTES);
Simon Hunte6f64612017-04-28 00:01:48 -0700125 break;
126
Simon Hunt9e2413e2017-05-03 14:25:40 -0700127 case PORT_STATS_BIT_SEC:
128 traffic.monitor(Mode.ALL_PORT_TRAFFIC_BIT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700129 break;
130
Simon Hunt9e2413e2017-05-03 14:25:40 -0700131 case PORT_STATS_PKT_SEC:
132 traffic.monitor(Mode.ALL_PORT_TRAFFIC_PKT_PS);
Simon Hunte6f64612017-04-28 00:01:48 -0700133 break;
134
135 default:
136 log.warn("Unknown traffic monitor type: " + mode);
137 break;
138 }
139 }
140 }
141
142 private final class Topo2CancelTraffic extends RequestHandler {
143 private Topo2CancelTraffic() {
144 super(CANCEL_TRAFFIC);
145 }
146
147 @Override
148 public void process(ObjectNode payload) {
149 log.debug("CANCEL TRAFFIC");
Simon Hunt9e2413e2017-05-03 14:25:40 -0700150 traffic.stopMonitoring();
Simon Hunte6f64612017-04-28 00:01:48 -0700151 }
152 }
153}
154
155