Mechanical code cleanup (commons)
[controller.git] / opendaylight / commons / liblldp / src / main / java / org / opendaylight / controller / liblldp / LLDPTLV.java
index 22bd4626d196c11a9c4b2a4bc00929e27e79ff33..3c5623475e20cf21b5afaf1db7df609c4642df40 100644 (file)
@@ -8,13 +8,16 @@
 
 package org.opendaylight.controller.liblldp;
 
+import org.apache.commons.lang3.ArrayUtils;
+import org.slf4j.LoggerFactory;
+
+import org.slf4j.Logger;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
-
 import org.apache.commons.lang3.tuple.MutablePair;
 import org.apache.commons.lang3.tuple.Pair;
 
@@ -27,14 +30,26 @@ public class LLDPTLV extends Packet {
     private static final String LENGTH = "Length";
     private static final String VALUE = "Value";
     private static final int LLDPTLVFields = 3;
+
+    /** OpenFlow OUI */
     public static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26,
-        (byte) 0xe1 }; // OpenFlow OUI
-    public static final byte[] customTlvSubType = new byte[] { 0 };
-    public static final int customTlvOffset = OFOUI.length
-            + customTlvSubType.length;
+        (byte) 0xe1 };
+
+    /** Length of Organizationally defined subtype field of TLV in bytes   */
+    private static final byte customTlvSubTypeLength = (byte)1;
+
+    /** OpenFlow subtype: nodeConnectorId of source */
+    public 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 };
+
+    public static final int customTlvOffset = OFOUI.length + customTlvSubTypeLength;
     public static final byte chassisIDSubType[] = new byte[] { 4 }; // MAC address for the system
     public static final byte portIDSubType[] = new byte[] { 7 }; // locally assigned
 
+    private static final Logger LOG = LoggerFactory.getLogger(LLDPTLV.class);
+
     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(
@@ -42,7 +57,7 @@ public class LLDPTLV extends Packet {
 
         private byte value;
 
-        private TLVType(byte value) {
+        TLVType(byte value) {
             this.value = value;
         }
 
@@ -55,9 +70,9 @@ public class LLDPTLV extends Packet {
         private static final long serialVersionUID = 1L;
 
         {
-            put(TYPE, new MutablePair<Integer, Integer>(0, 7));
-            put(LENGTH, new MutablePair<Integer, Integer>(7, 9));
-            put(VALUE, new MutablePair<Integer, Integer>(16, 0));
+            put(TYPE, new MutablePair<>(0, 7));
+            put(LENGTH, new MutablePair<>(7, 9));
+            put(VALUE, new MutablePair<>(16, 0));
         }
     };
 
@@ -69,7 +84,7 @@ public class LLDPTLV extends Packet {
      */
     public LLDPTLV() {
         payload = null;
-        fieldValues = new HashMap<String, byte[]>(LLDPTLVFields);
+        fieldValues = new HashMap<>(LLDPTLVFields);
         hdrFieldCoordMap = fieldCoordinates;
         hdrFieldsMap = fieldValues;
     }
@@ -254,16 +269,27 @@ public class LLDPTLV extends Packet {
      * @param portId
      *            port identifier string
      * @return the custom TLV value in byte array
+     * @see {@link #createCustomTLVValue(byte,String)}
      */
     static public byte[] createCustomTLVValue(String customString) {
-        byte[] customArray = customString.getBytes(Charset.defaultCharset());
-        byte[] customValue = new byte[customTlvOffset + customArray.length];
+        byte[] customByteArray = customString.getBytes(Charset.defaultCharset());
+        return createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID, customByteArray);
+    }
+
+    /**
+     * Creates the custom TLV value including OUI, subtype and custom string
+     * @param subtype openflow subtype
+     * @param portId
+     *            port identifier string
+     * @return the custom TLV value in byte array
+     */
+    static public byte[] createCustomTLVValue(byte[] subtype, byte[] customByteArray) {
+        byte[] customValue = new byte[customTlvOffset + customByteArray.length];
 
         System.arraycopy(OFOUI, 0, customValue, 0, OFOUI.length);
-        System.arraycopy(customTlvSubType, 0, customValue, OFOUI.length,
-                customTlvSubType.length);
-        System.arraycopy(customArray, 0, customValue, customTlvOffset,
-                customArray.length);
+        System.arraycopy(subtype, 0, customValue, OFOUI.length, 1);
+        System.arraycopy(customByteArray, 0, customValue, customTlvOffset,
+                customByteArray.length);
 
         return customValue;
     }
@@ -334,4 +360,14 @@ public class LLDPTLV extends Packet {
 
         return customString;
     }
+
+    public static int extractCustomOUI(final LLDPTLV lldptlv) {
+        byte[] value = lldptlv.getValue();
+        return BitBufferHelper.getInt(ArrayUtils.subarray(value, 0, 3));
+    }
+
+    public static byte extractCustomSubtype(final LLDPTLV lldptlv) {
+        byte[] value = lldptlv.getValue();
+        return BitBufferHelper.getByte(ArrayUtils.subarray(value, 3, 4));
+    }
 }