blob: e6e2d1be79f048aa3f66082af669a913b7fc12fa [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;
45
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,
Andrea Campanella5bdbe432021-05-03 15:59:19 +020049 Type op, Annotations... annotations) {
50 super(annotations);
alshabibe1248b62015-08-20 17:21:55 -070051 this.deviceId = deviceId;
52 this.appId = appId;
53 this.unit = unit;
54 this.burst = burst;
55 this.bands = bands;
56 this.context = Optional.ofNullable(context);
57 this.op = op;
58 }
59
60 @Override
61 public DeviceId deviceId() {
62 return deviceId;
63 }
64
alshabib70aaa1b2015-09-25 14:30:59 -070065
alshabibe1248b62015-08-20 17:21:55 -070066 @Override
67 public ApplicationId appId() {
68 return appId;
69 }
70
71 @Override
72 public Meter.Unit unit() {
73 return unit;
74 }
75
76 @Override
77 public boolean isBurst() {
78 return burst;
79 }
80
81 @Override
82 public Collection<Band> bands() {
83 return bands;
84 }
85
86 @Override
87 public Optional<MeterContext> context() {
88 return context;
89 }
90
91
92
93 public static Builder builder() {
94 return new Builder();
95 }
96
97 @Override
98 public String toString() {
99 return toStringHelper(this)
100 .add("device", deviceId)
101 .add("appId", appId.name())
102 .add("unit", unit)
103 .add("isBurst", burst)
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200104 .add("bands", bands)
105 .add("annotations", annotations())
106 .toString();
alshabibe1248b62015-08-20 17:21:55 -0700107 }
108
109 public static final class Builder implements MeterRequest.Builder {
110
111 private ApplicationId appId;
112 private Meter.Unit unit = Meter.Unit.KB_PER_SEC;
113 private boolean burst = false;
114 private Collection<Band> bands;
115 private DeviceId deviceId;
116 private MeterContext context;
alshabib70aaa1b2015-09-25 14:30:59 -0700117 private Optional<MeterId> desiredId = Optional.empty();
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200118 private Annotations annotations;
alshabibe1248b62015-08-20 17:21:55 -0700119
120
121 @Override
122 public MeterRequest.Builder forDevice(DeviceId deviceId) {
123 this.deviceId = deviceId;
124 return this;
125 }
126
127 @Override
128 public MeterRequest.Builder fromApp(ApplicationId appId) {
129 this.appId = appId;
130 return this;
131 }
132
133 @Override
134 public MeterRequest.Builder withUnit(Meter.Unit unit) {
135 this.unit = unit;
136 return this;
137 }
138
139 @Override
140 public MeterRequest.Builder burst() {
141 this.burst = true;
142 return this;
143 }
144
145 @Override
146 public MeterRequest.Builder withBands(Collection<Band> bands) {
147 this.bands = ImmutableSet.copyOf(bands);
148 return this;
149 }
150
151 @Override
152 public MeterRequest.Builder withContext(MeterContext context) {
153 this.context = context;
154 return this;
155 }
156
157 @Override
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200158 public MeterRequest.Builder withAnnotations(Annotations annotations) {
159 this.annotations = annotations;
160 return this;
161 }
162
163 @Override
alshabibe1248b62015-08-20 17:21:55 -0700164 public MeterRequest add() {
165 validate();
166 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands,
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200167 context, Type.ADD, annotations);
alshabibe1248b62015-08-20 17:21:55 -0700168 }
169
170 @Override
171 public MeterRequest remove() {
172 validate();
173 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands,
Andrea Campanella5bdbe432021-05-03 15:59:19 +0200174 context, Type.REMOVE, annotations);
alshabibe1248b62015-08-20 17:21:55 -0700175 }
176
177 private void validate() {
178 checkNotNull(deviceId, "Must specify a device");
179 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800180 checkArgument(!bands.isEmpty(), "Must have at least one band.");
alshabibe1248b62015-08-20 17:21:55 -0700181 checkNotNull(appId, "Must have an application id");
182 }
183
184
185 }
186}