blob: 94418978195ada77dd1f78313b0d5e0c4a0c9def [file] [log] [blame]
alshabib1d2bc402015-07-31 17:04:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
alshabib58fe6dc2015-08-19 17:16:13 -070018import static com.google.common.base.MoreObjects.toStringHelper;
alshabib1d2bc402015-07-31 17:04:11 -070019import static com.google.common.base.Preconditions.checkArgument;
Jordi Ortizaf75c132017-06-22 16:06:36 +020020import static com.google.common.base.Preconditions.checkNotNull;
alshabib1d2bc402015-07-31 17:04:11 -070021
22/**
23 * A default implementation for a Band.
24 */
alshabib7bb05012015-08-05 10:15:09 -070025public final class DefaultBand implements Band, BandEntry {
alshabib1d2bc402015-07-31 17:04:11 -070026
27 private final Type type;
28 private final long rate;
alshabib58fe6dc2015-08-19 17:16:13 -070029 //TODO: should be made optional
30 private final Long burstSize;
31 private final Short prec;
alshabib7bb05012015-08-05 10:15:09 -070032 private long packets;
33 private long bytes;
alshabib1d2bc402015-07-31 17:04:11 -070034
35 public DefaultBand(Type type, long rate,
alshabib58fe6dc2015-08-19 17:16:13 -070036 Long burstSize, Short prec) {
alshabib1d2bc402015-07-31 17:04:11 -070037 this.type = type;
Jayasree Ghoshe7a240c2016-09-10 14:45:15 +053038 if (type == Type.REMARK) {
39 checkArgument(prec <= MAX_PRECEDENCE && prec >= MIN_PRECEDENCE, ERR_MSG);
40 }
alshabib1d2bc402015-07-31 17:04:11 -070041 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 Li5c411232015-12-16 15:29:16 -080052 public Long burst() {
alshabib1d2bc402015-07-31 17:04:11 -070053 return burstSize;
54 }
55
56 @Override
Jian Li5c411232015-12-16 15:29:16 -080057 public Short dropPrecedence() {
alshabib1d2bc402015-07-31 17:04:11 -070058 return prec;
59 }
60
61 @Override
62 public Type type() {
63 return type;
64 }
65
alshabib7bb05012015-08-05 10:15:09 -070066 @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
alshabib58fe6dc2015-08-19 17:16:13 -070086 @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
alshabib1d2bc402015-07-31 17:04:11 -070095 public static Builder builder() {
96 return new Builder();
97 }
98
99 public static final class Builder implements Band.Builder {
100
101 private long rate;
alshabib58fe6dc2015-08-19 17:16:13 -0700102 private Long burstSize;
alshabib1d2bc402015-07-31 17:04:11 -0700103 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
alshabib7bb05012015-08-05 10:15:09 -0700131 public DefaultBand build() {
Jordi Ortizaf75c132017-06-22 16:06:36 +0200132 checkNotNull(type, "Band type can not be null");
Jian Li1684b002015-12-16 15:38:46 -0800133 checkArgument(type == Type.REMARK ^ prec == null,
134 "Only REMARK bands can have a precedence.");
alshabib1d2bc402015-07-31 17:04:11 -0700135 return new DefaultBand(type, rate, burstSize, prec);
136 }
alshabib1d2bc402015-07-31 17:04:11 -0700137 }
138}