blob: b0ce7385423db968f8329bfd48ea17e77d1d87cd [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;
20
21/**
22 * A default implementation for a Band.
23 */
alshabib7bb05012015-08-05 10:15:09 -070024public final class DefaultBand implements Band, BandEntry {
alshabib1d2bc402015-07-31 17:04:11 -070025
26 private final Type type;
27 private final long rate;
alshabib58fe6dc2015-08-19 17:16:13 -070028 //TODO: should be made optional
29 private final Long burstSize;
30 private final Short prec;
alshabib7bb05012015-08-05 10:15:09 -070031 private long packets;
32 private long bytes;
alshabib1d2bc402015-07-31 17:04:11 -070033
34 public DefaultBand(Type type, long rate,
alshabib58fe6dc2015-08-19 17:16:13 -070035 Long burstSize, Short prec) {
alshabib1d2bc402015-07-31 17:04:11 -070036 this.type = type;
Jayasree Ghoshe7a240c2016-09-10 14:45:15 +053037 if (type == Type.REMARK) {
38 checkArgument(prec <= MAX_PRECEDENCE && prec >= MIN_PRECEDENCE, ERR_MSG);
39 }
alshabib1d2bc402015-07-31 17:04:11 -070040 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 Li5c411232015-12-16 15:29:16 -080051 public Long burst() {
alshabib1d2bc402015-07-31 17:04:11 -070052 return burstSize;
53 }
54
55 @Override
Jian Li5c411232015-12-16 15:29:16 -080056 public Short dropPrecedence() {
alshabib1d2bc402015-07-31 17:04:11 -070057 return prec;
58 }
59
60 @Override
61 public Type type() {
62 return type;
63 }
64
alshabib7bb05012015-08-05 10:15:09 -070065 @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
alshabib58fe6dc2015-08-19 17:16:13 -070085 @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
alshabib1d2bc402015-07-31 17:04:11 -070094 public static Builder builder() {
95 return new Builder();
96 }
97
98 public static final class Builder implements Band.Builder {
99
100 private long rate;
alshabib58fe6dc2015-08-19 17:16:13 -0700101 private Long burstSize;
alshabib1d2bc402015-07-31 17:04:11 -0700102 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
alshabib7bb05012015-08-05 10:15:09 -0700130 public DefaultBand build() {
Jian Li1684b002015-12-16 15:38:46 -0800131 checkArgument(type == Type.REMARK ^ prec == null,
132 "Only REMARK bands can have a precedence.");
alshabib1d2bc402015-07-31 17:04:11 -0700133
134 return new DefaultBand(type, rate, burstSize, prec);
135 }
alshabib1d2bc402015-07-31 17:04:11 -0700136 }
137}