2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.vpnservice.dhcpservice.api;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.LinkedHashMap;
17 import java.util.List;
19 import org.apache.commons.lang3.ArrayUtils;
20 import org.opendaylight.controller.liblldp.HexEncode;
21 import org.opendaylight.controller.liblldp.NetUtils;
22 //import org.apache.commons.lang3.ArrayUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import static org.opendaylight.vpnservice.dhcpservice.api.DHCPConstants.*;
28 public class DHCPOptions {
29 protected static final Logger logger = LoggerFactory
30 .getLogger(DHCPOptions.class);
32 class DhcpOption extends Object {
37 public DhcpOption(byte code, byte[] value) {
38 if ((code != OPT_PAD) && (code != OPT_END) && (value != null)) {
41 this.length = (byte) value.length;
45 public byte getCode() {
49 public byte[] getValue() {
53 public byte[] serialize() {
54 byte[] opt1 = new byte[2];
56 opt1[1] = this.length;
57 return ArrayUtils.addAll(opt1, this.value);
61 public String toString() {
62 StringBuilder sb = new StringBuilder();
64 .append("code: ").append(this.code)
65 .append(", len: ").append(this.length)
66 .append(", value: 0x").append(HexEncode.bytesToHexString(this.value))
72 private LinkedHashMap<Byte, DhcpOption> options;
74 public DHCPOptions() {
75 options = new LinkedHashMap<Byte, DhcpOption>();
78 private void setOption(DhcpOption opt) {
79 this.options.put(opt.getCode(), opt);
82 private DhcpOption getOption(byte code) {
83 return this.options.get(code);
86 public void setOption(byte code, byte[] opt) {
87 this.setOption(new DhcpOption(code, opt));
90 public byte[] getOptionBytes(byte code) {
92 return this.getOption(code).getValue();
94 catch (NullPointerException e)
100 public void setOptionByte(byte code, byte opt) {
101 this.setOption(new DhcpOption(code, DHCPUtils.byteToByteArray(opt)));
104 public byte getOptionByte(byte code) {
105 return this.getOption(code).getValue()[0];
108 public void setOptionShort(byte code, short opt) {
109 this.setOption(new DhcpOption(code, DHCPUtils.shortToByteArray(opt)));
112 public short getOptionShort(byte code) {
113 byte[] opt = this.getOptionBytes(code);
114 return DHCPUtils.byteArrayToShort(opt);
117 public void setOptionInt(byte code, int opt) {
118 this.setOption(new DhcpOption(code, DHCPUtils.intToByteArray(opt)));
121 public int getOptionInt(byte code) {
122 byte[] opt = this.getOptionBytes(code);
123 return NetUtils.byteArray4ToInt(opt);
126 public void setOptionInetAddr(byte code, InetAddress opt) {
127 this.setOption(new DhcpOption(code, DHCPUtils.inetAddrToByteArray(opt)));
130 public InetAddress getOptionInetAddr(byte code) {
131 byte[] opt = this.getOptionBytes(code);
133 return InetAddress.getByAddress(opt);
134 } catch (UnknownHostException | NullPointerException e) {
139 public void setOptionStrAddr(byte code, String opt) throws UnknownHostException {
140 this.setOption(new DhcpOption(code, DHCPUtils.strAddrToByteArray(opt)));
143 public String getOptionStrAddr(byte code) {
144 byte[] opt = this.getOptionBytes(code);
146 return InetAddress.getByAddress(opt).getHostAddress();
147 } catch (UnknownHostException| NullPointerException e) {
152 public void setOptionStrAddrs(byte code, List<String> addrs) throws UnknownHostException {
153 if(!addrs.isEmpty()) {
154 this.setOption(new DhcpOption(code, DHCPUtils.strListAddrsToByteArray(addrs)));
158 public void setOptionString(byte code, String str) {
159 this.setOption(new DhcpOption(code, str.getBytes()));
162 public byte[] serialize() {
163 byte[] options = new byte[0];
164 for(DhcpOption dOpt: this.options.values()) {
165 options = ArrayUtils.addAll(options, dOpt.serialize());
167 byte[] end = new byte[] {(byte)255};
168 options = ArrayUtils.addAll(options, end);
172 private byte[] getOptionValArray(byte[] opt, int pos, int len) {
173 byte[] val = new byte[len];
174 for(int i = 0; i < len; i++) {
175 val[i] = opt[pos + i];
180 public void deserialize(byte[] options) {
184 if (options != null) {
185 while (pos < options.length) {
186 code = options[pos++];
187 if (code == OPT_END) {
190 len = options[pos++];
191 if ((len + pos) > options.length) {
192 // Throw exception???
195 value = getOptionValArray(options, pos, len);
196 setOption(code, value);
203 public String toString() {
204 StringBuilder sb = new StringBuilder();
207 for(DhcpOption dOpt: this.options.values()) {
208 //options = ArrayUtils.addAll(options, dOpt.serialize());
209 sb.append("Option").append(i++)
210 .append(dOpt.toString());
213 return sb.toString();
216 public boolean containsOption(byte code) {
217 return options.containsKey(code);
220 public DhcpOption unsetOption(byte code) {
221 return options.remove(code);