alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [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.collect.ImmutableSet; |
| 19 | import org.onosproject.core.ApplicationId; |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 20 | import org.onosproject.net.AbstractAnnotated; |
| 21 | import org.onosproject.net.Annotations; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.Optional; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * A default implementation of a meter. |
| 33 | */ |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 34 | public final class DefaultMeterRequest extends AbstractAnnotated implements MeterRequest { |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 35 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 36 | private final ApplicationId appId; |
| 37 | private final Meter.Unit unit; |
| 38 | private final boolean burst; |
| 39 | private final Collection<Band> bands; |
| 40 | private final DeviceId deviceId; |
| 41 | private final Optional<MeterContext> context; |
| 42 | private final Type op; |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 43 | private final MeterScope scope; |
| 44 | private final Optional<Long> index; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 45 | |
| 46 | private DefaultMeterRequest(DeviceId deviceId, ApplicationId appId, |
| 47 | Meter.Unit unit, boolean burst, |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 48 | Collection<Band> bands, MeterContext context, |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 49 | Type op, MeterScope scope, Optional<Long> index, |
| 50 | Annotations... annotations) { |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 51 | super(annotations); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 52 | this.deviceId = deviceId; |
| 53 | this.appId = appId; |
| 54 | this.unit = unit; |
| 55 | this.burst = burst; |
| 56 | this.bands = bands; |
| 57 | this.context = Optional.ofNullable(context); |
| 58 | this.op = op; |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 59 | this.scope = scope; |
| 60 | this.index = index; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public DeviceId deviceId() { |
| 65 | return deviceId; |
| 66 | } |
| 67 | |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 68 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public ApplicationId appId() { |
| 71 | return appId; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Meter.Unit unit() { |
| 76 | return unit; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean isBurst() { |
| 81 | return burst; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Collection<Band> bands() { |
| 86 | return bands; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Optional<MeterContext> context() { |
| 91 | return context; |
| 92 | } |
| 93 | |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 94 | @Override |
| 95 | public MeterScope scope() { |
| 96 | return scope; |
| 97 | } |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 98 | |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 99 | @Override |
| 100 | public Optional<Long> index() { |
| 101 | return index; |
| 102 | } |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 103 | |
| 104 | public static Builder builder() { |
| 105 | return new Builder(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return toStringHelper(this) |
| 111 | .add("device", deviceId) |
| 112 | .add("appId", appId.name()) |
| 113 | .add("unit", unit) |
| 114 | .add("isBurst", burst) |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 115 | .add("bands", bands) |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 116 | .add("scope", scope) |
| 117 | .add("desired index", index.orElse(null)) |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 118 | .add("annotations", annotations()) |
| 119 | .toString(); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | public static final class Builder implements MeterRequest.Builder { |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 123 | // Relevant only for delete |
| 124 | private static final Band DUMMY_BAND = new DefaultBand(Band.Type.DROP, 0L, 0L, (short) 0); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 125 | |
| 126 | private ApplicationId appId; |
| 127 | private Meter.Unit unit = Meter.Unit.KB_PER_SEC; |
| 128 | private boolean burst = false; |
| 129 | private Collection<Band> bands; |
| 130 | private DeviceId deviceId; |
| 131 | private MeterContext context; |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 132 | private Annotations annotations; |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 133 | private MeterScope scope = MeterScope.globalScope(); |
| 134 | private Optional<Long> desiredIndex = Optional.empty(); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 135 | |
| 136 | @Override |
| 137 | public MeterRequest.Builder forDevice(DeviceId deviceId) { |
| 138 | this.deviceId = deviceId; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public MeterRequest.Builder fromApp(ApplicationId appId) { |
| 144 | this.appId = appId; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 149 | public MeterRequest.Builder withUnit(Meter.Unit unit) { |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 150 | this.unit = unit; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 155 | public MeterRequest.Builder burst() { |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 156 | this.burst = true; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Override |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 161 | public MeterRequest.Builder withBands(Collection<Band> bands) { |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 162 | this.bands = ImmutableSet.copyOf(bands); |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public MeterRequest.Builder withContext(MeterContext context) { |
| 168 | this.context = context; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | @Override |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 173 | public MeterRequest.Builder withAnnotations(Annotations annotations) { |
| 174 | this.annotations = annotations; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | @Override |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 179 | public MeterRequest.Builder withScope(MeterScope scope) { |
| 180 | this.scope = scope; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public MeterRequest.Builder withIndex(Long index) { |
| 186 | this.desiredIndex = Optional.ofNullable(index); |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | @Override |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 191 | public MeterRequest add() { |
| 192 | validate(); |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 193 | return new DefaultMeterRequest(deviceId, appId, unit, burst, bands, context, |
| 194 | Type.ADD, scope, desiredIndex, annotations); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public MeterRequest remove() { |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 199 | // Allow to create the removal request without specifying bands |
| 200 | if (bands == null || bands.isEmpty()) { |
| 201 | bands = ImmutableSet.of(DUMMY_BAND); |
| 202 | } |
| 203 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 204 | validate(); |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 205 | return new DefaultMeterRequest(deviceId, appId, unit, burst, bands, context, |
| 206 | Type.REMOVE, scope, desiredIndex, annotations); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private void validate() { |
| 210 | checkNotNull(deviceId, "Must specify a device"); |
| 211 | checkNotNull(bands, "Must have bands."); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 212 | checkArgument(!bands.isEmpty(), "Must have at least one band."); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 213 | checkNotNull(appId, "Must have an application id"); |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 214 | if (desiredIndex.isPresent()) { |
| 215 | checkArgument(desiredIndex.get() >= 0, "Desired index cannot be negative"); |
| 216 | } |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | } |
| 221 | } |