Merge "OPNFLWPLUG-1062 Include additional LLDP fields in liblldp"
[openflowplugin.git] / libraries / liblldp / src / main / java / org / opendaylight / openflowplugin / libraries / liblldp / LLDPTLV.java
index f5af1572e570833b7742fd85766a124c5f0abeae..2263a5f749ca40bb6947482bf544a5cd64a60055 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.openflowplugin.libraries.liblldp;
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -17,40 +18,42 @@ import java.util.Map;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.tuple.MutablePair;
 import org.apache.commons.lang3.tuple.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Class that represents the LLDPTLV objects.
  */
 @SuppressWarnings("checkstyle:AbbreviationAsWordInName")
 public class LLDPTLV extends Packet {
+    private static final Logger LOG = LoggerFactory.getLogger(LLDPTLV.class);
     private static final String TYPE = "Type";
     private static final String LENGTH = "Length";
     private static final String VALUE = "Value";
     private static final int LLDPTLV_FIELDS = 3;
 
     /** OpenFlow OUI. */
-    public static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26,
-        (byte) 0xe1 };
+    static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26, (byte) 0xe1 };
 
     /** Length of Organizationally defined subtype field of TLV in bytes.   */
     private static final byte CUSTOM_TLV_SUB_TYPE_LENGTH = (byte)1;
 
     /** OpenFlow subtype: nodeConnectorId of source. */
-    public static final byte[] CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID = new byte[] { 0 };
+    private static final byte[] CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID = new byte[] { 0 };
 
     /** OpenFlow subtype: custom sec = hash code of verification of origin of LLDP. */
-    public static final byte[] CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC = new byte[] { 1 };
+    private static final byte[] CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC = new byte[] { 1 };
 
-    public static final int CUSTOM_TLV_OFFSET = OFOUI.length + CUSTOM_TLV_SUB_TYPE_LENGTH;
-    public static final byte[] CHASSISID_SUB_TYPE = new byte[] { 4 }; // MAC address for the system
-    public static final byte[] PORTID_SUB_TYPE = new byte[] { 7 }; // locally assigned
+    private static final int CUSTOM_TLV_OFFSET = OFOUI.length + CUSTOM_TLV_SUB_TYPE_LENGTH;
+    private static final byte[] CHASSISID_SUB_TYPE = new byte[] { 4 }; // MAC address for the system
+    private static final byte[] PORTID_SUB_TYPE = new byte[] { 7 }; // locally assigned
 
     public enum TLVType {
         Unknown((byte) 0), ChassisID((byte) 1), PortID((byte) 2), TTL((byte) 3), PortDesc(
-                (byte) 4), SystemName((byte) 5), SystemDesc((byte) 6), Custom(
-                        (byte) 127);
+                (byte) 4), SystemName((byte) 5), SystemDesc((byte) 6), SystemCapabilities((byte) 7),
+                ManagementAddress((byte) 8), Custom((byte) 127);
 
-        private byte value;
+        private final byte value;
 
         TLVType(final byte value) {
             this.value = value;
@@ -95,7 +98,7 @@ public class LLDPTLV extends Packet {
      */
     public int getLength() {
         return (int) BitBufferHelper.toNumber(fieldValues.get(LENGTH),
-                FIELD_COORDINATES.get(LENGTH).getRight().intValue());
+                FIELD_COORDINATES.get(LENGTH).getRight());
     }
 
     /**
@@ -186,8 +189,7 @@ public class LLDPTLV extends Packet {
     public int getfieldnumBits(final String fieldName) {
         if (fieldName.equals(VALUE)) {
             return NetUtils.NUM_BITS_IN_A_BYTE * BitBufferHelper.getShort(
-                    fieldValues.get(LENGTH), FIELD_COORDINATES.get(LENGTH)
-                    .getRight().intValue());
+                    fieldValues.get(LENGTH), FIELD_COORDINATES.get(LENGTH).getRight());
         }
         return FIELD_COORDINATES.get(fieldName).getRight();
     }
@@ -211,8 +213,7 @@ public class LLDPTLV extends Packet {
      * @return the SystemName TLV value in byte array
      */
     public static byte[] createSystemNameTLVValue(final String nodeId) {
-        byte[] nid = nodeId.getBytes();
-        return nid;
+        return nodeId.getBytes(StandardCharsets.UTF_8);
     }
 
     /**
@@ -293,6 +294,16 @@ public class LLDPTLV extends Packet {
         return customValue;
     }
 
+    /**
+     * Creates a custom TLV value including OUI of sub type custom sec and custom bytes value.
+     *
+     * @param customValue the custom value
+     * @return the custom TLV value in byte array
+     */
+    public static byte[] createSecSubTypeCustomTLVValue(final byte[] customValue) {
+        return createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC, customValue);
+    }
+
     /**
      * Retrieves the string from TLV value and returns it in HexString format.
      *
@@ -368,4 +379,13 @@ public class LLDPTLV extends Packet {
         byte[] value = lldptlv.getValue();
         return BitBufferHelper.getByte(ArrayUtils.subarray(value, 3, 4));
     }
+
+    public static CustomTLVKey createPortSubTypeCustomTLVKey() {
+        return new CustomTLVKey(BitBufferHelper.getInt(OFOUI), CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID[0]);
+    }
+
+    public static CustomTLVKey createSecSubTypeCustomTLVKey() {
+        return new CustomTLVKey(BitBufferHelper.getInt(LLDPTLV.OFOUI), LLDPTLV.CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC[0]);
+    }
 }
+