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