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