ONOS-4086 to ONOS-4091, ONOS-4098 to ONOS-4100:JUNIT for ISIS controller

Change-Id: If3501a55fcbf994cd69facfd97f43b4a4f0f7812
diff --git a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisNeighbor.java b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisNeighbor.java
index 3180c5d..7fac0cb 100755
--- a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisNeighbor.java
+++ b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisNeighbor.java
@@ -45,7 +45,8 @@
     private String neighborSystemId;
     private Ip4Address interfaceIp;
     private MacAddress neighborMacAddress;
-    private int holdingTime;
+    private volatile int holdingTime;
+    private int neighborDownInterval;
     private IsisRouterType routerType;
     private String l1LanId;
     private String l2LanId;
@@ -54,6 +55,8 @@
     private IsisInterfaceState neighborState = IsisInterfaceState.INITIAL;
     private InternalInactivityTimeCheck inActivityTimeCheckTask;
     private ScheduledExecutorService exServiceInActivity;
+    private InternalHoldingTimeCheck holdingTimeCheckTask;
+    private ScheduledExecutorService exServiceHoldingTimeCheck;
     private boolean inActivityTimerScheduled = false;
     private IsisInterface isisInterface;
 
@@ -72,6 +75,7 @@
         this.interfaceIp = (helloMessage.interfaceIpAddresses() != null) ?
                 interfaceIpAddresses.get(0) : IsisConstants.DEFAULTIP;
         this.holdingTime = helloMessage.holdingTime();
+        neighborDownInterval = holdingTime;
         this.routerType = IsisRouterType.get(helloMessage.circuitType());
         if (helloMessage instanceof L1L2HelloPdu) {
             if (IsisPduType.L1HELLOPDU == helloMessage.isisPduType()) {
@@ -83,6 +87,7 @@
             this.localCircuitId = ((P2PHelloPdu) helloMessage).localCircuitId();
         }
         this.isisInterface = isisInterface;
+        startHoldingTimeCheck();
     }
 
     /**
@@ -244,7 +249,7 @@
      * @param l2LanId L2 lan ID
      */
     public void setL2LanId(String l2LanId) {
-        this.l1LanId = l1LanId;
+        this.l2LanId = l2LanId;
     }
 
     /**
@@ -293,6 +298,25 @@
     }
 
     /**
+     * Starts the holding time check timer.
+     */
+    public void startHoldingTimeCheck() {
+        log.debug("IsisNeighbor::startHoldingTimeCheck");
+        holdingTimeCheckTask = new InternalHoldingTimeCheck();
+        exServiceHoldingTimeCheck = Executors.newSingleThreadScheduledExecutor();
+        exServiceHoldingTimeCheck.scheduleAtFixedRate(holdingTimeCheckTask, 1,
+                                                      1, TimeUnit.SECONDS);
+    }
+
+    /**
+     * Stops the holding time check timer.
+     */
+    public void stopHoldingTimeCheck() {
+        log.debug("IsisNeighbor::stopHoldingTimeCheck ");
+        exServiceHoldingTimeCheck.shutdown();
+    }
+
+    /**
      * Starts the inactivity timer.
      */
     public void startInactivityTimeCheck() {
@@ -300,8 +324,8 @@
             log.debug("IsisNeighbor::startInactivityTimeCheck");
             inActivityTimeCheckTask = new InternalInactivityTimeCheck();
             exServiceInActivity = Executors.newSingleThreadScheduledExecutor();
-            exServiceInActivity.scheduleAtFixedRate(inActivityTimeCheckTask, holdingTime,
-                                                    holdingTime, TimeUnit.SECONDS);
+            exServiceInActivity.scheduleAtFixedRate(inActivityTimeCheckTask, neighborDownInterval,
+                                                    neighborDownInterval, TimeUnit.SECONDS);
             inActivityTimerScheduled = true;
         }
     }
@@ -328,6 +352,8 @@
         isisInterface.setL2LanId(IsisConstants.DEFAULTLANID);
 
         neighborState = IsisInterfaceState.DOWN;
+        stopInactivityTimeCheck();
+        stopHoldingTimeCheck();
         isisInterface.removeNeighbor(this);
     }
 
@@ -347,4 +373,20 @@
             neighborDown();
         }
     }
+
+    /**
+     * Represents a Task which will decrement holding time for this neighbor.
+     */
+    private class InternalHoldingTimeCheck implements Runnable {
+        /**
+         * Creates an instance.
+         */
+        InternalHoldingTimeCheck() {
+        }
+
+        @Override
+        public void run() {
+            holdingTime--;
+        }
+    }
 }
\ No newline at end of file
diff --git a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdb.java b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdb.java
index 33a0a17..8a58269 100755
--- a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdb.java
+++ b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdb.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.isis.controller.impl.lsdb;
 
+import org.jboss.netty.buffer.ChannelBuffers;
 import org.onosproject.isis.controller.IsisInterface;
 import org.onosproject.isis.controller.IsisLsdb;
 import org.onosproject.isis.controller.IsisLsdbAge;
@@ -24,6 +25,7 @@
 import org.onosproject.isis.controller.LspWrapper;
 import org.onosproject.isis.io.isispacket.pdu.LsPdu;
 import org.onosproject.isis.io.util.IsisConstants;
+import org.onosproject.isis.io.util.IsisUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,6 +43,9 @@
     private Map<String, LspWrapper> isisL1Db = new ConcurrentHashMap<>();
     private Map<String, LspWrapper> isisL2Db = new ConcurrentHashMap<>();
     private IsisLsdbAge lsdbAge = null;
+
+
+
     private int l1LspSeqNo = IsisConstants.STARTLSSEQUENCENUM;
     private int l2LspSeqNo = IsisConstants.STARTLSSEQUENCENUM;
 
@@ -59,6 +64,23 @@
     }
 
     /**
+     * Sets the level 1 link state sequence number.
+     *
+     * @param l1LspSeqNo link state sequence number
+     */
+    public void setL1LspSeqNo(int l1LspSeqNo) {
+        this.l1LspSeqNo = l1LspSeqNo;
+    }
+
+    /**
+     * Sets the level 2 link state sequence number.
+     *
+     * @param l2LspSeqNo link state sequence number
+     */
+    public void setL2LspSeqNo(int l2LspSeqNo) {
+        this.l2LspSeqNo = l2LspSeqNo;
+    }
+    /**
      * Returns the LSDB LSP key.
      *
      * @param systemId system ID
@@ -108,7 +130,7 @@
      * @return List of LSPs
      */
     public List<LspWrapper> allLspHeaders(boolean excludeMaxAgeLsp) {
-        List<LspWrapper> summaryList = new CopyOnWriteArrayList();
+        List<LspWrapper> summaryList = new CopyOnWriteArrayList<>();
         addLspToHeaderList(summaryList, excludeMaxAgeLsp, isisL1Db);
         addLspToHeaderList(summaryList, excludeMaxAgeLsp, isisL2Db);
 
@@ -187,9 +209,17 @@
      */
     public boolean addLsp(IsisMessage isisMessage, boolean isSelfOriginated, IsisInterface isisInterface) {
         LsPdu lspdu = (LsPdu) isisMessage;
+        if (isSelfOriginated) {
+            //Add length and checksum
+            byte[] lspBytes = lspdu.asBytes();
+            lspdu.setPduLength(lspBytes.length);
+            lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION,
+                                            IsisConstants.CHECKSUMPOSITION + 1);
+            byte[] checkSum = {lspBytes[IsisConstants.CHECKSUMPOSITION], lspBytes[IsisConstants.CHECKSUMPOSITION + 1]};
+            lspdu.setCheckSum(ChannelBuffers.copiedBuffer(checkSum).readUnsignedShort());
+        }
         DefaultLspWrapper lspWrapper = new DefaultLspWrapper();
         lspWrapper.setLspAgeReceived(IsisConstants.LSPMAXAGE - lspdu.remainingLifeTime());
-        lspWrapper.setRemainingLifetime(IsisConstants.LSPMAXAGE - lsdbAge.ageCounter());
         lspWrapper.setLspType(IsisPduType.get(lspdu.pduType()));
         lspWrapper.setLsPdu(lspdu);
         lspWrapper.setAgeCounterWhenReceived(lsdbAge.ageCounter());
@@ -198,7 +228,6 @@
         lspWrapper.setIsisInterface(isisInterface);
         lspWrapper.setLsdbAge(lsdbAge);
         addLsp(lspWrapper, lspdu.lspId());
-
         log.debug("Added LSp In LSDB: {}", lspWrapper);
 
         return true;
@@ -217,9 +246,11 @@
 
         switch (lspWrapper.lsPdu().isisPduType()) {
             case L1LSPDU:
+                isisL1Db.remove(key);
                 isisL1Db.put(key, lspWrapper);
                 break;
             case L2LSPDU:
+                isisL2Db.remove(key);
                 isisL2Db.put(key, lspWrapper);
                 break;
             default:
@@ -228,7 +259,7 @@
         }
 
         //add it to bin
-        Integer binNumber = lsdbAge.age2Bin(IsisConstants.LSPMAXAGE - lspWrapper.remainingLifetime());
+        Integer binNumber = lsdbAge.age2Bin(IsisConstants.LSPMAXAGE - lspWrapper.lspAgeReceived());
         IsisLspBin lspBin = lsdbAge.getLspBin(binNumber);
         if (lspBin != null) {
             //remove from existing
@@ -264,7 +295,8 @@
     public String isNewerOrSameLsp(IsisMessage lsp1, IsisMessage lsp2) {
         LsPdu receivedLsp = (LsPdu) lsp1;
         LsPdu lspFromDb = (LsPdu) lsp2;
-        if (receivedLsp.sequenceNumber() > lspFromDb.sequenceNumber()) {
+        if (receivedLsp.sequenceNumber() > lspFromDb.sequenceNumber() ||
+                receivedLsp.checkSum() != lspFromDb.checkSum()) {
             return "latest";
         } else if (receivedLsp.sequenceNumber() < lspFromDb.sequenceNumber()) {
             return "old";
@@ -312,4 +344,4 @@
                 break;
         }
     }
-}
\ No newline at end of file
+}