blob: 0dbd4f7a6087a522079701d4fde56a2e1f94de0b [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
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 Shum79919522021-08-22 19:35:34 +080045 private final MeterScope scope;
46 private final Optional<Long> index;
alshabibe1248b62015-08-20 17:21:55 -070047
48 private DefaultMeterRequest(DeviceId deviceId, ApplicationId appId,
49 Meter.Unit unit, boolean burst,
alshabib70aaa1b2015-09-25 14:30:59 -070050 Collection<Band> bands, MeterContext context,
Wailok Shum79919522021-08-22 19:35:34 +080051 Type op, MeterScope scope, Optional<Long> index,
52 Annotations... annotations) {
Andrea Campanella5bdbe432021-05-03 15:59:19 +020053 super(annotations);
alshabibe1248b62015-08-20 17:21:55 -070054 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 Shum79919522021-08-22 19:35:34 +080061 this.scope = scope;
62 this.index = index;
alshabibe1248b62015-08-20 17:21:55 -070063 }
64
65 @Override
66 public DeviceId deviceId() {
67 return deviceId;
68 }
69
alshabib70aaa1b2015-09-25 14:30:59 -070070
alshabibe1248b62015-08-20 17:21:55 -070071 @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 Shum79919522021-08-22 19:35:34 +080096 @Override
97 public MeterScope scope() {
98 return scope;
99 }
alshabibe1248b62015-08-20 17:21:55 -0700100
Wailok Shum79919522021-08-22 19:35:34 +0800101 @Override
102 public Optional<Long> index() {
103 return index;
104 }
alshabibe1248b62015-08-20 17:21:55 -0700105
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 Campanella5bdbe432021-05-03 15:59:19 +0200117 .add("bands", bands)
Wailok Shum79919522021-08-22 19:35:34 +0800118 .add("scope", scope)
119 .add("desired index", index.orElse(null))
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200120 .add("annotations", annotations())
121 .toString();
alshabibe1248b62015-08-20 17:21:55 -0700122 }
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 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() {
199 validate();
Wailok Shum79919522021-08-22 19:35:34 +0800200 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands, context,
201 Type.REMOVE, scope, desiredIndex, annotations);
alshabibe1248b62015-08-20 17:21:55 -0700202 }
203
204 private void validate() {
205 checkNotNull(deviceId, "Must specify a device");
206 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800207 checkArgument(!bands.isEmpty(), "Must have at least one band.");
alshabibe1248b62015-08-20 17:21:55 -0700208 checkNotNull(appId, "Must have an application id");
Wailok Shum79919522021-08-22 19:35:34 +0800209 if (desiredIndex.isPresent()) {
210 checkArgument(desiredIndex.get() >= 0, "Desired index cannot be negative");
211 }
alshabibe1248b62015-08-20 17:21:55 -0700212 }
213
214
215 }
216}