blob: baa2e6eb821c574ff668359e83505d5b970b4d25 [file] [log] [blame]
Lior Assoulinea21c0ca2018-01-28 16:18:48 -08001/*
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
18package org.onlab.packet.dhcp;
19
20import com.google.common.base.MoreObjects;
21import com.google.common.collect.Lists;
22import org.onlab.packet.DHCP6;
23import org.onlab.packet.Deserializer;
24import org.onlab.packet.Ip6Address;
25
26import java.nio.ByteBuffer;
27import java.util.List;
28import java.util.Objects;
29
30/**
31 * DHCPv6 Lease Query Option.
32 */
33public final class Dhcp6LeaseQueryOption extends Dhcp6Option {
34
35 public static final int DEFAULT_LEN = 1 + 16;
36 //public short QueryType;
37 public Ip6Address linkAddress;
38 private List<Dhcp6Option> options;
Taras Lemkin96a0d342018-03-26 14:52:58 +000039 public byte queryType;
Lior Assoulinea21c0ca2018-01-28 16:18:48 -080040
41 public Dhcp6LeaseQueryOption(Dhcp6Option dhcp6Option) {
42 super(dhcp6Option);
Taras Lemkin96a0d342018-03-26 14:52:58 +000043 options = Lists.newArrayList();
Lior Assoulinea21c0ca2018-01-28 16:18:48 -080044 }
45
46 @Override
47 public short getCode() {
48 return DHCP6.OptionCode.LEASE_QUERY.value();
49 }
50
51 @Override
52 public short getLength() {
53 //return (short) payload.serialize().length;
54 return (short) (DEFAULT_LEN + options.stream()
55 .mapToInt(opt -> (int) opt.getLength() + Dhcp6Option.DEFAULT_LEN)
56 .sum());
57 }
58
59 @Override
60 public byte[] getData() {
61 return payload.serialize();
62 }
63
64
65 public static Deserializer<Dhcp6Option> deserializer() {
66 return (data, offset, length) -> {
67 Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, length);
68 Dhcp6LeaseQueryOption lQ6Option = new Dhcp6LeaseQueryOption(dhcp6Option);
69
70 byte[] optionData = lQ6Option.getData();
Taras Lemkin96a0d342018-03-26 14:52:58 +000071 if (optionData.length >= DEFAULT_LEN) {
Lior Assoulinea21c0ca2018-01-28 16:18:48 -080072 ByteBuffer bb = ByteBuffer.wrap(optionData);
73 // fetch the Query type - just pop the byte from the byte buffer for subsequent parsing...
Taras Lemkin96a0d342018-03-26 14:52:58 +000074 lQ6Option.queryType = bb.get();
Lior Assoulinea21c0ca2018-01-28 16:18:48 -080075 byte[] ipv6Addr = new byte[16];
76 bb.get(ipv6Addr);
77 lQ6Option.linkAddress = Ip6Address.valueOf(ipv6Addr);
78 //int optionsLen = dhcp6Option.getLength() - 1 - 16; // query type (1) + link address (16)
79
80 lQ6Option.options = Lists.newArrayList();
81
82 while (bb.remaining() >= Dhcp6Option.DEFAULT_LEN) {
83 Dhcp6Option option;
84 ByteBuffer optByteBuffer = ByteBuffer.wrap(optionData,
85 bb.position(),
86 optionData.length - bb.position());
87 short code = optByteBuffer.getShort();
88 short len = optByteBuffer.getShort();
89 int optLen = UNSIGNED_SHORT_MASK & len;
90 byte[] subOptData = new byte[Dhcp6Option.DEFAULT_LEN + optLen];
91 bb.get(subOptData);
92
93 // TODO: put more sub-options?
94 if (code == DHCP6.OptionCode.IAADDR.value()) {
95 option = Dhcp6IaAddressOption.deserializer()
96 .deserialize(subOptData, 0, subOptData.length);
97 } else if (code == DHCP6.OptionCode.ORO.value()) {
98 option = Dhcp6Option.deserializer()
99 .deserialize(subOptData, 0, subOptData.length);
100 } else {
101 option = Dhcp6Option.deserializer()
102 .deserialize(subOptData, 0, subOptData.length);
103 }
104 lQ6Option.options.add(option);
105 }
106 }
107 return lQ6Option;
108 };
109 }
110
111 @Override
112 public byte[] serialize() {
Taras Lemkin96a0d342018-03-26 14:52:58 +0000113 byte[] serializedPayload = payload.serialize();
114
115 ByteBuffer bb = ByteBuffer.allocate(serializedPayload.length + Dhcp6Option.DEFAULT_LEN);
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800116 bb.putShort(getCode());
117 bb.putShort(getLength());
Taras Lemkin96a0d342018-03-26 14:52:58 +0000118 bb.put(serializedPayload);
119
Lior Assoulinea21c0ca2018-01-28 16:18:48 -0800120 return bb.array();
121 }
122
123 @Override
124 public String toString() {
125 return MoreObjects.toStringHelper(getClass())
126 .add("code", getCode())
127 .add("length", getLength())
128 .toString();
129 }
130
131
132 @Override
133 public int hashCode() {
134 return Objects.hash(super.hashCode(), linkAddress, options);
135 }
136
137 @Override
138 public boolean equals(Object obj) {
139 if (this == obj) {
140 return true;
141 }
142 if (obj == null) {
143 return false;
144 }
145 if (!(obj instanceof Dhcp6LeaseQueryOption)) {
146 return false;
147 }
148 if (!super.equals(obj)) {
149 return false;
150 }
151 final Dhcp6LeaseQueryOption other = (Dhcp6LeaseQueryOption) obj;
152
153 return Objects.equals(getCode(), other.getCode()) &&
154 Objects.equals(getLength(), other.getLength()) &&
155 Objects.equals(linkAddress, other.linkAddress) &&
156 Objects.equals(options, other.options);
157 }
158}