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 | |
| 36 | |
| 37 | |
| 38 | private final ApplicationId appId; |
| 39 | private final Meter.Unit unit; |
| 40 | private final boolean burst; |
| 41 | private final Collection<Band> bands; |
| 42 | private final DeviceId deviceId; |
| 43 | private final Optional<MeterContext> context; |
| 44 | private final Type op; |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 45 | private final MeterScope scope; |
| 46 | private final Optional<Long> index; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 47 | |
| 48 | private DefaultMeterRequest(DeviceId deviceId, ApplicationId appId, |
| 49 | Meter.Unit unit, boolean burst, |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 50 | Collection<Band> bands, MeterContext context, |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 51 | Type op, MeterScope scope, Optional<Long> index, |
| 52 | Annotations... annotations) { |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 53 | super(annotations); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 54 | this.deviceId = deviceId; |
| 55 | this.appId = appId; |
| 56 | this.unit = unit; |
| 57 | this.burst = burst; |
| 58 | this.bands = bands; |
| 59 | this.context = Optional.ofNullable(context); |
| 60 | this.op = op; |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 61 | this.scope = scope; |
| 62 | this.index = index; |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public DeviceId deviceId() { |
| 67 | return deviceId; |
| 68 | } |
| 69 | |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 70 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public ApplicationId appId() { |
| 73 | return appId; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Meter.Unit unit() { |
| 78 | return unit; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean isBurst() { |
| 83 | return burst; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Collection<Band> bands() { |
| 88 | return bands; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Optional<MeterContext> context() { |
| 93 | return context; |
| 94 | } |
| 95 | |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 96 | @Override |
| 97 | public MeterScope scope() { |
| 98 | return scope; |
| 99 | } |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 100 | |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 101 | @Override |
| 102 | public Optional<Long> index() { |
| 103 | return index; |
| 104 | } |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 105 | |
| 106 | public static Builder builder() { |
| 107 | return new Builder(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return toStringHelper(this) |
| 113 | .add("device", deviceId) |
| 114 | .add("appId", appId.name()) |
| 115 | .add("unit", unit) |
| 116 | .add("isBurst", burst) |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 117 | .add("bands", bands) |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 118 | .add("scope", scope) |
| 119 | .add("desired index", index.orElse(null)) |
Andrea Campanella | 5bdbe43 | 2021-05-03 15:59:19 +0200 | [diff] [blame] | 120 | .add("annotations", annotations()) |
| 121 | .toString(); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public static final class Builder implements MeterRequest.Builder { |
| 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() { |
| 199 | validate(); |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 200 | return new DefaultMeterRequest(deviceId, appId, unit, burst, bands, context, |
| 201 | Type.REMOVE, scope, desiredIndex, annotations); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | private void validate() { |
| 205 | checkNotNull(deviceId, "Must specify a device"); |
| 206 | checkNotNull(bands, "Must have bands."); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 207 | checkArgument(!bands.isEmpty(), "Must have at least one band."); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 208 | checkNotNull(appId, "Must have an application id"); |
Wailok Shum | 7991952 | 2021-08-22 19:35:34 +0800 | [diff] [blame] | 209 | if (desiredIndex.isPresent()) { |
| 210 | checkArgument(desiredIndex.get() >= 0, "Desired index cannot be negative"); |
| 211 | } |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | } |
| 216 | } |