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