blob: 953feb2a6fcc0c637fdd4295beb2a2a1a9df7e40 [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -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.incubator.net.meter;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.net.DeviceId;
20
21import java.util.Collection;
22import java.util.Collections;
23import java.util.Optional;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * A default implementation of a meter.
30 */
31public final class DefaultMeter implements Meter {
32
33
34 private final MeterId id;
35 private final ApplicationId appId;
36 private final Unit unit;
37 private final boolean burst;
38 private final Collection<Band> bands;
39 private final DeviceId deviceId;
40 private final Optional<MeterContext> context;
41
42 private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId,
43 Unit unit, boolean burst,
44 Collection<Band> bands, Optional<MeterContext> context) {
45 this.deviceId = deviceId;
46 this.id = id;
47 this.appId = appId;
48 this.unit = unit;
49 this.burst = burst;
50 this.bands = bands;
51 this.context = context;
52 }
53
54 @Override
55 public DeviceId deviceId() {
56 return deviceId;
57 }
58
59 @Override
60 public MeterId id() {
61 return id;
62 }
63
64 @Override
65 public ApplicationId appId() {
66 return appId;
67 }
68
69 @Override
70 public Unit unit() {
71 return unit;
72 }
73
74 @Override
75 public boolean isBurst() {
76 return burst;
77 }
78
79 @Override
80 public Collection<Band> bands() {
81 return bands;
82 }
83
84 @Override
85 public Optional<MeterContext> context() {
86 return null;
87 }
88
89 public static Builder builder() {
90 return new Builder();
91 }
92
93 public static final class Builder implements Meter.Builder {
94
95 private MeterId id;
96 private ApplicationId appId;
97 private Unit unit = Unit.KB_PER_SEC;
98 private boolean burst = false;
99 private Collection<Band> bands;
100 private DeviceId deviceId;
101 private Optional<MeterContext> context;
102
103
104 @Override
105 public Meter.Builder forDevice(DeviceId deviceId) {
106 this.deviceId = deviceId;
107 return this;
108 }
109
110 @Override
111 public Meter.Builder withId(int id) {
112 this.id = MeterId.meterId(id);
113 return this;
114 }
115
116 @Override
117 public Meter.Builder fromApp(ApplicationId appId) {
118 this.appId = appId;
119 return this;
120 }
121
122 @Override
123 public Meter.Builder withUnit(Unit unit) {
124 this.unit = unit;
125 return this;
126 }
127
128 @Override
129 public Meter.Builder burst() {
130 this.burst = true;
131 return this;
132 }
133
134 @Override
135 public Meter.Builder withBands(Collection<Band> bands) {
136 this.bands = Collections.unmodifiableCollection(bands);
137 return this;
138 }
139
140 @Override
141 public Meter.Builder withContext(MeterContext context) {
142 this.context = Optional.<MeterContext>ofNullable(context);
143 return this;
144 }
145
146 @Override
147 public Meter build() {
148 checkNotNull(deviceId, "Must specify a device");
149 checkNotNull(bands, "Must have bands.");
150 checkArgument(bands.size() > 0, "Must have at least one band.");
151 checkNotNull(appId, "Must have an application id");
152 checkNotNull(id, "Must specify a meter id");
153 return new DefaultMeter(deviceId, id, appId, unit, burst, bands, context);
154 }
155
156
157 }
158}