blob: 60e7e83ca0f56dc09ac3c96b0cc6d2580b56ca86 [file] [log] [blame]
Simon Hunt1911fe42017-05-02 18:25:58 -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 org.onosproject.ui.impl.TrafficMonitorBase;
21import org.onosproject.ui.impl.topo.util.ServicesBundle;
Simon Hunta7aea842017-05-03 19:42:50 -070022import org.onosproject.ui.impl.topo.util.TrafficLink;
Simon Hunt0e161092017-05-08 17:41:38 -070023import org.onosproject.ui.model.topo.UiLinkId;
24import org.onosproject.ui.model.topo.UiSynthLink;
Simon Hunta7aea842017-05-03 19:42:50 -070025import org.onosproject.ui.topo.Highlights;
Simon Hunt1911fe42017-05-02 18:25:58 -070026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
Simon Hunt0e161092017-05-08 17:41:38 -070029import java.util.HashMap;
30import java.util.HashSet;
31import java.util.Map;
Simon Hunta7aea842017-05-03 19:42:50 -070032import java.util.Set;
33
Simon Hunt1911fe42017-05-02 18:25:58 -070034/**
35 * Encapsulates the behavior of monitoring specific traffic patterns in the
36 * Topology-2 view.
37 */
38public class Traffic2Monitor extends TrafficMonitorBase {
39
40 private static final Logger log =
41 LoggerFactory.getLogger(Traffic2Monitor.class);
42
Simon Hunta7aea842017-05-03 19:42:50 -070043 // link back to our message handler (for outbound messages)
Simon Hunt9e2413e2017-05-03 14:25:40 -070044 private final Topo2TrafficMessageHandler msgHandler;
45
Simon Hunt1911fe42017-05-02 18:25:58 -070046 /**
47 * Constructs a traffic monitor.
48 *
Simon Hunt9e2413e2017-05-03 14:25:40 -070049 * @param trafficPeriod traffic task period in ms
Simon Hunt1911fe42017-05-02 18:25:58 -070050 * @param servicesBundle bundle of services
Simon Hunt9e2413e2017-05-03 14:25:40 -070051 * @param msgHandler our message handler
Simon Hunt1911fe42017-05-02 18:25:58 -070052 */
Simon Hunt9e2413e2017-05-03 14:25:40 -070053 public Traffic2Monitor(long trafficPeriod, ServicesBundle servicesBundle,
54 Topo2TrafficMessageHandler msgHandler) {
Simon Hunt1911fe42017-05-02 18:25:58 -070055 super(trafficPeriod, servicesBundle);
Simon Hunt9e2413e2017-05-03 14:25:40 -070056 this.msgHandler = msgHandler;
Simon Hunt1911fe42017-05-02 18:25:58 -070057 }
58
59 @Override
60 protected void sendAllFlowTraffic() {
Simon Hunt9e2413e2017-05-03 14:25:40 -070061 log.debug("TOPO-2-TRAFFIC: sendAllFlowTraffic");
Simon Hunt8e258112017-05-05 13:19:04 -070062 msgHandler.sendHighlights(trafficSummary(TrafficLink.StatsType.FLOW_STATS));
Simon Hunt1911fe42017-05-02 18:25:58 -070063 }
64
65 @Override
66 protected void sendAllPortTrafficBits() {
Simon Hunt9e2413e2017-05-03 14:25:40 -070067 log.debug("TOPO-2-TRAFFIC: sendAllPortTrafficBits");
Simon Hunt8e258112017-05-05 13:19:04 -070068 msgHandler.sendHighlights(trafficSummary(TrafficLink.StatsType.PORT_STATS));
Simon Hunt1911fe42017-05-02 18:25:58 -070069 }
70
71 @Override
72 protected void sendAllPortTrafficPackets() {
Simon Hunt9e2413e2017-05-03 14:25:40 -070073 log.debug("TOPO-2-TRAFFIC: sendAllPortTrafficPackets");
Simon Hunt8e258112017-05-05 13:19:04 -070074 msgHandler.sendHighlights(trafficSummary(TrafficLink.StatsType.PORT_PACKET_STATS));
Simon Hunt1911fe42017-05-02 18:25:58 -070075 }
76
77 @Override
Simon Hunt1911fe42017-05-02 18:25:58 -070078 protected void sendClearHighlights() {
Simon Hunt9e2413e2017-05-03 14:25:40 -070079 log.debug("TOPO-2-TRAFFIC: sendClearHighlights");
Simon Hunt8e258112017-05-05 13:19:04 -070080 msgHandler.sendHighlights(new Highlights());
Simon Hunt1911fe42017-05-02 18:25:58 -070081 }
82
Simon Hunt9e2413e2017-05-03 14:25:40 -070083
84 // NOTE: currently this monitor holds no state - nothing to do for these...
85 @Override
86 protected void sendDeviceLinkFlows() {
87 }
88
89 @Override
90 protected void sendSelectedIntentTraffic() {
91 }
92
Simon Hunt1911fe42017-05-02 18:25:58 -070093 @Override
94 protected void clearSelection() {
Simon Hunt1911fe42017-05-02 18:25:58 -070095 }
Simon Hunta7aea842017-05-03 19:42:50 -070096
97 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98 // -- link aggregation
99
100
101 @Override
102 protected Set<TrafficLink> doAggregation(Set<TrafficLink> linksWithTraffic) {
Simon Hunta7aea842017-05-03 19:42:50 -0700103 log.debug("Need to aggregate {} links", linksWithTraffic.size());
104
Simon Hunt0e161092017-05-08 17:41:38 -0700105 // first, retrieve from the shared topology model those synth links that
106 // are part of the region currently being viewed by the user...
107 Map<UiLinkId, UiSynthLink> synthLinkMap =
108 msgHandler.retrieveRelevantSynthLinks();
109
110 // NOTE: compute Set<TrafficLink> which represents the consolidated links
111
112 Map<UiLinkId, TrafficLink> mappedByUiLinkId = new HashMap<>();
113
114 for (TrafficLink tl : linksWithTraffic) {
115 UiLinkId tlid = UiLinkId.uiLinkId(tl.key());
116 UiSynthLink sl = synthLinkMap.get(tlid);
117 if (sl != null) {
118 UiLinkId aggrid = sl.link().id();
119 TrafficLink aggregated = mappedByUiLinkId.get(aggrid);
120 if (aggregated == null) {
121 aggregated = new TrafficLink(tl);
122 mappedByUiLinkId.put(aggrid, aggregated);
123 } else {
124 aggregated.mergeStats(tl);
125 }
126 }
127 }
128
129 Set<TrafficLink> result = new HashSet<>();
130 result.addAll(mappedByUiLinkId.values());
131 return result;
Simon Hunta7aea842017-05-03 19:42:50 -0700132 }
Simon Hunt1911fe42017-05-02 18:25:58 -0700133}