Adj-In RIB code fix

Change-Id: Ice870ab1aff068cc27a5337a3f4ff7090a53d84d
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java
old mode 100755
new mode 100644
index 9b14b4b..3967a22
--- a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/linkstate/BgpLinkLSIdentifier.java
@@ -18,6 +18,7 @@
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.ListIterator;
 import java.util.Objects;
 
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -28,6 +29,7 @@
 import org.onosproject.bgpio.types.IPv6AddressTlv;
 import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
 import org.onosproject.bgpio.types.attr.BgpAttrNodeMultiTopologyId;
+import org.onosproject.bgpio.util.Constants;
 import org.onosproject.bgpio.util.UnSupportedAttribute;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,13 +40,12 @@
 /**
  * Implementation of local node descriptors, remote node descriptors and link descriptors.
  */
-public class BgpLinkLSIdentifier {
+public class BgpLinkLSIdentifier implements Comparable<Object> {
     private static final Logger log = LoggerFactory.getLogger(BgpLinkLSIdentifier.class);
     public static final short IPV4_INTERFACE_ADDRESS_TYPE = 259;
     public static final short IPV4_NEIGHBOR_ADDRESS_TYPE = 260;
     public static final short IPV6_INTERFACE_ADDRESS_TYPE = 261;
     public static final short IPV6_NEIGHBOR_ADDRESS_TYPE = 262;
-    public static final int TYPE_AND_LEN = 4;
 
     private NodeDescriptors localNodeDescriptors;
     private NodeDescriptors remoteNodeDescriptors;
@@ -107,12 +108,13 @@
      */
     public static NodeDescriptors parseNodeDescriptors(ChannelBuffer cb, short desType, byte protocolId)
             throws BgpParseException {
-        ChannelBuffer tempBuf = cb;
+        log.debug("parse Node descriptors");
+        ChannelBuffer tempBuf = cb.copy();
         short type = cb.readShort();
         short length = cb.readShort();
         if (cb.readableBytes() < length) {
             throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
-                    tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
+                    tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN_AS_SHORT));
         }
         NodeDescriptors nodeIdentifier = new NodeDescriptors();
         ChannelBuffer tempCb = cb.readBytes(length);
@@ -138,12 +140,12 @@
         int count = 0;
 
         while (cb.readableBytes() > 0) {
-            ChannelBuffer tempBuf = cb;
+            ChannelBuffer tempBuf = cb.copy();
             short type = cb.readShort();
             short length = cb.readShort();
             if (cb.readableBytes() < length) {
                 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
-                        tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
+                        tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN_AS_SHORT));
             }
             ChannelBuffer tempCb = cb.readBytes(length);
             switch (type) {
@@ -170,7 +172,7 @@
                     //length + 4 implies data contains type, length and value
                     throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                             BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, tempBuf.readBytes(length
-                                    + TYPE_AND_LEN));
+                                    + Constants.TYPE_AND_LEN_AS_SHORT));
                 }
                 break;
             default:
@@ -231,8 +233,12 @@
             } else {
                 while (objListIterator.hasNext() && isCommonSubTlv) {
                     BgpValueType subTlv = objListIterator.next();
-                    isCommonSubTlv = Objects.equals(linkDescriptor.contains(subTlv),
-                            other.linkDescriptor.contains(subTlv));
+                    if (linkDescriptor.contains(subTlv) && other.linkDescriptor.contains(subTlv)) {
+                        isCommonSubTlv = Objects.equals(linkDescriptor.get(linkDescriptor.indexOf(subTlv)),
+                                         other.linkDescriptor.get(other.linkDescriptor.indexOf(subTlv)));
+                    } else {
+                        isCommonSubTlv = false;
+                    }
                 }
                 return isCommonSubTlv && Objects.equals(this.localNodeDescriptors, other.localNodeDescriptors)
                         && Objects.equals(this.remoteNodeDescriptors, other.remoteNodeDescriptors);
@@ -249,4 +255,42 @@
                 .add("linkDescriptor", linkDescriptor)
                 .toString();
     }
+
+    @Override
+    public int compareTo(Object o) {
+        if (this.equals(o)) {
+            return 0;
+        }
+        int result = this.localNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).localNodeDescriptors);
+        if (result != 0) {
+            return result;
+        } else if (this.remoteNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).remoteNodeDescriptors) != 0) {
+            return this.remoteNodeDescriptors.compareTo(((BgpLinkLSIdentifier) o).remoteNodeDescriptors);
+        } else {
+            int countOtherSubTlv = ((BgpLinkLSIdentifier) o).linkDescriptor.size();
+            int countObjSubTlv = linkDescriptor.size();
+            if (countOtherSubTlv != countObjSubTlv) {
+                if (countOtherSubTlv > countObjSubTlv) {
+                    return 1;
+                } else {
+                    return -1;
+                }
+           }
+            ListIterator<BgpValueType> listIterator = linkDescriptor.listIterator();
+            ListIterator<BgpValueType> listIteratorOther = ((BgpLinkLSIdentifier) o).linkDescriptor.listIterator();
+            while (listIterator.hasNext()) {
+                BgpValueType tlv = listIterator.next();
+                BgpValueType tlv1 = listIteratorOther.next();
+                if (linkDescriptor.contains(tlv) && ((BgpLinkLSIdentifier) o).linkDescriptor.contains(tlv1)) {
+                    int res = linkDescriptor.get(linkDescriptor.indexOf(tlv)).compareTo(
+                            ((BgpLinkLSIdentifier) o).linkDescriptor.get(((BgpLinkLSIdentifier) o).linkDescriptor
+                                    .indexOf(tlv1)));
+                    if (res != 0) {
+                        return res;
+                    }
+                }
+            }
+        }
+        return 0;
+    }
 }