alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 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 | |
Daniele Moro | 909b958 | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
| 19 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkArgument; |
Jordi Ortiz | af75c13 | 2017-06-22 16:06:36 +0200 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * A default implementation for a Band. |
| 26 | */ |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 27 | public final class DefaultBand implements Band, BandEntry { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 28 | |
| 29 | private final Type type; |
| 30 | private final long rate; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 31 | //TODO: should be made optional |
| 32 | private final Long burstSize; |
| 33 | private final Short prec; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 34 | private long packets; |
| 35 | private long bytes; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 36 | |
| 37 | public DefaultBand(Type type, long rate, |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 38 | Long burstSize, Short prec) { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 39 | this.type = type; |
Jayasree Ghosh | e7a240c | 2016-09-10 14:45:15 +0530 | [diff] [blame] | 40 | if (type == Type.REMARK) { |
| 41 | checkArgument(prec <= MAX_PRECEDENCE && prec >= MIN_PRECEDENCE, ERR_MSG); |
| 42 | } |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 43 | this.rate = rate; |
| 44 | this.burstSize = burstSize; |
| 45 | this.prec = prec; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public long rate() { |
| 50 | return rate; |
| 51 | } |
| 52 | |
| 53 | @Override |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 54 | public Long burst() { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 55 | return burstSize; |
| 56 | } |
| 57 | |
| 58 | @Override |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 59 | public Short dropPrecedence() { |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 60 | return prec; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Type type() { |
| 65 | return type; |
| 66 | } |
| 67 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 68 | @Override |
| 69 | public long packets() { |
| 70 | return packets; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public long bytes() { |
| 75 | return bytes; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void setPackets(long packets) { |
| 80 | this.packets = packets; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void setBytes(long bytes) { |
| 85 | this.bytes = bytes; |
| 86 | } |
| 87 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public String toString() { |
| 90 | return toStringHelper(this) |
| 91 | .add("rate", rate) |
| 92 | .add("burst-size", burstSize) |
| 93 | .add("type", type) |
| 94 | .add("drop-precedence", prec).toString(); |
| 95 | } |
| 96 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 97 | public static Builder builder() { |
| 98 | return new Builder(); |
| 99 | } |
| 100 | |
Daniele Moro | 909b958 | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 101 | @Override |
| 102 | public boolean equals(Object o) { |
| 103 | if (this == o) { |
| 104 | return true; |
| 105 | } |
| 106 | if (o == null || getClass() != o.getClass()) { |
| 107 | return false; |
| 108 | } |
| 109 | DefaultBand that = (DefaultBand) o; |
| 110 | return rate == that.rate && |
| 111 | type == that.type && |
| 112 | Objects.equal(burstSize, that.burstSize) && |
| 113 | Objects.equal(prec, that.prec); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int hashCode() { |
| 118 | return Objects.hashCode(type, rate, burstSize, prec); |
| 119 | } |
| 120 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 121 | public static final class Builder implements Band.Builder { |
| 122 | |
| 123 | private long rate; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 124 | private Long burstSize; |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 125 | private Short prec; |
| 126 | private Type type; |
| 127 | |
| 128 | @Override |
| 129 | public Band.Builder withRate(long rate) { |
| 130 | this.rate = rate; |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Band.Builder burstSize(long burstSize) { |
| 136 | this.burstSize = burstSize; |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Band.Builder dropPrecedence(short prec) { |
| 142 | this.prec = prec; |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Band.Builder ofType(Type type) { |
| 148 | this.type = type; |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | @Override |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 153 | public DefaultBand build() { |
Jordi Ortiz | af75c13 | 2017-06-22 16:06:36 +0200 | [diff] [blame] | 154 | checkNotNull(type, "Band type can not be null"); |
Jian Li | 1684b00 | 2015-12-16 15:38:46 -0800 | [diff] [blame] | 155 | checkArgument(type == Type.REMARK ^ prec == null, |
| 156 | "Only REMARK bands can have a precedence."); |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 157 | return new DefaultBand(type, rate, burstSize, prec); |
| 158 | } |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 159 | } |
| 160 | } |