blob: 1ca446691705ad19b21a0822df21a90744abbba1 [file] [log] [blame]
alshabibe1248b62015-08-20 17:21:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
20import org.onosproject.net.DeviceId;
21
22import java.util.Collection;
23import java.util.Optional;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * A default implementation of a meter.
31 */
32public final class DefaultMeterRequest implements MeterRequest {
33
34
35
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;
43
44 private DefaultMeterRequest(DeviceId deviceId, ApplicationId appId,
45 Meter.Unit unit, boolean burst,
alshabib70aaa1b2015-09-25 14:30:59 -070046 Collection<Band> bands, MeterContext context,
47 Type op) {
alshabibe1248b62015-08-20 17:21:55 -070048 this.deviceId = deviceId;
49 this.appId = appId;
50 this.unit = unit;
51 this.burst = burst;
52 this.bands = bands;
53 this.context = Optional.ofNullable(context);
54 this.op = op;
55 }
56
57 @Override
58 public DeviceId deviceId() {
59 return deviceId;
60 }
61
alshabib70aaa1b2015-09-25 14:30:59 -070062
alshabibe1248b62015-08-20 17:21:55 -070063 @Override
64 public ApplicationId appId() {
65 return appId;
66 }
67
68 @Override
69 public Meter.Unit unit() {
70 return unit;
71 }
72
73 @Override
74 public boolean isBurst() {
75 return burst;
76 }
77
78 @Override
79 public Collection<Band> bands() {
80 return bands;
81 }
82
83 @Override
84 public Optional<MeterContext> context() {
85 return context;
86 }
87
88
89
90 public static Builder builder() {
91 return new Builder();
92 }
93
94 @Override
95 public String toString() {
96 return toStringHelper(this)
97 .add("device", deviceId)
98 .add("appId", appId.name())
99 .add("unit", unit)
100 .add("isBurst", burst)
101 .add("bands", bands).toString();
102 }
103
104 public static final class Builder implements MeterRequest.Builder {
105
106 private ApplicationId appId;
107 private Meter.Unit unit = Meter.Unit.KB_PER_SEC;
108 private boolean burst = false;
109 private Collection<Band> bands;
110 private DeviceId deviceId;
111 private MeterContext context;
alshabib70aaa1b2015-09-25 14:30:59 -0700112 private Optional<MeterId> desiredId = Optional.empty();
alshabibe1248b62015-08-20 17:21:55 -0700113
114
115 @Override
116 public MeterRequest.Builder forDevice(DeviceId deviceId) {
117 this.deviceId = deviceId;
118 return this;
119 }
120
121 @Override
122 public MeterRequest.Builder fromApp(ApplicationId appId) {
123 this.appId = appId;
124 return this;
125 }
126
127 @Override
128 public MeterRequest.Builder withUnit(Meter.Unit unit) {
129 this.unit = unit;
130 return this;
131 }
132
133 @Override
134 public MeterRequest.Builder burst() {
135 this.burst = true;
136 return this;
137 }
138
139 @Override
140 public MeterRequest.Builder withBands(Collection<Band> bands) {
141 this.bands = ImmutableSet.copyOf(bands);
142 return this;
143 }
144
145 @Override
146 public MeterRequest.Builder withContext(MeterContext context) {
147 this.context = context;
148 return this;
149 }
150
151 @Override
152 public MeterRequest add() {
153 validate();
154 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands,
155 context, Type.ADD);
156 }
157
158 @Override
159 public MeterRequest remove() {
160 validate();
161 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands,
162 context, Type.REMOVE);
163 }
164
165 private void validate() {
166 checkNotNull(deviceId, "Must specify a device");
167 checkNotNull(bands, "Must have bands.");
Jon Hallcbd1b392017-01-18 20:15:44 -0800168 checkArgument(!bands.isEmpty(), "Must have at least one band.");
alshabibe1248b62015-08-20 17:21:55 -0700169 checkNotNull(appId, "Must have an application id");
170 }
171
172
173 }
174}