OPNFLWPLUG-972 : Point to openflowplugin liblldp
[netvirt.git] / vpnservice / dhcpservice / dhcpservice-api / src / main / java / org / opendaylight / netvirt / dhcpservice / api / DHCPOptions.java
index e226809a4a8d2c901f7c96a944f7faaef44e9209..5f2f3e101d2c24950465422a1f70257bc605cdce 100644 (file)
@@ -8,34 +8,28 @@
 
 package org.opendaylight.netvirt.dhcpservice.api;
 
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPConstants.OPT_END;
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPConstants.OPT_PAD;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.nio.charset.StandardCharsets;
 import java.util.LinkedHashMap;
-
-
-
 import java.util.List;
-
 import org.apache.commons.lang3.ArrayUtils;
-import org.opendaylight.controller.liblldp.HexEncode;
-import org.opendaylight.controller.liblldp.NetUtils;
-//import org.apache.commons.lang3.ArrayUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import static org.opendaylight.netvirt.dhcpservice.api.DHCPConstants.*;
+import org.opendaylight.openflowplugin.libraries.liblldp.HexEncode;
+import org.opendaylight.openflowplugin.libraries.liblldp.NetUtils;
 
 public class DHCPOptions {
-    protected static final Logger logger = LoggerFactory
-            .getLogger(DHCPOptions.class);
 
-    class DhcpOption {
+    private static class DhcpOption {
         private byte code;
         private byte length;
         private byte[] value;
 
-        public DhcpOption(byte code, byte[] value) {
-            if ((code != OPT_PAD) && (code != OPT_END) && (value != null)) {
+        DhcpOption(byte code, byte[] value) {
+            if (code != OPT_PAD && code != OPT_END && value != null) {
                 this.code = code;
                 this.value = value;
                 this.length = (byte) value.length;
@@ -61,40 +55,37 @@ public class DHCPOptions {
         public String toString() {
             StringBuilder sb = new StringBuilder();
             sb.append("{ ")
-            .append("code: ").append(this.code)
-            .append(", len: ").append(this.length)
-            .append(", value: 0x").append(HexEncode.bytesToHexString(this.value))
-            .append(" }");
+                    .append("code: ").append(this.code)
+                    .append(", len: ").append(this.length)
+                    .append(", value: 0x").append(HexEncode.bytesToHexString(this.value))
+                    .append(" }");
             return sb.toString();
         }
     }
 
-    private LinkedHashMap<Byte, DhcpOption> options;
+    private final LinkedHashMap<Byte, DhcpOption> options;
 
     public DHCPOptions() {
-        options = new LinkedHashMap<Byte, DhcpOption>();
-    }
-
-    private void setOption(DhcpOption opt) {
-        this.options.put(opt.getCode(), opt);
+        options = new LinkedHashMap<>();
     }
 
     private DhcpOption getOption(byte code) {
         return this.options.get(code);
     }
 
+    private void setOption(DhcpOption opt) {
+        this.options.put(opt.getCode(), opt);
+    }
+
     public void setOption(byte code, byte[] opt) {
         this.setOption(new DhcpOption(code, opt));
     }
 
+    // It's unclear from all the callers if returning an empty array would be safe.
+    @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
     public byte[] getOptionBytes(byte code) {
-        try{
-            return this.getOption(code).getValue();
-        }
-        catch (NullPointerException e)
-        {
-            return null;
-        }
+        DhcpOption option = this.getOption(code);
+        return option != null ? option.getValue() : null;
     }
 
     public void setOptionByte(byte code, byte opt) {
@@ -144,55 +135,56 @@ public class DHCPOptions {
         byte[] opt = this.getOptionBytes(code);
         try {
             return InetAddress.getByAddress(opt).getHostAddress();
-        } catch (UnknownHostException| NullPointerException e) {
+        } catch (UnknownHostException | NullPointerException e) {
             return null;
         }
     }
 
     public void setOptionStrAddrs(byte code, List<String> addrs) throws UnknownHostException {
-        if(!addrs.isEmpty()) {
+        if (!addrs.isEmpty()) {
             this.setOption(new DhcpOption(code, DHCPUtils.strListAddrsToByteArray(addrs)));
         }
     }
 
     public void setOptionString(byte code, String str) {
-        this.setOption(new DhcpOption(code, str.getBytes()));
+        this.setOption(new DhcpOption(code, str.getBytes(StandardCharsets.UTF_8)));
     }
 
     public byte[] serialize() {
-        byte[] options = new byte[0];
-        for(DhcpOption dOpt: this.options.values()) {
-            options = ArrayUtils.addAll(options, dOpt.serialize());
+        byte[] serializedOptions = new byte[0];
+        for (DhcpOption dhcpOption: this.options.values()) {
+            serializedOptions = ArrayUtils.addAll(serializedOptions, dhcpOption.serialize());
         }
         byte[] end = new byte[] {(byte)255};
-        options = ArrayUtils.addAll(options, end);
-        return options;
+        serializedOptions = ArrayUtils.addAll(serializedOptions, end);
+        return serializedOptions;
     }
 
     private byte[] getOptionValArray(byte[] opt, int pos, int len) {
         byte[] val = new byte[len];
-        for(int i = 0; i < len; i++) {
+        for (int i = 0; i < len; i++) {
             val[i] = opt[pos + i];
         }
         return val;
     }
 
-    public void deserialize(byte[] options) {
+    public void deserialize(byte[] serializedOptions) {
         int pos = 0;
-        byte code, len;
+        byte code;
+        byte len;
         byte[] value;
-        if (options != null) {
-            while (pos < options.length) {
-                code = options[pos++];
+        if (serializedOptions != null) {
+            while (pos < serializedOptions.length) {
+                code = serializedOptions[pos++];
                 if (code == OPT_END) {
                     break;
                 }
-                len = options[pos++];
-                if ((len + pos) > options.length) {
+                len = serializedOptions[pos++];
+                if (len + pos > serializedOptions.length) {
                     // Throw exception???
                     break;
                 }
-                value = getOptionValArray(options, pos, len);
+                value = getOptionValArray(serializedOptions, pos, len);
                 setOption(code, value);
                 pos += len;
             }
@@ -203,11 +195,10 @@ public class DHCPOptions {
     public String toString() {
         StringBuilder sb = new StringBuilder();
         sb.append("{");
-        int i = 1;
-        for(DhcpOption dOpt: this.options.values()) {
+        int count = 1;
+        for (DhcpOption dhcpOption: this.options.values()) {
             //options = ArrayUtils.addAll(options, dOpt.serialize());
-            sb.append("Option").append(i++)
-            .append(dOpt.toString());
+            sb.append("Option").append(count++).append(dhcpOption.toString());
         }
         sb.append("}");
         return sb.toString();