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 com.google.common.base.Objects; |
| 20 | import org.onlab.packet.Deserializer; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | import static org.onlab.packet.PacketUtils.checkInput; |
| 26 | |
| 27 | /** |
| 28 | * Represents LACP collector information. |
| 29 | */ |
| 30 | public class LacpCollectorTlv extends LacpTlv { |
| 31 | public static final byte LENGTH = 16; |
| 32 | private static final byte[] RESERVED = new byte[12]; |
| 33 | |
| 34 | private short collectorMaxDelay; |
| 35 | |
| 36 | /** |
| 37 | * Gets collector max delay. |
| 38 | * |
| 39 | * @return collector max delay |
| 40 | */ |
| 41 | public short getCollectorMaxDelay() { |
| 42 | return collectorMaxDelay; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Sets collector max delay. |
| 47 | * |
| 48 | * @param collectorMaxDelay collector max delay |
| 49 | * @return this |
| 50 | */ |
| 51 | public LacpCollectorTlv setCollectorMaxDelay(short collectorMaxDelay) { |
| 52 | this.collectorMaxDelay = collectorMaxDelay; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Deserializer function for LacpCollectorTlv packets. |
| 58 | * |
| 59 | * @return deserializer function |
| 60 | */ |
| 61 | public static Deserializer<LacpCollectorTlv> deserializer() { |
| 62 | return (data, offset, length) -> { |
| 63 | checkInput(data, offset, length, LENGTH - HEADER_LENGTH); |
| 64 | |
| 65 | LacpCollectorTlv lacpCollectorTlv = new LacpCollectorTlv(); |
| 66 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 67 | |
| 68 | lacpCollectorTlv.setCollectorMaxDelay(bb.getShort()); |
| 69 | |
| 70 | return lacpCollectorTlv; |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public byte[] serialize() { |
| 76 | final byte[] data = new byte[LENGTH - HEADER_LENGTH]; |
| 77 | |
| 78 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 79 | bb.putShort(this.collectorMaxDelay); |
| 80 | bb.put(RESERVED); |
| 81 | |
| 82 | return data; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean equals(Object obj) { |
| 87 | if (this == obj) { |
| 88 | return true; |
| 89 | } |
| 90 | if (!super.equals(obj)) { |
| 91 | return false; |
| 92 | } |
| 93 | if (!(obj instanceof LacpCollectorTlv)) { |
| 94 | return false; |
| 95 | } |
| 96 | final LacpCollectorTlv other = (LacpCollectorTlv) obj; |
| 97 | return this.collectorMaxDelay == other.collectorMaxDelay; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public int hashCode() { |
| 102 | return Objects.hashCode(super.hashCode(), collectorMaxDelay); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public String toString() { |
| 107 | return toStringHelper(getClass()) |
| 108 | .add("collectorMaxDelay", Short.toString(collectorMaxDelay)) |
| 109 | .toString(); |
| 110 | } |
| 111 | } |