blob: 400c2a5048f0478ee2aad2001c46743b32626c81 [file] [log] [blame]
Frank Wange11a98d2016-10-26 17:04:03 +08001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16package org.onosproject.net.behaviour;
17
18import com.google.common.annotations.Beta;
19import com.google.common.base.MoreObjects;
20import org.onlab.util.Bandwidth;
21import org.onosproject.net.AbstractDescription;
22import org.onosproject.net.SparseAnnotations;
23
24import java.util.EnumSet;
25import java.util.Objects;
26import java.util.Optional;
27
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Default implementation of Queue description entity.
33 */
34@Beta
35public final class DefaultQueueDescription extends AbstractDescription
36 implements QueueDescription {
37
38 private final QueueId queueId;
39 private final Optional<Integer> dscp;
40 private final EnumSet<Type> type;
41 private final Optional<Bandwidth> maxRate;
42 private final Optional<Bandwidth> minRate;
43 private final Optional<Long> burst;
44 private final Optional<Long> priority;
45
46 public static final int MIN_DSCP = 0;
47 public static final int MAX_DSCP = 63;
48
49 private DefaultQueueDescription(QueueId queueId, Optional<Integer> dscp, EnumSet<Type> type,
50 Optional<Bandwidth> maxRate, Optional<Bandwidth> minRate,
51 Optional<Long> burst, Optional<Long> priority,
52 SparseAnnotations... annotations) {
53 super(annotations);
54 if (dscp.isPresent()) {
55 checkArgument(dscp.get() < MIN_DSCP || dscp.get() > MAX_DSCP, "dscp should be in range 0 to 63.");
56 }
57 this.queueId = checkNotNull(queueId);
58 this.dscp = dscp;
59 this.type = type;
60 this.maxRate = maxRate;
61 this.minRate = minRate;
62 this.burst = burst;
63 this.priority = priority;
64 }
65
66 @Override
67 public QueueId queueId() {
68 return queueId;
69 }
70
71 @Override
72 public EnumSet<Type> type() {
73 return type;
74 }
75
76 @Override
77 public Optional<Integer> dscp() {
78 return dscp;
79 }
80
81 @Override
82 public Optional<Bandwidth> maxRate() {
83 return maxRate;
84 }
85
86 @Override
87 public Optional<Bandwidth> minRate() {
88 return minRate;
89 }
90
91 @Override
92 public Optional<Long> burst() {
93 return burst;
94 }
95
96 @Override
97 public Optional<Long> priority() {
98 return priority;
99 }
100
101 @Override
102 public int hashCode() {
103 return Objects.hash(queueId, type, dscp, maxRate, minRate, burst, priority);
104 }
105
106 @Override
107 public boolean equals(Object obj) {
108 if (this == obj) {
109 return true;
110 }
111 if (obj instanceof DefaultQueueDescription) {
112 final DefaultQueueDescription other = (DefaultQueueDescription) obj;
113 return Objects.equals(this.queueId, other.queueId) &&
114 Objects.equals(this.type, other.type) &&
115 Objects.equals(this.dscp, other.dscp) &&
116 Objects.equals(this.maxRate, other.maxRate) &&
117 Objects.equals(this.minRate, other.minRate) &&
118 Objects.equals(this.burst, other.burst) &&
119 Objects.equals(this.priority, other.priority);
120 }
121 return false;
122 }
123
124 @Override
125 public String toString() {
126 return MoreObjects.toStringHelper(this)
127 .add("name", queueId())
128 .add("type", type())
129 .add("dscp", dscp().orElse(0))
130 .add("maxRate", maxRate().orElse(null))
131 .add("minRate", minRate().orElse(null))
132 .add("burst", burst().orElse(0L))
133 .add("priority", priority().orElse(0L))
134 .toString();
135 }
136
137 public static Builder builder() {
138 return new Builder();
139 }
140
141 public static final class Builder implements QueueDescription.Builder {
142
143 private QueueId queueId;
144 private Optional<Integer> dscp = Optional.empty();
145 private EnumSet<Type> type;
146 private Optional<Bandwidth> minRate = Optional.empty();
147 private Optional<Bandwidth> maxRate = Optional.empty();
148 private Optional<Long> burst = Optional.empty();
149 private Optional<Long> priority = Optional.empty();
150
151 private Builder() {
152 }
153
154 @Override
155 public QueueDescription build() {
156 return new DefaultQueueDescription(queueId, dscp, type,
157 maxRate, minRate, burst, priority);
158 }
159
160 @Override
161 public Builder queueId(QueueId queueId) {
162 this.queueId = queueId;
163 return this;
164 }
165
166 @Override
167 public Builder dscp(Integer dscp) {
168 this.dscp = Optional.ofNullable(dscp);
169 return this;
170 }
171
172 @Override
173 public Builder type(EnumSet<Type> type) {
174 this.type = type;
175 return this;
176 }
177
178 @Override
179 public Builder maxRate(Bandwidth maxRate) {
180 this.maxRate = Optional.ofNullable(maxRate);
181 return this;
182 }
183
184 @Override
185 public Builder minRate(Bandwidth minRate) {
186 this.minRate = Optional.ofNullable(minRate);
187 return this;
188 }
189
190 @Override
191 public Builder burst(Long burst) {
192 this.burst = Optional.ofNullable(burst);
193 return this;
194 }
195
196 @Override
197 public Builder priority(Long priority) {
198 this.priority = Optional.ofNullable(priority);
199 return this;
200 }
201 }
202}