alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 16 | package org.onosproject.net.meter; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 17 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | |
| 22 | import java.util.Collection; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 23 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame^] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * A default implementation of a meter. |
| 30 | */ |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 31 | public final class DefaultMeter implements Meter, MeterEntry { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 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; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 40 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 41 | private MeterState state; |
| 42 | private long life; |
| 43 | private long refCount; |
| 44 | private long packets; |
| 45 | private long bytes; |
| 46 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 47 | private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId, |
| 48 | Unit unit, boolean burst, |
alshabib | eadfc8e | 2015-08-18 15:40:46 -0700 | [diff] [blame] | 49 | Collection<Band> bands) { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 50 | this.deviceId = deviceId; |
| 51 | this.id = id; |
| 52 | this.appId = appId; |
| 53 | this.unit = unit; |
| 54 | this.burst = burst; |
| 55 | this.bands = bands; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public DeviceId deviceId() { |
| 60 | return deviceId; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public MeterId id() { |
| 65 | return id; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public ApplicationId appId() { |
| 70 | return appId; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Unit unit() { |
| 75 | return unit; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean isBurst() { |
| 80 | return burst; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Collection<Band> bands() { |
| 85 | return bands; |
| 86 | } |
| 87 | |
| 88 | @Override |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 89 | public MeterState state() { |
| 90 | return state; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public long life() { |
| 95 | return life; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public long referenceCount() { |
| 100 | return refCount; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public long packetsSeen() { |
| 105 | return packets; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public long bytesSeen() { |
| 110 | return bytes; |
| 111 | } |
| 112 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 113 | public static Builder builder() { |
| 114 | return new Builder(); |
| 115 | } |
| 116 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public void setState(MeterState state) { |
| 119 | this.state = state; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void setLife(long life) { |
| 124 | this.life = life; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setReferenceCount(long count) { |
| 129 | this.refCount = count; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void setProcessedPackets(long packets) { |
| 134 | this.packets = packets; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setProcessedBytes(long bytes) { |
| 139 | this.bytes = bytes; |
| 140 | } |
| 141 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame^] | 142 | @Override |
| 143 | public String toString() { |
| 144 | return toStringHelper(this) |
| 145 | .add("device", deviceId) |
| 146 | .add("id", id) |
| 147 | .add("appId", appId.name()) |
| 148 | .add("unit", unit) |
| 149 | .add("isBurst", burst) |
| 150 | .add("state", state) |
| 151 | .add("bands", bands).toString(); |
| 152 | } |
| 153 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 154 | public static final class Builder implements Meter.Builder { |
| 155 | |
| 156 | private MeterId id; |
| 157 | private ApplicationId appId; |
| 158 | private Unit unit = Unit.KB_PER_SEC; |
| 159 | private boolean burst = false; |
| 160 | private Collection<Band> bands; |
| 161 | private DeviceId deviceId; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 162 | |
| 163 | |
| 164 | @Override |
| 165 | public Meter.Builder forDevice(DeviceId deviceId) { |
| 166 | this.deviceId = deviceId; |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Override |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame^] | 171 | public Meter.Builder withId(MeterId id) { |
| 172 | this.id = id; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 173 | return this; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Meter.Builder fromApp(ApplicationId appId) { |
| 178 | this.appId = appId; |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public Meter.Builder withUnit(Unit unit) { |
| 184 | this.unit = unit; |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public Meter.Builder burst() { |
| 190 | this.burst = true; |
| 191 | return this; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Meter.Builder withBands(Collection<Band> bands) { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame^] | 196 | this.bands = ImmutableSet.copyOf(bands); |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 197 | return this; |
| 198 | } |
| 199 | |
| 200 | @Override |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 201 | public DefaultMeter build() { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 202 | checkNotNull(deviceId, "Must specify a device"); |
| 203 | checkNotNull(bands, "Must have bands."); |
| 204 | checkArgument(bands.size() > 0, "Must have at least one band."); |
| 205 | checkNotNull(appId, "Must have an application id"); |
| 206 | checkNotNull(id, "Must specify a meter id"); |
alshabib | eadfc8e | 2015-08-18 15:40:46 -0700 | [diff] [blame] | 207 | return new DefaultMeter(deviceId, id, appId, unit, burst, bands); |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | } |
| 212 | } |