Charles Chan | 64c2dfd | 2018-07-24 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onlab.packet.lacp; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | /** |
| 22 | * LACP state. |
| 23 | */ |
| 24 | public class LacpState { |
| 25 | private static final byte MASK_ACTIVE = 0x1; |
| 26 | private static final byte MASK_TIMEOUT = 0x2; |
| 27 | private static final byte MASK_AGG = 0x4; |
| 28 | private static final byte MASK_SYNC = 0x8; |
| 29 | private static final byte MASK_COLLECTING = 0x10; |
| 30 | private static final byte MASK_DISTRIBUTING = 0x20; |
| 31 | private static final byte MASK_DEFAULT = 0x40; |
| 32 | private static final byte MASK_EXPIRED = (byte) 0x80; |
| 33 | |
| 34 | private byte state; |
| 35 | |
| 36 | /** |
| 37 | * Constructs LACP state with zero value. |
| 38 | */ |
| 39 | public LacpState() { |
| 40 | this.state = 0; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Constructs LACP state with given value. |
| 45 | * |
| 46 | * @param state state in byte. |
| 47 | */ |
| 48 | public LacpState(byte state) { |
| 49 | this.state = state; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets LACP state in byte. |
| 54 | * |
| 55 | * @return LACP state |
| 56 | */ |
| 57 | public byte toByte() { |
| 58 | return state; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Checks if this state has the active flag set. |
| 63 | * |
| 64 | * @return true if this state has the active flag set. |
| 65 | */ |
| 66 | public boolean isActive() { |
| 67 | return (state & MASK_ACTIVE) != 0; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets active bit. |
| 72 | * |
| 73 | * @param value desired value |
| 74 | * @return this |
| 75 | */ |
| 76 | public LacpState setActive(boolean value) { |
| 77 | setBit(MASK_ACTIVE, value); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Checks if this state has the timeout flag set. Timeout flag indicates short timeout if set. |
| 83 | * |
| 84 | * @return true if this state has the timeout flag set. |
| 85 | */ |
| 86 | public boolean isTimeout() { |
| 87 | return (state & MASK_TIMEOUT) != 0; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets timeout bit. |
| 92 | * |
| 93 | * @param value desired value |
| 94 | * @return this |
| 95 | */ |
| 96 | public LacpState setTimeout(boolean value) { |
| 97 | setBit(MASK_TIMEOUT, value); |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Checks if this state has the aggregatable flag set. |
| 103 | * |
| 104 | * @return true if this state has the aggregatable flag set. |
| 105 | */ |
| 106 | public boolean isAggregatable() { |
| 107 | return (state & MASK_AGG) != 0; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sets aggregatable bit. |
| 112 | * |
| 113 | * @param value desired value |
| 114 | * @return this |
| 115 | */ |
| 116 | public LacpState setAggregatable(boolean value) { |
| 117 | setBit(MASK_AGG, value); |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Checks if this state has the synchronization flag set. |
| 123 | * |
| 124 | * @return true if this state has the synchronization flag set. |
| 125 | */ |
| 126 | public boolean isSync() { |
| 127 | return (state & MASK_SYNC) != 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets sync bit. |
| 132 | * |
| 133 | * @param value desired value |
| 134 | * @return this |
| 135 | */ |
| 136 | public LacpState setSync(boolean value) { |
| 137 | setBit(MASK_SYNC, value); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Checks if this state has the collecting flag set. |
| 143 | * |
| 144 | * @return true if this state has the collecting flag set. |
| 145 | */ |
| 146 | public boolean isCollecting() { |
| 147 | return (state & MASK_COLLECTING) != 0; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Sets collecting bit. |
| 152 | * |
| 153 | * @param value desired value |
| 154 | * @return this |
| 155 | */ |
| 156 | public LacpState setCollecting(boolean value) { |
| 157 | setBit(MASK_COLLECTING, value); |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Checks if this state has the distributing flag set. |
| 163 | * |
| 164 | * @return true if this state has the distributing flag set. |
| 165 | */ |
| 166 | public boolean isDistributing() { |
| 167 | return (state & MASK_DISTRIBUTING) != 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Sets distributing bit. |
| 172 | * |
| 173 | * @param value desired value |
| 174 | * @return this |
| 175 | */ |
| 176 | public LacpState setDistributing(boolean value) { |
| 177 | setBit(MASK_DISTRIBUTING, value); |
| 178 | return this; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Checks if this state has the default flag set. |
| 183 | * |
| 184 | * @return true if this state has the default flag set. |
| 185 | */ |
| 186 | public boolean isDefault() { |
| 187 | return (state & MASK_DEFAULT) != 0; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Sets default bit. |
| 192 | * |
| 193 | * @param value desired value |
| 194 | * @return this |
| 195 | */ |
| 196 | public LacpState setDefault(boolean value) { |
| 197 | setBit(MASK_DEFAULT, value); |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Checks if this state has the expired flag set. |
| 203 | * |
| 204 | * @return true if this state has the expired flag set. |
| 205 | */ |
| 206 | public boolean isExpired() { |
| 207 | return (state & MASK_EXPIRED) != 0; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Sets expired bit. |
| 212 | * |
| 213 | * @param value desired value |
| 214 | * @return this |
| 215 | */ |
| 216 | public LacpState setExpired(boolean value) { |
| 217 | setBit(MASK_EXPIRED, value); |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the bit masked by given mask in the state to desired value. |
| 223 | * |
| 224 | * @param mask bit to mask |
| 225 | * @param value desire value |
| 226 | */ |
| 227 | private void setBit(byte mask, boolean value) { |
| 228 | state = (byte) (value ? state | mask : state & ~mask); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean equals(Object obj) { |
| 233 | if (this == obj) { |
| 234 | return true; |
| 235 | } |
| 236 | if (!(obj instanceof LacpState)) { |
| 237 | return false; |
| 238 | } |
| 239 | final LacpState other = (LacpState) obj; |
| 240 | |
| 241 | return this.state == other.state; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public int hashCode() { |
| 246 | return Objects.hash(state); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String toString() { |
| 251 | StringBuilder builder = new StringBuilder(); |
| 252 | builder.append("{ "); |
| 253 | if (isActive()) { |
| 254 | builder.append("ACT "); |
| 255 | } |
| 256 | if (isTimeout()) { |
| 257 | builder.append("STO "); |
| 258 | } |
| 259 | if (isAggregatable()) { |
| 260 | builder.append("AGG "); |
| 261 | } |
| 262 | if (isSync()) { |
| 263 | builder.append("SYN "); |
| 264 | } |
| 265 | if (isCollecting()) { |
| 266 | builder.append("COL "); |
| 267 | } |
| 268 | if (isDistributing()) { |
| 269 | builder.append("DIS "); |
| 270 | } |
| 271 | if (isDefault()) { |
| 272 | builder.append("DEF "); |
| 273 | } |
| 274 | if (isExpired()) { |
| 275 | builder.append("EXP "); |
| 276 | } |
| 277 | builder.append("}"); |
| 278 | return builder.toString(); |
| 279 | } |
| 280 | } |