blob: 236566cb72824700d55094bfb728d408208c4af9 [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;
37 this.rate = rate;
38 this.burstSize = burstSize;
39 this.prec = prec;
40 }
41
42 @Override
43 public long rate() {
44 return rate;
45 }
46
47 @Override
Jian Li5c411232015-12-16 15:29:16 -080048 public Long burst() {
alshabib1d2bc402015-07-31 17:04:11 -070049 return burstSize;
50 }
51
52 @Override
Jian Li5c411232015-12-16 15:29:16 -080053 public Short dropPrecedence() {
alshabib1d2bc402015-07-31 17:04:11 -070054 return prec;
55 }
56
57 @Override
58 public Type type() {
59 return type;
60 }
61
alshabib7bb05012015-08-05 10:15:09 -070062 @Override
63 public long packets() {
64 return packets;
65 }
66
67 @Override
68 public long bytes() {
69 return bytes;
70 }
71
72 @Override
73 public void setPackets(long packets) {
74 this.packets = packets;
75 }
76
77 @Override
78 public void setBytes(long bytes) {
79 this.bytes = bytes;
80 }
81
alshabib58fe6dc2015-08-19 17:16:13 -070082 @Override
83 public String toString() {
84 return toStringHelper(this)
85 .add("rate", rate)
86 .add("burst-size", burstSize)
87 .add("type", type)
88 .add("drop-precedence", prec).toString();
89 }
90
alshabib1d2bc402015-07-31 17:04:11 -070091 public static Builder builder() {
92 return new Builder();
93 }
94
95 public static final class Builder implements Band.Builder {
96
97 private long rate;
alshabib58fe6dc2015-08-19 17:16:13 -070098 private Long burstSize;
alshabib1d2bc402015-07-31 17:04:11 -070099 private Short prec;
100 private Type type;
101
102 @Override
103 public Band.Builder withRate(long rate) {
104 this.rate = rate;
105 return this;
106 }
107
108 @Override
109 public Band.Builder burstSize(long burstSize) {
110 this.burstSize = burstSize;
111 return this;
112 }
113
114 @Override
115 public Band.Builder dropPrecedence(short prec) {
116 this.prec = prec;
117 return this;
118 }
119
120 @Override
121 public Band.Builder ofType(Type type) {
122 this.type = type;
123 return this;
124 }
125
126 @Override
alshabib7bb05012015-08-05 10:15:09 -0700127 public DefaultBand build() {
Jian Li1684b002015-12-16 15:38:46 -0800128 checkArgument(type == Type.REMARK ^ prec == null,
129 "Only REMARK bands can have a precedence.");
alshabib1d2bc402015-07-31 17:04:11 -0700130
131 return new DefaultBand(type, rate, burstSize, prec);
132 }
alshabib1d2bc402015-07-31 17:04:11 -0700133 }
134}