Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [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 | */ |
| 17 | |
| 18 | package org.onlab.packet.dhcp; |
| 19 | |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 21 | import org.onlab.packet.DHCP6; |
| 22 | import org.onlab.packet.DeserializationException; |
| 23 | import org.onlab.packet.Deserializer; |
| 24 | |
| 25 | import java.nio.ByteBuffer; |
| 26 | |
| 27 | /** |
| 28 | * DHCPv6 Client Identifier Option. |
| 29 | */ |
| 30 | public final class Dhcp6ClientIdOption extends Dhcp6Option { |
| 31 | @Override |
| 32 | public short getCode() { |
| 33 | return DHCP6.OptionCode.CLIENTID.value(); |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public short getLength() { |
| 38 | return (short) payload.serialize().length; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public byte[] getData() { |
| 43 | return payload.serialize(); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void setData(byte[] data) { |
| 48 | try { |
| 49 | Dhcp6Duid duid = Dhcp6Duid.deserializer().deserialize(data, 0, data.length); |
| 50 | this.setDuid(duid); |
| 51 | } catch (DeserializationException e) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 52 | throw new IllegalArgumentException("Invalid DUID"); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | public Dhcp6Duid getDuid() { |
| 58 | return (Dhcp6Duid) payload; |
| 59 | } |
| 60 | |
| 61 | public void setDuid(Dhcp6Duid duid) { |
| 62 | this.setPayload(duid); |
| 63 | duid.setParent(this); |
| 64 | } |
| 65 | |
| 66 | public static Deserializer<Dhcp6Option> deserializer() { |
| 67 | return (data, offset, length) -> { |
| 68 | Dhcp6Option dhcp6Option = |
| 69 | Dhcp6Option.deserializer().deserialize(data, offset, length); |
| 70 | Dhcp6ClientIdOption clientIdentifier = new Dhcp6ClientIdOption(); |
| 71 | |
| 72 | if (dhcp6Option.getLength() < DEFAULT_LEN) { |
| 73 | throw new DeserializationException("Invalid length of Client Id option"); |
| 74 | } |
| 75 | |
| 76 | Dhcp6Duid duid = |
| 77 | Dhcp6Duid.deserializer().deserialize(dhcp6Option.getData(), 0, dhcp6Option.getLength()); |
| 78 | clientIdentifier.setPayload(duid); |
| 79 | return clientIdentifier; |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public byte[] serialize() { |
| 85 | ByteBuffer bb = ByteBuffer.allocate(this.getLength() + Dhcp6Option.DEFAULT_LEN); |
| 86 | bb.putShort(getCode()); |
| 87 | bb.putShort(getLength()); |
| 88 | bb.put(payload.serialize()); |
| 89 | return bb.array(); |
| 90 | } |
Yi Tseng | b4fdb04 | 2017-08-07 13:32:32 -0700 | [diff] [blame] | 91 | |
| 92 | @Override |
| 93 | public String toString() { |
| 94 | return MoreObjects.toStringHelper(getClass()) |
| 95 | .add("code", getCode()) |
| 96 | .add("length", getLength()) |
| 97 | .add("duid", getDuid().toString()) |
| 98 | .toString(); |
| 99 | } |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 100 | } |