blob: f4f3fc8887524541ab9710da0f0318898a608b26 [file] [log] [blame]
Yi Tsengca34e1d2017-07-18 16:16:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengca34e1d2017-07-18 16:16:25 -07003 *
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
18package org.onlab.packet.dhcp;
19
Yi Tsengb4fdb042017-08-07 13:32:32 -070020import com.google.common.base.MoreObjects;
Yi Tsengca34e1d2017-07-18 16:16:25 -070021import org.onlab.packet.DHCP6;
22import org.onlab.packet.DeserializationException;
23import org.onlab.packet.Deserializer;
24
25import java.nio.ByteBuffer;
26
27/**
28 * DHCPv6 Client Identifier Option.
29 */
30public 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 Milkey986a47a2018-01-25 11:38:51 -080052 throw new IllegalArgumentException("Invalid DUID");
Yi Tsengca34e1d2017-07-18 16:16:25 -070053 }
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 Tsengb4fdb042017-08-07 13:32:32 -070091
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 Tsengca34e1d2017-07-18 16:16:25 -0700100}