Lior Assouline | a21c0ca | 2018-01-28 16:18:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 18 | package org.onlab.packet.dhcp; |
| 19 | |
| 20 | import org.onlab.packet.DHCP6; |
| 21 | import org.onlab.packet.DeserializationException; |
| 22 | import org.onlab.packet.Deserializer; |
| 23 | |
| 24 | import java.nio.ByteBuffer; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | public final class Dhcp6CLTOption extends Dhcp6Option { |
| 28 | public static final int DEFAULT_LEN = 4; |
| 29 | private int clt; // client last transaction time |
| 30 | |
| 31 | @Override |
| 32 | public short getCode() { |
| 33 | return DHCP6.OptionCode.CLIENT_LT.value(); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public short getLength() { |
| 38 | return (short) (DEFAULT_LEN); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Gets Client Last Transaction Time. |
| 43 | * |
| 44 | * @return Client Last Transaction Time |
| 45 | */ |
| 46 | public int getClt() { |
| 47 | return clt; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Sets Identity Association ID. |
| 52 | * |
| 53 | * @param clt the Client Last Transaction Time. |
| 54 | */ |
| 55 | public void setClt(int clt) { |
| 56 | this.clt = clt; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Default constructor. |
| 62 | */ |
| 63 | public Dhcp6CLTOption() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Constructs a DHCPv6 Client Last Transaction Time option. |
| 68 | * |
| 69 | * @param dhcp6Option the DHCPv6 option |
| 70 | */ |
| 71 | public Dhcp6CLTOption(Dhcp6Option dhcp6Option) { |
| 72 | super(dhcp6Option); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Gets deserializer. |
| 77 | * |
| 78 | * @return the deserializer |
| 79 | */ |
| 80 | public static Deserializer<Dhcp6Option> deserializer() { |
| 81 | return (data, offset, length) -> { |
| 82 | Dhcp6Option dhcp6Option = |
| 83 | Dhcp6Option.deserializer().deserialize(data, offset, length); |
| 84 | if (dhcp6Option.getLength() < DEFAULT_LEN) { |
| 85 | throw new DeserializationException("Invalid CLT option data"); |
| 86 | } |
| 87 | Dhcp6CLTOption cltOption = new Dhcp6CLTOption(dhcp6Option); |
| 88 | byte[] optionData = cltOption.getData(); |
| 89 | ByteBuffer bb = ByteBuffer.wrap(optionData); |
| 90 | cltOption.clt = bb.getInt(); |
| 91 | |
| 92 | return cltOption; |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public byte[] serialize() { |
| 98 | int payloadLen = DEFAULT_LEN; |
| 99 | int len = Dhcp6Option.DEFAULT_LEN + payloadLen; |
| 100 | ByteBuffer bb = ByteBuffer.allocate(len); |
| 101 | bb.putShort(DHCP6.OptionCode.CLIENT_LT.value()); |
| 102 | bb.putShort((short) payloadLen); |
| 103 | bb.putInt(clt); |
| 104 | return bb.array(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return getToStringHelper() |
| 111 | .add("clt", clt) |
| 112 | .toString(); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | @Override |
| 117 | public int hashCode() { |
| 118 | return Objects.hash(super.hashCode(), clt); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean equals(Object obj) { |
| 123 | if (this == obj) { |
| 124 | return true; |
| 125 | } |
| 126 | if (obj == null) { |
| 127 | return false; |
| 128 | } |
| 129 | if (!(obj instanceof Dhcp6CLTOption)) { |
| 130 | return false; |
| 131 | } |
| 132 | if (!super.equals(obj)) { |
| 133 | return false; |
| 134 | } |
| 135 | final Dhcp6CLTOption other = (Dhcp6CLTOption) obj; |
| 136 | |
| 137 | return Objects.equals(getCode(), other.getCode()) && |
| 138 | Objects.equals(getLength(), other.getLength()) && |
| 139 | Objects.equals(clt, other.clt); |
| 140 | } |
| 141 | } |