blob: 617151d6a74e7e45a191a2824079f07c61147893 [file] [log] [blame]
alshabibe1248b62015-08-20 17:21:55 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabibe1248b62015-08-20 17:21:55 -07003 *
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.collect.ImmutableSet;
19import org.onosproject.core.ApplicationId;
Andrea Campanella5bdbe432021-05-03 15:59:19 +020020import org.onosproject.net.AbstractAnnotated;
21import org.onosproject.net.Annotations;
alshabibe1248b62015-08-20 17:21:55 -070022import org.onosproject.net.DeviceId;
23
24import java.util.Collection;
25import java.util.Optional;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * A default implementation of a meter.
33 */
Andrea Campanella5bdbe432021-05-03 15:59:19 +020034public final class DefaultMeterRequest extends AbstractAnnotated implements MeterRequest {
alshabibe1248b62015-08-20 17:21:55 -070035
alshabibe1248b62015-08-20 17:21:55 -070036 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 Shum79919522021-08-22 19:35:34 +080043 private final MeterScope scope;
44 private final Optional<Long> index;
alshabibe1248b62015-08-20 17:21:55 -070045
46 private DefaultMeterRequest(DeviceId deviceId, ApplicationId appId,
47 Meter.Unit unit, boolean burst,
alshabib70aaa1b2015-09-25 14:30:59 -070048 Collection<Band> bands, MeterContext context,
Wailok Shum79919522021-08-22 19:35:34 +080049 Type op, MeterScope scope, Optional<Long> index,
50 Annotations... annotations) {
Andrea Campanella5bdbe432021-05-03 15:59:19 +020051 super(annotations);
alshabibe1248b62015-08-20 17:21:55 -070052 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 Shum79919522021-08-22 19:35:34 +080059 this.scope = scope;
60 this.index = index;
alshabibe1248b62015-08-20 17:21:55 -070061 }
62
63 @Override
64 public DeviceId deviceId() {
65 return deviceId;
66 }
67
alshabib70aaa1b2015-09-25 14:30:59 -070068
alshabibe1248b62015-08-20 17:21:55 -070069 @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 Shum79919522021-08-22 19:35:34 +080094 @Override
95 public MeterScope scope() {
96 return scope;
97 }
alshabibe1248b62015-08-20 17:21:55 -070098
Wailok Shum79919522021-08-22 19:35:34 +080099 @Override
100 public Optional<Long> index() {
101 return index;
102 }
alshabibe1248b62015-08-20 17:21:55 -0700103
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 Campanella5bdbe432021-05-03 15:59:19 +0200115 .add("bands", bands)
Wailok Shum79919522021-08-22 19:35:34 +0800116 .add("scope", scope)
117 .add("desired index", index.orElse(null))
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200118 .add("annotations", annotations())
119 .toString();
alshabibe1248b62015-08-20 17:21:55 -0700120 }
121
122 public static final class Builder implements MeterRequest.Builder {
pierventrec0914ec2021-08-27 15:25:02 +0200123 // Relevant only for delete
124 private static final Band DUMMY_BAND = new DefaultBand(Band.Type.DROP, 0L, 0L, (short) 0);
alshabibe1248b62015-08-20 17:21:55 -0700125
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 Campanella5bdbe432021-05-03 15:59:19 +0200132 private Annotations annotations;
Wailok Shum79919522021-08-22 19:35:34 +0800133 private MeterScope scope = MeterScope.globalScope();
134 private Optional<Long> desiredIndex = Optional.empty();
alshabibe1248b62015-08-20 17:21:55 -0700135
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 Shum79919522021-08-22 19:35:34 +0800149 public MeterRequest.Builder withUnit(Meter.Unit unit) {
alshabibe1248b62015-08-20 17:21:55 -0700150 this.unit = unit;
151 return this;
152 }
153
154 @Override
Wailok Shum79919522021-08-22 19:35:34 +0800155 public MeterRequest.Builder burst() {
alshabibe1248b62015-08-20 17:21:55 -0700156 this.burst = true;
157 return this;
158 }
159
160 @Override
Wailok Shum79919522021-08-22 19:35:34 +0800161 public MeterRequest.Builder withBands(Collection<Band> bands) {
alshabibe1248b62015-08-20 17:21:55 -0700162 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 Campanella5bdbe432021-05-03 15:59:19 +0200173 public MeterRequest.Builder withAnnotations(Annotations annotations) {
174 this.annotations = annotations;
175 return this;
176 }
177
178 @Override
Wailok Shum79919522021-08-22 19:35:34 +0800179 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
alshabibe1248b62015-08-20 17:21:55 -0700191 public MeterRequest add() {
192 validate();
Wailok Shum79919522021-08-22 19:35:34 +0800193 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands, context,
194 Type.ADD, scope, desiredIndex, annotations);
alshabibe1248b62015-08-20 17:21:55 -0700195 }
196
197 @Override
198 public MeterRequest remove() {
pierventrec0914ec2021-08-27 15:25:02 +0200199 // Allow to create the removal request without specifying bands
200 if (bands == null || bands.isEmpty()) {
201 bands = ImmutableSet.of(DUMMY_BAND);
202 }
203
alshabibe1248b62015-08-20 17:21:55 -0700204 validate();
Wailok Shum79919522021-08-22 19:35:34 +0800205 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands, context,
206 Type.REMOVE, scope, desiredIndex, annotations);
alshabibe1248b62015-08-20 17:21:55 -0700207 }
208
209 private void validate() {
210 checkNotNull(deviceId, "Must specify a device");
211 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800212 checkArgument(!bands.isEmpty(), "Must have at least one band.");
alshabibe1248b62015-08-20 17:21:55 -0700213 checkNotNull(appId, "Must have an application id");
Wailok Shum79919522021-08-22 19:35:34 +0800214 if (desiredIndex.isPresent()) {
215 checkArgument(desiredIndex.get() >= 0, "Desired index cannot be negative");
216 }
alshabibe1248b62015-08-20 17:21:55 -0700217 }
218
219
220 }
221}