blob: 380625012a17aa9cedaf69cd0b261f1354b179d5 [file] [log] [blame]
Jordi Ortizaa8de492016-12-01 00:21:36 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jordi Ortizaa8de492016-12-01 00:21:36 +01003 *
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 */
16package org.onosproject.net.meter;
17
18import com.google.common.base.MoreObjects;
cansu.toprak409289d2017-10-27 10:04:05 +030019import com.google.common.collect.Sets;
Jordi Ortizaa8de492016-12-01 00:21:36 +010020import org.onosproject.net.DeviceId;
21
22import java.util.HashSet;
23import java.util.Set;
24
Wailok Shumf013a782021-07-26 16:51:01 +080025import static com.google.common.base.Preconditions.checkArgument;
Jordi Ortizaa8de492016-12-01 00:21:36 +010026import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Default implementation of MeterFeatures.
30 */
31public final class DefaultMeterFeatures implements MeterFeatures {
32 private DeviceId deviceId;
Wailok Shumf013a782021-07-26 16:51:01 +080033 private long startIndex;
34 private long endIndex;
Jordi Ortizaa8de492016-12-01 00:21:36 +010035 private Set<Band.Type> bandTypes;
36 private Set<Meter.Unit> units;
37 private boolean burst;
38 private boolean stats;
39 private short maxBands;
40 private short maxColor;
cansu.toprak409289d2017-10-27 10:04:05 +030041 private Set<MeterFeaturesFlag> features;
Wailok Shumf013a782021-07-26 16:51:01 +080042 private MeterScope scope;
Jordi Ortizaa8de492016-12-01 00:21:36 +010043
Wailok Shumf013a782021-07-26 16:51:01 +080044 private DefaultMeterFeatures(DeviceId did, long startIndex, long endIndex,
Jordi Ortizaa8de492016-12-01 00:21:36 +010045 Set<Band.Type> bandTypes, Set<Meter.Unit> units,
46 boolean burst, boolean stats,
Wailok Shumf013a782021-07-26 16:51:01 +080047 short maxBands, short maxColor, Set<MeterFeaturesFlag> flag,
48 MeterScope scope) {
Jordi Ortizaa8de492016-12-01 00:21:36 +010049 this.deviceId = did;
Wailok Shumf013a782021-07-26 16:51:01 +080050 this.startIndex = startIndex;
51 this.endIndex = endIndex;
Jordi Ortizaa8de492016-12-01 00:21:36 +010052 this.bandTypes = bandTypes;
53 this.burst = burst;
54 this.stats = stats;
55 this.units = units;
56 this.maxBands = maxBands;
57 this.maxColor = maxColor;
cansu.toprak409289d2017-10-27 10:04:05 +030058 this.features = flag;
Wailok Shumf013a782021-07-26 16:51:01 +080059 this.scope = scope;
Jordi Ortizaa8de492016-12-01 00:21:36 +010060 }
61
62 @Override
63 public DeviceId deviceId() {
64 return deviceId;
65 }
66
67 @Override
68 public long maxMeter() {
Wailok Shumf013a782021-07-26 16:51:01 +080069 // For OpenFlow meter, return end index as maxMeter
70 return scope.isGlobal() ? endIndex + 1 : endIndex - startIndex + 1;
71 }
72
73 @Override
74 public long startIndex() {
75 return startIndex;
76 }
77
78 @Override
79 public long endIndex() {
80 return endIndex;
Jordi Ortizaa8de492016-12-01 00:21:36 +010081 }
82
83 @Override
84 public Set<Band.Type> bandTypes() {
85 return bandTypes;
86 }
87
88 @Override
89 public Set<Meter.Unit> unitTypes() {
90 return units;
91 }
92
93 @Override
94 public boolean isBurstSupported() {
95 return burst;
96 }
97
98 @Override
99 public boolean isStatsSupported() {
100 return stats;
101 }
102
103 @Override
104 public short maxBands() {
105 return maxBands;
106 }
107
108 @Override
109 public short maxColor() {
110 return maxColor;
111 }
112
cansu.toprak409289d2017-10-27 10:04:05 +0300113 @Override
114 public Set<MeterFeaturesFlag> features() {
115 return features;
116 }
117
Wailok Shumf013a782021-07-26 16:51:01 +0800118 @Override
119 public MeterScope scope() {
120 return scope;
121 }
122
Jordi Ortizaa8de492016-12-01 00:21:36 +0100123 public static Builder builder() {
124 return new Builder();
125 }
126
127 public static MeterFeatures noMeterFeatures(DeviceId deviceId) {
128 return DefaultMeterFeatures.builder().forDevice(deviceId)
129 .build();
130 }
131
132 @Override
133 public String toString() {
134 return MoreObjects.toStringHelper(getClass())
135 .add("deviceId", deviceId())
Wailok Shumf013a782021-07-26 16:51:01 +0800136 .add("startIndex", startIndex())
137 .add("endIndex", endIndex())
Jordi Ortizaa8de492016-12-01 00:21:36 +0100138 .add("maxBands", maxBands())
139 .add("maxColor", maxColor())
140 .add("bands", bandTypes())
141 .add("burst", isBurstSupported())
142 .add("stats", isStatsSupported())
143 .add("units", unitTypes())
Wailok Shumf013a782021-07-26 16:51:01 +0800144 .add("scope", scope())
Jordi Ortizaa8de492016-12-01 00:21:36 +0100145 .toString();
146 }
147
148 /**
149 * A DefaultMeterFeatures builder.
150 */
151 public static final class Builder implements MeterFeatures.Builder {
152 private DeviceId did;
153 private long mmeter = 0L;
Wailok Shumf013a782021-07-26 16:51:01 +0800154 private long starti = -1L;
155 private long endi = -1L;
Jordi Ortizaa8de492016-12-01 00:21:36 +0100156 private short mbands = 0;
157 private short mcolors = 0;
158 private Set<Band.Type> bandTypes = new HashSet<>();
159 private Set<Meter.Unit> units1 = new HashSet<>();
160 private boolean burst = false;
161 private boolean stats = false;
cansu.toprak409289d2017-10-27 10:04:05 +0300162 private Set<MeterFeaturesFlag> features = Sets.newHashSet();
Wailok Shumf013a782021-07-26 16:51:01 +0800163 private MeterScope mscope = MeterScope.globalScope();
Jordi Ortizaa8de492016-12-01 00:21:36 +0100164
165 @Override
166 public MeterFeatures.Builder forDevice(DeviceId deviceId) {
167 did = deviceId;
168 return this;
169 }
170
171 @Override
172 public MeterFeatures.Builder withMaxMeters(long maxMeter) {
173 mmeter = maxMeter;
174 return this;
175 }
176
177 @Override
Wailok Shumf013a782021-07-26 16:51:01 +0800178 public MeterFeatures.Builder withStartIndex(long startIndex) {
179 starti = startIndex;
180 return this;
181 }
182
183 @Override
184 public MeterFeatures.Builder withEndIndex(long endIndex) {
185 endi = endIndex;
186 return this;
187 }
188
189 @Override
Jordi Ortizaa8de492016-12-01 00:21:36 +0100190 public MeterFeatures.Builder withMaxBands(short maxBands) {
191 mbands = maxBands;
192 return this;
193 }
194
195 @Override
196 public MeterFeatures.Builder withMaxColors(short maxColors) {
197 mcolors = maxColors;
198 return this;
199 }
200
201 @Override
202 public MeterFeatures.Builder withBandTypes(Set<Band.Type> types) {
203 bandTypes = types;
204 return this;
205 }
206
207 @Override
208 public MeterFeatures.Builder withUnits(Set<Meter.Unit> units) {
209 units1 = units;
210 return this;
211 }
212
213 @Override
214 public MeterFeatures.Builder hasBurst(boolean hasBurst) {
215 burst = hasBurst;
216 return this;
217 }
218
219 @Override
220 public MeterFeatures.Builder hasStats(boolean hasStats) {
221 stats = hasStats;
222 return this;
223 }
224
225 @Override
cansu.toprak409289d2017-10-27 10:04:05 +0300226 public MeterFeatures.Builder withFeatures(Set<MeterFeaturesFlag> featureFlags) {
227 features = featureFlags;
228 return this;
229 }
230
231 @Override
Wailok Shumf013a782021-07-26 16:51:01 +0800232 public MeterFeatures.Builder withScope(MeterScope scope) {
233 mscope = scope;
234 return this;
235 }
236
237 @Override
Jordi Ortizaa8de492016-12-01 00:21:36 +0100238 public MeterFeatures build() {
Wailok Shumf013a782021-07-26 16:51:01 +0800239 // In case some functions are using maxMeter
240 // and both indexes are not set
241 // Start index will be
242 // 1, if it is global scope (An OpenFlow meter)
243 // 0, for the rest (A P4RT meter)
244 if (mmeter != 0L && starti == -1L && endi == -1L) {
245 starti = mscope.isGlobal() ? 1 : 0;
246 endi = mmeter - 1;
247 }
248 // If one of the index is unset/unvalid value, treated as no meter features
249 if (starti <= -1 || endi <= -1) {
250 starti = -1;
251 endi = -1;
252 }
253
Jordi Ortizaa8de492016-12-01 00:21:36 +0100254 checkNotNull(did, "Must specify a device");
Wailok Shumf013a782021-07-26 16:51:01 +0800255 checkArgument(starti <= endi, "Start index must be less than or equal to end index");
256
257 return new DefaultMeterFeatures(did, starti, endi, bandTypes, units1, burst,
258 stats, mbands, mcolors, features, mscope);
Jordi Ortizaa8de492016-12-01 00:21:36 +0100259 }
260 }
261}