blob: 80675e259485df5291a2d00aded450c680996e5f [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.BasePacket;
22import org.onlab.packet.Deserializer;
Yi Tsengca34e1d2017-07-18 16:16:25 -070023
24import java.nio.ByteBuffer;
Yi Tsengb4fdb042017-08-07 13:32:32 -070025import java.util.Arrays;
Yi Tsengca34e1d2017-07-18 16:16:25 -070026
27public class Dhcp6Duid extends BasePacket {
28 private static final int DEFAULT_LLT_LEN = 8;
29 private static final int DEFAULT_EN_LEN = 6;
30 private static final int DEFAULT_LL_LEN = 4;
31 public enum DuidType {
32 DUID_LLT((short) 1),
33 DUID_EN((short) 2),
34 DUID_LL((short) 3);
35
36 private short value;
37 DuidType(short value) {
38 this.value = value;
39 }
40
41 public short getValue() {
42 return value;
43 }
44
45 public static DuidType of(short type) {
46 switch (type) {
47 case 1:
48 return DUID_LLT;
49 case 2:
50 return DUID_EN;
51 case 3:
52 return DUID_LL;
53 default:
Ray Milkey986a47a2018-01-25 11:38:51 -080054 throw new IllegalArgumentException("Unknown type: " + type);
Yi Tsengca34e1d2017-07-18 16:16:25 -070055 }
56 }
57 }
58 // general field
59 private DuidType duidType;
60
61 // fields for DUID_LLT & DUID_LL
62 private short hardwareType;
63 private int duidTime;
64 private byte[] linkLayerAddress;
65
66 // fields for DUID_EN
67 private int enterpriseNumber;
68 private byte[] identifier;
69
70 public DuidType getDuidType() {
71 return duidType;
72 }
73
74 public void setDuidType(DuidType duidType) {
75 this.duidType = duidType;
76 }
77
78 public short getHardwareType() {
79 return hardwareType;
80 }
81
82 public void setHardwareType(short hardwareType) {
83 this.hardwareType = hardwareType;
84 }
85
86 public int getDuidTime() {
87 return duidTime;
88 }
89
90 public void setDuidTime(int duidTime) {
91 this.duidTime = duidTime;
92 }
93
94 public byte[] getLinkLayerAddress() {
95 return linkLayerAddress;
96 }
97
98 public void setLinkLayerAddress(byte[] linkLayerAddress) {
99 this.linkLayerAddress = linkLayerAddress;
100 }
101
102 public int getEnterpriseNumber() {
103 return enterpriseNumber;
104 }
105
106 public void setEnterpriseNumber(int enterpriseNumber) {
107 this.enterpriseNumber = enterpriseNumber;
108 }
109
110 public byte[] getIdentifier() {
111 return identifier;
112 }
113
114 public void setIdentifier(byte[] identifier) {
115 this.identifier = identifier;
116 }
117
118 @Override
119 public byte[] serialize() {
120 ByteBuffer byteBuffer;
121 switch (duidType) {
122 case DUID_LLT:
123 byteBuffer = ByteBuffer.allocate(DEFAULT_LLT_LEN + linkLayerAddress.length);
124 byteBuffer.putShort(duidType.value);
125 byteBuffer.putShort(hardwareType);
126 byteBuffer.putInt(duidTime);
127 byteBuffer.put(linkLayerAddress);
128 break;
129 case DUID_EN:
130 byteBuffer = ByteBuffer.allocate(DEFAULT_EN_LEN + identifier.length);
131 byteBuffer.putShort(duidType.value);
132 byteBuffer.putInt(enterpriseNumber);
133 byteBuffer.put(identifier);
134 break;
135 case DUID_LL:
136 byteBuffer = ByteBuffer.allocate(DEFAULT_LL_LEN + linkLayerAddress.length);
137 byteBuffer.putShort(duidType.value);
138 byteBuffer.putShort(hardwareType);
139 byteBuffer.put(linkLayerAddress);
140 break;
141 default:
Ray Milkey986a47a2018-01-25 11:38:51 -0800142 throw new IllegalArgumentException("Unknown duidType: " + duidType.toString());
Yi Tsengca34e1d2017-07-18 16:16:25 -0700143 }
144 return byteBuffer.array();
145 }
146
Ray Milkeyf0c47612017-09-28 11:29:38 -0700147
Yi Tsengca34e1d2017-07-18 16:16:25 -0700148
149 public static Deserializer<Dhcp6Duid> deserializer() {
150 return (data, offset, length) -> {
151 Dhcp6Duid duid = new Dhcp6Duid();
152 ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, length);
153
154 DuidType duidType = DuidType.of(byteBuffer.getShort());
155 duid.setDuidType(duidType);
156 switch (duidType) {
157 case DUID_LLT:
158 duid.setHardwareType(byteBuffer.getShort());
159 duid.setDuidTime(byteBuffer.getInt());
160 duid.linkLayerAddress = new byte[length - DEFAULT_LLT_LEN];
161 byteBuffer.get(duid.linkLayerAddress);
162 break;
163 case DUID_EN:
164 duid.setEnterpriseNumber(byteBuffer.getInt());
165 duid.identifier = new byte[length - DEFAULT_EN_LEN];
166 byteBuffer.get(duid.identifier);
167 break;
168 case DUID_LL:
169 duid.setHardwareType(byteBuffer.getShort());
170 duid.linkLayerAddress = new byte[length - DEFAULT_LL_LEN];
171 byteBuffer.get(duid.linkLayerAddress);
172 break;
173 default:
Ray Milkey986a47a2018-01-25 11:38:51 -0800174 throw new IllegalArgumentException("Unknown type: " + duidType);
Yi Tsengca34e1d2017-07-18 16:16:25 -0700175 }
176 return duid;
177 };
178 }
Yi Tsengb4fdb042017-08-07 13:32:32 -0700179
180 @Override
181 public String toString() {
182 MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(getClass());
183
184 switch (duidType) {
185 case DUID_LLT:
186 helper.add("type", "DUID_LLT");
187 helper.add("hardwareType", hardwareType);
188 helper.add("duidTime", duidTime);
189 helper.add("linkLayerAddress", Arrays.toString(linkLayerAddress));
190 break;
191 case DUID_EN:
192 helper.add("type", "DUID_EN");
193 helper.add("enterpriseNumber", enterpriseNumber);
194 helper.add("id", Arrays.toString(identifier));
195 break;
196 case DUID_LL:
197 helper.add("type", "DUID_LL");
198 helper.add("hardwareType", hardwareType);
199 helper.add("linkLayerAddress", Arrays.toString(linkLayerAddress));
200 break;
201 default:
202 helper.add("type", "Unknown");
203 }
204 return helper.toString();
205 }
Yi Tsengca34e1d2017-07-18 16:16:25 -0700206}