alshabib | e1248b6 | 2015-08-20 17:21:55 -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.net.meter; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Optional; |
| 24 | |
| 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * A default implementation of a meter. |
| 31 | */ |
| 32 | public 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, |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 46 | Collection<Band> bands, MeterContext context, |
| 47 | Type op) { |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 48 | 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 | |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 62 | |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 63 | @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; |
alshabib | 70aaa1b | 2015-09-25 14:30:59 -0700 | [diff] [blame] | 112 | private Optional<MeterId> desiredId = Optional.empty(); |
alshabib | e1248b6 | 2015-08-20 17:21:55 -0700 | [diff] [blame] | 113 | |
| 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."); |
| 168 | checkArgument(bands.size() > 0, "Must have at least one band."); |
| 169 | checkNotNull(appId, "Must have an application id"); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | } |
| 174 | } |