blob: 94cada47b98d1d30c8f6ff9f98c82a338eb6e67f [file] [log] [blame]
alshabibe1248b62015-08-20 17:21:55 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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,
46 Collection<Band> bands, MeterContext context, Type op) {
47 this.deviceId = deviceId;
48 this.appId = appId;
49 this.unit = unit;
50 this.burst = burst;
51 this.bands = bands;
52 this.context = Optional.ofNullable(context);
53 this.op = op;
54 }
55
56 @Override
57 public DeviceId deviceId() {
58 return deviceId;
59 }
60
61 @Override
62 public ApplicationId appId() {
63 return appId;
64 }
65
66 @Override
67 public Meter.Unit unit() {
68 return unit;
69 }
70
71 @Override
72 public boolean isBurst() {
73 return burst;
74 }
75
76 @Override
77 public Collection<Band> bands() {
78 return bands;
79 }
80
81 @Override
82 public Optional<MeterContext> context() {
83 return context;
84 }
85
86
87
88 public static Builder builder() {
89 return new Builder();
90 }
91
92 @Override
93 public String toString() {
94 return toStringHelper(this)
95 .add("device", deviceId)
96 .add("appId", appId.name())
97 .add("unit", unit)
98 .add("isBurst", burst)
99 .add("bands", bands).toString();
100 }
101
102 public static final class Builder implements MeterRequest.Builder {
103
104 private ApplicationId appId;
105 private Meter.Unit unit = Meter.Unit.KB_PER_SEC;
106 private boolean burst = false;
107 private Collection<Band> bands;
108 private DeviceId deviceId;
109 private MeterContext context;
110
111
112 @Override
113 public MeterRequest.Builder forDevice(DeviceId deviceId) {
114 this.deviceId = deviceId;
115 return this;
116 }
117
118 @Override
119 public MeterRequest.Builder fromApp(ApplicationId appId) {
120 this.appId = appId;
121 return this;
122 }
123
124 @Override
125 public MeterRequest.Builder withUnit(Meter.Unit unit) {
126 this.unit = unit;
127 return this;
128 }
129
130 @Override
131 public MeterRequest.Builder burst() {
132 this.burst = true;
133 return this;
134 }
135
136 @Override
137 public MeterRequest.Builder withBands(Collection<Band> bands) {
138 this.bands = ImmutableSet.copyOf(bands);
139 return this;
140 }
141
142 @Override
143 public MeterRequest.Builder withContext(MeterContext context) {
144 this.context = context;
145 return this;
146 }
147
148 @Override
149 public MeterRequest add() {
150 validate();
151 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands,
152 context, Type.ADD);
153 }
154
155 @Override
156 public MeterRequest remove() {
157 validate();
158 return new DefaultMeterRequest(deviceId, appId, unit, burst, bands,
159 context, Type.REMOVE);
160 }
161
162 private void validate() {
163 checkNotNull(deviceId, "Must specify a device");
164 checkNotNull(bands, "Must have bands.");
165 checkArgument(bands.size() > 0, "Must have at least one band.");
166 checkNotNull(appId, "Must have an application id");
167 }
168
169
170 }
171}