blob: 85e6c70f2689383f9b64475a190bb6748d806823 [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabib1d2bc402015-07-31 17:04:11 -07003 *
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 */
alshabib10c810b2015-08-18 16:59:04 -070016package org.onosproject.net.meter;
alshabib1d2bc402015-07-31 17:04:11 -070017
Daniele Morob25299a2022-01-26 15:47:29 +010018import com.google.common.base.Objects;
19
alshabib58fe6dc2015-08-19 17:16:13 -070020import static com.google.common.base.MoreObjects.toStringHelper;
alshabib1d2bc402015-07-31 17:04:11 -070021import static com.google.common.base.Preconditions.checkArgument;
Jordi Ortizaf75c132017-06-22 16:06:36 +020022import static com.google.common.base.Preconditions.checkNotNull;
alshabib1d2bc402015-07-31 17:04:11 -070023
24/**
25 * A default implementation for a Band.
26 */
alshabib7bb05012015-08-05 10:15:09 -070027public final class DefaultBand implements Band, BandEntry {
alshabib1d2bc402015-07-31 17:04:11 -070028
29 private final Type type;
30 private final long rate;
alshabib58fe6dc2015-08-19 17:16:13 -070031 //TODO: should be made optional
32 private final Long burstSize;
33 private final Short prec;
alshabib7bb05012015-08-05 10:15:09 -070034 private long packets;
35 private long bytes;
alshabib1d2bc402015-07-31 17:04:11 -070036
37 public DefaultBand(Type type, long rate,
alshabib58fe6dc2015-08-19 17:16:13 -070038 Long burstSize, Short prec) {
alshabib1d2bc402015-07-31 17:04:11 -070039 this.type = type;
Jayasree Ghoshe7a240c2016-09-10 14:45:15 +053040 if (type == Type.REMARK) {
41 checkArgument(prec <= MAX_PRECEDENCE && prec >= MIN_PRECEDENCE, ERR_MSG);
42 }
alshabib1d2bc402015-07-31 17:04:11 -070043 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 Li5c411232015-12-16 15:29:16 -080054 public Long burst() {
alshabib1d2bc402015-07-31 17:04:11 -070055 return burstSize;
56 }
57
58 @Override
Jian Li5c411232015-12-16 15:29:16 -080059 public Short dropPrecedence() {
alshabib1d2bc402015-07-31 17:04:11 -070060 return prec;
61 }
62
63 @Override
64 public Type type() {
65 return type;
66 }
67
alshabib7bb05012015-08-05 10:15:09 -070068 @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
alshabib58fe6dc2015-08-19 17:16:13 -070088 @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
alshabib1d2bc402015-07-31 17:04:11 -070097 public static Builder builder() {
98 return new Builder();
99 }
100
Daniele Morob25299a2022-01-26 15:47:29 +0100101 @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
alshabib1d2bc402015-07-31 17:04:11 -0700121 public static final class Builder implements Band.Builder {
122
123 private long rate;
alshabib58fe6dc2015-08-19 17:16:13 -0700124 private Long burstSize;
alshabib1d2bc402015-07-31 17:04:11 -0700125 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
alshabib7bb05012015-08-05 10:15:09 -0700153 public DefaultBand build() {
Jordi Ortizaf75c132017-06-22 16:06:36 +0200154 checkNotNull(type, "Band type can not be null");
Jian Li1684b002015-12-16 15:38:46 -0800155 checkArgument(type == Type.REMARK ^ prec == null,
156 "Only REMARK bands can have a precedence.");
alshabib1d2bc402015-07-31 17:04:11 -0700157 return new DefaultBand(type, rate, burstSize, prec);
158 }
alshabib1d2bc402015-07-31 17:04:11 -0700159 }
160}