blob: b9a4368a0bdd01c4eb750b9f8e7d20f57abac9de [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 Shum90b988a2021-09-13 17:23:20 +080069 long maxMeter = 0;
70 if (startIndex != -1 && endIndex != -1) {
71 maxMeter = endIndex - startIndex + 1;
72 }
73 return maxMeter;
Wailok Shumf013a782021-07-26 16:51:01 +080074 }
75
76 @Override
77 public long startIndex() {
78 return startIndex;
79 }
80
81 @Override
82 public long endIndex() {
83 return endIndex;
Jordi Ortizaa8de492016-12-01 00:21:36 +010084 }
85
86 @Override
87 public Set<Band.Type> bandTypes() {
88 return bandTypes;
89 }
90
91 @Override
92 public Set<Meter.Unit> unitTypes() {
93 return units;
94 }
95
96 @Override
97 public boolean isBurstSupported() {
98 return burst;
99 }
100
101 @Override
102 public boolean isStatsSupported() {
103 return stats;
104 }
105
106 @Override
107 public short maxBands() {
108 return maxBands;
109 }
110
111 @Override
112 public short maxColor() {
113 return maxColor;
114 }
115
cansu.toprak409289d2017-10-27 10:04:05 +0300116 @Override
117 public Set<MeterFeaturesFlag> features() {
118 return features;
119 }
120
Wailok Shumf013a782021-07-26 16:51:01 +0800121 @Override
122 public MeterScope scope() {
123 return scope;
124 }
125
Jordi Ortizaa8de492016-12-01 00:21:36 +0100126 public static Builder builder() {
127 return new Builder();
128 }
129
130 public static MeterFeatures noMeterFeatures(DeviceId deviceId) {
131 return DefaultMeterFeatures.builder().forDevice(deviceId)
132 .build();
133 }
134
135 @Override
136 public String toString() {
137 return MoreObjects.toStringHelper(getClass())
138 .add("deviceId", deviceId())
Wailok Shumf013a782021-07-26 16:51:01 +0800139 .add("startIndex", startIndex())
140 .add("endIndex", endIndex())
Jordi Ortizaa8de492016-12-01 00:21:36 +0100141 .add("maxBands", maxBands())
142 .add("maxColor", maxColor())
143 .add("bands", bandTypes())
144 .add("burst", isBurstSupported())
145 .add("stats", isStatsSupported())
146 .add("units", unitTypes())
Wailok Shumf013a782021-07-26 16:51:01 +0800147 .add("scope", scope())
Jordi Ortizaa8de492016-12-01 00:21:36 +0100148 .toString();
149 }
150
151 /**
152 * A DefaultMeterFeatures builder.
153 */
154 public static final class Builder implements MeterFeatures.Builder {
155 private DeviceId did;
156 private long mmeter = 0L;
Wailok Shumf013a782021-07-26 16:51:01 +0800157 private long starti = -1L;
158 private long endi = -1L;
Jordi Ortizaa8de492016-12-01 00:21:36 +0100159 private short mbands = 0;
160 private short mcolors = 0;
161 private Set<Band.Type> bandTypes = new HashSet<>();
162 private Set<Meter.Unit> units1 = new HashSet<>();
163 private boolean burst = false;
164 private boolean stats = false;
cansu.toprak409289d2017-10-27 10:04:05 +0300165 private Set<MeterFeaturesFlag> features = Sets.newHashSet();
Wailok Shumf013a782021-07-26 16:51:01 +0800166 private MeterScope mscope = MeterScope.globalScope();
Jordi Ortizaa8de492016-12-01 00:21:36 +0100167
168 @Override
169 public MeterFeatures.Builder forDevice(DeviceId deviceId) {
170 did = deviceId;
171 return this;
172 }
173
174 @Override
175 public MeterFeatures.Builder withMaxMeters(long maxMeter) {
176 mmeter = maxMeter;
177 return this;
178 }
179
180 @Override
Wailok Shumf013a782021-07-26 16:51:01 +0800181 public MeterFeatures.Builder withStartIndex(long startIndex) {
182 starti = startIndex;
183 return this;
184 }
185
186 @Override
187 public MeterFeatures.Builder withEndIndex(long endIndex) {
188 endi = endIndex;
189 return this;
190 }
191
192 @Override
Jordi Ortizaa8de492016-12-01 00:21:36 +0100193 public MeterFeatures.Builder withMaxBands(short maxBands) {
194 mbands = maxBands;
195 return this;
196 }
197
198 @Override
199 public MeterFeatures.Builder withMaxColors(short maxColors) {
200 mcolors = maxColors;
201 return this;
202 }
203
204 @Override
205 public MeterFeatures.Builder withBandTypes(Set<Band.Type> types) {
206 bandTypes = types;
207 return this;
208 }
209
210 @Override
211 public MeterFeatures.Builder withUnits(Set<Meter.Unit> units) {
212 units1 = units;
213 return this;
214 }
215
216 @Override
217 public MeterFeatures.Builder hasBurst(boolean hasBurst) {
218 burst = hasBurst;
219 return this;
220 }
221
222 @Override
223 public MeterFeatures.Builder hasStats(boolean hasStats) {
224 stats = hasStats;
225 return this;
226 }
227
228 @Override
cansu.toprak409289d2017-10-27 10:04:05 +0300229 public MeterFeatures.Builder withFeatures(Set<MeterFeaturesFlag> featureFlags) {
230 features = featureFlags;
231 return this;
232 }
233
234 @Override
Wailok Shumf013a782021-07-26 16:51:01 +0800235 public MeterFeatures.Builder withScope(MeterScope scope) {
236 mscope = scope;
237 return this;
238 }
239
240 @Override
Jordi Ortizaa8de492016-12-01 00:21:36 +0100241 public MeterFeatures build() {
Wailok Shumf013a782021-07-26 16:51:01 +0800242 // In case some functions are using maxMeter
243 // and both indexes are not set
244 // Start index will be
245 // 1, if it is global scope (An OpenFlow meter)
246 // 0, for the rest (A P4RT meter)
247 if (mmeter != 0L && starti == -1L && endi == -1L) {
248 starti = mscope.isGlobal() ? 1 : 0;
Wailok Shum90b988a2021-09-13 17:23:20 +0800249 endi = mscope.isGlobal() ? mmeter : mmeter - 1;
Wailok Shumf013a782021-07-26 16:51:01 +0800250 }
251 // If one of the index is unset/unvalid value, treated as no meter features
252 if (starti <= -1 || endi <= -1) {
253 starti = -1;
254 endi = -1;
255 }
256
Jordi Ortizaa8de492016-12-01 00:21:36 +0100257 checkNotNull(did, "Must specify a device");
Wailok Shumf013a782021-07-26 16:51:01 +0800258 checkArgument(starti <= endi, "Start index must be less than or equal to end index");
259
260 return new DefaultMeterFeatures(did, starti, endi, bandTypes, units1, burst,
261 stats, mbands, mcolors, features, mscope);
Jordi Ortizaa8de492016-12-01 00:21:36 +0100262 }
263 }
264}