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