Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 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 | package org.onosproject.net.meter; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
| 24 | |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Default implementation of MeterFeatures. |
| 30 | */ |
| 31 | public final class DefaultMeterFeatures implements MeterFeatures { |
| 32 | private DeviceId deviceId; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 33 | private long startIndex; |
| 34 | private long endIndex; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 35 | 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.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 41 | private Set<MeterFeaturesFlag> features; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 42 | private MeterScope scope; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 43 | |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 44 | private DefaultMeterFeatures(DeviceId did, long startIndex, long endIndex, |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 45 | Set<Band.Type> bandTypes, Set<Meter.Unit> units, |
| 46 | boolean burst, boolean stats, |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 47 | short maxBands, short maxColor, Set<MeterFeaturesFlag> flag, |
| 48 | MeterScope scope) { |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 49 | this.deviceId = did; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 50 | this.startIndex = startIndex; |
| 51 | this.endIndex = endIndex; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 52 | this.bandTypes = bandTypes; |
| 53 | this.burst = burst; |
| 54 | this.stats = stats; |
| 55 | this.units = units; |
| 56 | this.maxBands = maxBands; |
| 57 | this.maxColor = maxColor; |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 58 | this.features = flag; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 59 | this.scope = scope; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public DeviceId deviceId() { |
| 64 | return deviceId; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public long maxMeter() { |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 69 | // 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 Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 81 | } |
| 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.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 113 | @Override |
| 114 | public Set<MeterFeaturesFlag> features() { |
| 115 | return features; |
| 116 | } |
| 117 | |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 118 | @Override |
| 119 | public MeterScope scope() { |
| 120 | return scope; |
| 121 | } |
| 122 | |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 123 | 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 Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 136 | .add("startIndex", startIndex()) |
| 137 | .add("endIndex", endIndex()) |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 138 | .add("maxBands", maxBands()) |
| 139 | .add("maxColor", maxColor()) |
| 140 | .add("bands", bandTypes()) |
| 141 | .add("burst", isBurstSupported()) |
| 142 | .add("stats", isStatsSupported()) |
| 143 | .add("units", unitTypes()) |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 144 | .add("scope", scope()) |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 145 | .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 Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 154 | private long starti = -1L; |
| 155 | private long endi = -1L; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 156 | 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.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 162 | private Set<MeterFeaturesFlag> features = Sets.newHashSet(); |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 163 | private MeterScope mscope = MeterScope.globalScope(); |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 164 | |
| 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 Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 178 | 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 Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 190 | 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.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 226 | public MeterFeatures.Builder withFeatures(Set<MeterFeaturesFlag> featureFlags) { |
| 227 | features = featureFlags; |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | @Override |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 232 | public MeterFeatures.Builder withScope(MeterScope scope) { |
| 233 | mscope = scope; |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | @Override |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 238 | public MeterFeatures build() { |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 239 | // 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 Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 254 | checkNotNull(did, "Must specify a device"); |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame] | 255 | 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 Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |