blob: e8926cef9781e19dc29c5817f8363c751dca889c [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
20import org.onlab.packet.BasePacket;
21import org.onlab.packet.Deserializer;
22import org.onlab.packet.IPacket;
23
24import java.nio.ByteBuffer;
25
26public class Dhcp6Duid extends BasePacket {
27 private static final int DEFAULT_LLT_LEN = 8;
28 private static final int DEFAULT_EN_LEN = 6;
29 private static final int DEFAULT_LL_LEN = 4;
30 public enum DuidType {
31 DUID_LLT((short) 1),
32 DUID_EN((short) 2),
33 DUID_LL((short) 3);
34
35 private short value;
36 DuidType(short value) {
37 this.value = value;
38 }
39
40 public short getValue() {
41 return value;
42 }
43
44 public static DuidType of(short type) {
45 switch (type) {
46 case 1:
47 return DUID_LLT;
48 case 2:
49 return DUID_EN;
50 case 3:
51 return DUID_LL;
52 default:
53 throw new RuntimeException("Unknown type: " + type);
54 }
55 }
56 }
57 // general field
58 private DuidType duidType;
59
60 // fields for DUID_LLT & DUID_LL
61 private short hardwareType;
62 private int duidTime;
63 private byte[] linkLayerAddress;
64
65 // fields for DUID_EN
66 private int enterpriseNumber;
67 private byte[] identifier;
68
69 public DuidType getDuidType() {
70 return duidType;
71 }
72
73 public void setDuidType(DuidType duidType) {
74 this.duidType = duidType;
75 }
76
77 public short getHardwareType() {
78 return hardwareType;
79 }
80
81 public void setHardwareType(short hardwareType) {
82 this.hardwareType = hardwareType;
83 }
84
85 public int getDuidTime() {
86 return duidTime;
87 }
88
89 public void setDuidTime(int duidTime) {
90 this.duidTime = duidTime;
91 }
92
93 public byte[] getLinkLayerAddress() {
94 return linkLayerAddress;
95 }
96
97 public void setLinkLayerAddress(byte[] linkLayerAddress) {
98 this.linkLayerAddress = linkLayerAddress;
99 }
100
101 public int getEnterpriseNumber() {
102 return enterpriseNumber;
103 }
104
105 public void setEnterpriseNumber(int enterpriseNumber) {
106 this.enterpriseNumber = enterpriseNumber;
107 }
108
109 public byte[] getIdentifier() {
110 return identifier;
111 }
112
113 public void setIdentifier(byte[] identifier) {
114 this.identifier = identifier;
115 }
116
117 @Override
118 public byte[] serialize() {
119 ByteBuffer byteBuffer;
120 switch (duidType) {
121 case DUID_LLT:
122 byteBuffer = ByteBuffer.allocate(DEFAULT_LLT_LEN + linkLayerAddress.length);
123 byteBuffer.putShort(duidType.value);
124 byteBuffer.putShort(hardwareType);
125 byteBuffer.putInt(duidTime);
126 byteBuffer.put(linkLayerAddress);
127 break;
128 case DUID_EN:
129 byteBuffer = ByteBuffer.allocate(DEFAULT_EN_LEN + identifier.length);
130 byteBuffer.putShort(duidType.value);
131 byteBuffer.putInt(enterpriseNumber);
132 byteBuffer.put(identifier);
133 break;
134 case DUID_LL:
135 byteBuffer = ByteBuffer.allocate(DEFAULT_LL_LEN + linkLayerAddress.length);
136 byteBuffer.putShort(duidType.value);
137 byteBuffer.putShort(hardwareType);
138 byteBuffer.put(linkLayerAddress);
139 break;
140 default:
141 throw new RuntimeException("Unknown duidType: " + duidType.toString());
142 }
143 return byteBuffer.array();
144 }
145
146 @Override
147 public IPacket deserialize(byte[] data, int offset, int length) {
148 return null;
149 }
150
151 public static Deserializer<Dhcp6Duid> deserializer() {
152 return (data, offset, length) -> {
153 Dhcp6Duid duid = new Dhcp6Duid();
154 ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, length);
155
156 DuidType duidType = DuidType.of(byteBuffer.getShort());
157 duid.setDuidType(duidType);
158 switch (duidType) {
159 case DUID_LLT:
160 duid.setHardwareType(byteBuffer.getShort());
161 duid.setDuidTime(byteBuffer.getInt());
162 duid.linkLayerAddress = new byte[length - DEFAULT_LLT_LEN];
163 byteBuffer.get(duid.linkLayerAddress);
164 break;
165 case DUID_EN:
166 duid.setEnterpriseNumber(byteBuffer.getInt());
167 duid.identifier = new byte[length - DEFAULT_EN_LEN];
168 byteBuffer.get(duid.identifier);
169 break;
170 case DUID_LL:
171 duid.setHardwareType(byteBuffer.getShort());
172 duid.linkLayerAddress = new byte[length - DEFAULT_LL_LEN];
173 byteBuffer.get(duid.linkLayerAddress);
174 break;
175 default:
176 throw new RuntimeException("Unknown type: " + duidType);
177 }
178 return duid;
179 };
180 }
181}