blob: 04d33910afcf00280c8889530f4bd93a74c74790 [file] [log] [blame]
Thomas Vachuska7c27ad72014-11-14 16:20:10 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska7c27ad72014-11-14 16:20:10 -08003 *
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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080017
18import com.google.common.collect.Sets;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.PortNumber;
21import org.onosproject.net.flow.FlowEntry;
22import org.onosproject.net.flow.FlowRule;
23import org.onosproject.net.flow.instructions.Instruction;
24import org.onosproject.net.flow.instructions.Instructions;
25import org.onosproject.net.statistic.StatisticStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070026import org.osgi.service.component.annotations.Activate;
27import org.osgi.service.component.annotations.Component;
28import org.osgi.service.component.annotations.Deactivate;
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080029import org.slf4j.Logger;
30
alshabib9c57bdd2014-11-28 19:14:06 -050031import java.util.Collections;
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080032import java.util.HashSet;
33import java.util.Map;
34import java.util.Set;
35import java.util.concurrent.ConcurrentHashMap;
36import java.util.concurrent.atomic.AtomicInteger;
37
38import static org.slf4j.LoggerFactory.getLogger;
39
40
41/**
42 * Maintains statistics using RPC calls to collect stats from remote instances
43 * on demand.
44 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045@Component(immediate = true, service = StatisticStore.class)
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080046public class SimpleStatisticStore implements StatisticStore {
47
48 private final Logger log = getLogger(getClass());
49
50 private Map<ConnectPoint, InternalStatisticRepresentation>
51 representations = new ConcurrentHashMap<>();
52
53 private Map<ConnectPoint, Set<FlowEntry>> previous = new ConcurrentHashMap<>();
54 private Map<ConnectPoint, Set<FlowEntry>> current = new ConcurrentHashMap<>();
55
56 @Activate
57 public void activate() {
58 log.info("Started");
59 }
60
61 @Deactivate
62 public void deactivate() {
63 log.info("Stopped");
64 }
65
66 @Override
67 public void prepareForStatistics(FlowRule rule) {
68 ConnectPoint cp = buildConnectPoint(rule);
69 if (cp == null) {
70 return;
71 }
72 InternalStatisticRepresentation rep;
73 synchronized (representations) {
74 rep = getOrCreateRepresentation(cp);
75 }
76 rep.prepare();
77 }
78
79 @Override
80 public synchronized void removeFromStatistics(FlowRule rule) {
81 ConnectPoint cp = buildConnectPoint(rule);
82 if (cp == null) {
83 return;
84 }
85 InternalStatisticRepresentation rep = representations.get(cp);
alshabib9c57bdd2014-11-28 19:14:06 -050086 if (rep != null && rep.remove(rule)) {
87 updatePublishedStats(cp, Collections.emptySet());
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080088 }
89 Set<FlowEntry> values = current.get(cp);
90 if (values != null) {
91 values.remove(rule);
92 }
93 values = previous.get(cp);
94 if (values != null) {
95 values.remove(rule);
96 }
97
98 }
99
100 @Override
101 public void addOrUpdateStatistic(FlowEntry rule) {
102 ConnectPoint cp = buildConnectPoint(rule);
103 if (cp == null) {
104 return;
105 }
106 InternalStatisticRepresentation rep = representations.get(cp);
107 if (rep != null && rep.submit(rule)) {
108 updatePublishedStats(cp, rep.get());
109 }
110 }
111
112 private synchronized void updatePublishedStats(ConnectPoint cp,
113 Set<FlowEntry> flowEntries) {
114 Set<FlowEntry> curr = current.get(cp);
115 if (curr == null) {
116 curr = new HashSet<>();
117 }
118 previous.put(cp, curr);
119 current.put(cp, flowEntries);
120
121 }
122
123 @Override
124 public Set<FlowEntry> getCurrentStatistic(ConnectPoint connectPoint) {
125 return getCurrentStatisticInternal(connectPoint);
126 }
127
128 private synchronized Set<FlowEntry> getCurrentStatisticInternal(ConnectPoint connectPoint) {
129 return current.get(connectPoint);
130 }
131
132 @Override
133 public Set<FlowEntry> getPreviousStatistic(ConnectPoint connectPoint) {
134 return getPreviousStatisticInternal(connectPoint);
135 }
136
137 private synchronized Set<FlowEntry> getPreviousStatisticInternal(ConnectPoint connectPoint) {
138 return previous.get(connectPoint);
139 }
140
141 private InternalStatisticRepresentation getOrCreateRepresentation(ConnectPoint cp) {
142
143 if (representations.containsKey(cp)) {
144 return representations.get(cp);
145 } else {
146 InternalStatisticRepresentation rep = new InternalStatisticRepresentation();
147 representations.put(cp, rep);
148 return rep;
149 }
150
151 }
152
153 private ConnectPoint buildConnectPoint(FlowRule rule) {
154 PortNumber port = getOutput(rule);
Jonathan Hart7baba072015-02-23 14:27:59 -0800155
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800156 if (port == null) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800157 return null;
158 }
159 ConnectPoint cp = new ConnectPoint(rule.deviceId(), port);
160 return cp;
161 }
162
163 private PortNumber getOutput(FlowRule rule) {
Ray Milkey42507352015-03-20 15:16:10 -0700164 for (Instruction i : rule.treatment().immediate()) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800165 if (i.type() == Instruction.Type.OUTPUT) {
166 Instructions.OutputInstruction out = (Instructions.OutputInstruction) i;
167 return out.port();
168 }
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800169 }
170 return null;
171 }
172
173 private class InternalStatisticRepresentation {
174
175 private final AtomicInteger counter = new AtomicInteger(0);
176 private final Set<FlowEntry> rules = new HashSet<>();
177
178 public void prepare() {
179 counter.incrementAndGet();
180 }
181
alshabib9c57bdd2014-11-28 19:14:06 -0500182 public synchronized boolean remove(FlowRule rule) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800183 rules.remove(rule);
alshabib9c57bdd2014-11-28 19:14:06 -0500184 return counter.decrementAndGet() == 0;
Thomas Vachuska7c27ad72014-11-14 16:20:10 -0800185 }
186
187 public synchronized boolean submit(FlowEntry rule) {
188 if (rules.contains(rule)) {
189 rules.remove(rule);
190 }
191 rules.add(rule);
192 if (counter.get() == 0) {
193 return true;
194 } else {
195 return counter.decrementAndGet() == 0;
196 }
197 }
198
199 public synchronized Set<FlowEntry> get() {
200 counter.set(rules.size());
201 return Sets.newHashSet(rules);
202 }
203
204 }
205
206}