Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [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.packet.DscpClass; |
| 21 | import org.onlab.packet.IPPrecedence; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | /** |
| 29 | * Represents an action to be taken by a marker/policer. |
| 30 | */ |
| 31 | @Beta |
| 32 | public final class BandwidthProfileAction { |
| 33 | |
| 34 | /** |
| 35 | * Denotes the type of action to be taken. |
| 36 | */ |
| 37 | public enum Action { |
| 38 | /** |
| 39 | * Traffic is allowed to pass unmodified. |
| 40 | */ |
| 41 | PASS, |
| 42 | |
| 43 | /** |
| 44 | * Traffic is allowed to pass after being appropriately remarked. |
| 45 | */ |
| 46 | REMARK, |
| 47 | |
| 48 | /** |
| 49 | * Traffic is dropped. |
| 50 | */ |
| 51 | DISCARD |
| 52 | } |
| 53 | |
| 54 | private final Action action; |
| 55 | private final DscpClass dscpClass; |
| 56 | private final IPPrecedence ipPrecedence; |
| 57 | private final Short dropPrecedence; |
| 58 | |
| 59 | private BandwidthProfileAction(Action action, |
| 60 | DscpClass dscpClass, |
| 61 | IPPrecedence ipPrecedence, |
| 62 | Short dropPrecedence) { |
| 63 | this.action = action; |
| 64 | this.dscpClass = dscpClass; |
| 65 | this.ipPrecedence = ipPrecedence; |
| 66 | this.dropPrecedence = dropPrecedence; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Obtains the type of this bandwidth profile action object. |
| 71 | * |
| 72 | * @return the bandwidth profile action type |
| 73 | */ |
| 74 | public Action getAction() { |
| 75 | return this.action; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Obtains the DSCP class corresponding to the REMARK action. |
| 80 | * If this is not a REMARK action or if another field is remarked |
| 81 | * null is returned. |
| 82 | * |
| 83 | * @return the DSCP class for the action; may be null |
| 84 | */ |
| 85 | public DscpClass getDscpClass() { |
| 86 | return this.dscpClass; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Obtains the IP precedence corresponding to the REMARK action. |
| 91 | * If this is not a REMARK action or if another field is remarked |
| 92 | * null is returned. |
| 93 | * |
| 94 | * @return the IP precedence for the action; may be null |
| 95 | */ |
| 96 | public IPPrecedence getIpPrecedence() { |
| 97 | return this.ipPrecedence; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Obtains the drop precedence corresponding to the REMARK action. |
| 102 | * If this is not a REMARK action or if another field is remarked |
| 103 | * null is returned. |
| 104 | * |
| 105 | * @return the drop precedence for the action; may be null |
| 106 | */ |
| 107 | public Short getDropPrecedence() { |
| 108 | return this.dropPrecedence; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns a new builder. |
| 113 | * |
| 114 | * @return new builder |
| 115 | */ |
| 116 | public static Builder builder() { |
| 117 | return new Builder(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Builder of BandwidthProfileAction entities. |
| 122 | */ |
| 123 | public static final class Builder { |
| 124 | |
| 125 | private Action action; |
| 126 | private DscpClass dscpClass; |
| 127 | private IPPrecedence ipPrecedence; |
| 128 | private Short dropPrecedence; |
| 129 | |
| 130 | /** |
| 131 | * Sets the type of this builder. |
| 132 | * |
| 133 | * @param action the builder type to set |
| 134 | * @return this builder instance |
| 135 | */ |
| 136 | public Builder action(Action action) { |
| 137 | this.action = action; |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets the DSCP class of this builder. |
| 143 | * |
| 144 | * @param dscpClass the builder DSCP class to set |
| 145 | * @return this builder instance |
| 146 | */ |
| 147 | public Builder dscpClass(DscpClass dscpClass) { |
| 148 | this.dscpClass = dscpClass; |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Sets the IP precedence of this builder. |
| 154 | * |
| 155 | * @param ipPrecedence the builder IP precedence to set |
| 156 | * @return this builder instance |
| 157 | */ |
| 158 | public Builder ipPrecedence(IPPrecedence ipPrecedence) { |
| 159 | this.ipPrecedence = ipPrecedence; |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the drop precedence of this builder. |
| 165 | * |
| 166 | * @param dropPrecedence the drop IP precedence to set |
| 167 | * @return this builder instance |
| 168 | */ |
| 169 | public Builder dropPrecedence(Short dropPrecedence) { |
| 170 | this.dropPrecedence = dropPrecedence; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Builds a new BandwidthProfileAction based on builder's parameters. |
| 176 | * |
| 177 | * @return a new BandwidthProfileAction instance |
| 178 | */ |
| 179 | public BandwidthProfileAction build() { |
| 180 | checkNotNull(action); |
| 181 | checkArgument(!action.equals(Action.REMARK) || |
| 182 | (dscpClass != null ^ |
| 183 | ipPrecedence != null ^ |
| 184 | dropPrecedence != null), |
| 185 | "Exactly one remark type must be defined"); |
| 186 | return new BandwidthProfileAction(action, |
| 187 | dscpClass, |
| 188 | ipPrecedence, |
| 189 | dropPrecedence); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public int hashCode() { |
| 195 | return Objects.hash(action, dscpClass, ipPrecedence, dropPrecedence); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean equals(Object obj) { |
| 200 | if (this == obj) { |
| 201 | return true; |
| 202 | } |
| 203 | if (obj instanceof BandwidthProfileAction) { |
| 204 | final BandwidthProfileAction that = (BandwidthProfileAction) obj; |
| 205 | return this.getClass() == that.getClass() && |
| 206 | Objects.equals(this.action, that.action) && |
| 207 | Objects.equals(this.dscpClass, that.dscpClass) && |
| 208 | Objects.equals(this.ipPrecedence, that.ipPrecedence) && |
| 209 | Objects.equals(this.dropPrecedence, that.dropPrecedence); |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public String toString() { |
| 216 | return MoreObjects.toStringHelper(getClass()) |
| 217 | .add("action", action == null ? null : action.name()) |
| 218 | .add("dscpClass", dscpClass == null ? null : dscpClass.name()) |
| 219 | .add("ipPrecedence", ipPrecedence) |
| 220 | .add("dropPrecedence", dropPrecedence) |
| 221 | .toString(); |
| 222 | } |
| 223 | } |