ONOS-4505: Bug Fixes

Change-Id: I190b4e6b950381eef8d6fba1e3ac345ea6854e40
diff --git a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/Controller.java b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/Controller.java
index f16aeb4..7cd7785 100644
--- a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/Controller.java
+++ b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/Controller.java
@@ -112,10 +112,6 @@
             if (isisProcesses.size() > 0) {
                 processes = isisProcesses;
                 connectPeer();
-                //Initializing the interface map in channel handler
-                if (isisChannelHandler != null) {
-                    isisChannelHandler.initializeInterfaceMap();
-                }
             }
         } else {
             isisChannelHandler.updateInterfaceMap(isisProcesses);
@@ -154,7 +150,6 @@
         isisChannelHandler = new IsisChannelHandler(this, processes);
         ChannelPipelineFactory pfact = new IsisPipelineFactory(isisChannelHandler);
         peerBootstrap.setPipelineFactory(pfact);
-        ChannelFuture connection = peerBootstrap.connect(new InetSocketAddress(IsisConstants.SHOST, isisPort.toInt()));
     }
 
     /**
@@ -499,6 +494,7 @@
         public void run() {
             log.debug("Connect to peer {}", IsisConstants.SHOST);
             initConnection();
+            isisChannelHandler.sentConfigPacket(configPacket);
             InetSocketAddress connectToSocket = new InetSocketAddress(IsisConstants.SHOST, isisPort.toInt());
             try {
                 peerBootstrap.connect(connectToSocket).addListener(new ChannelFutureListener() {
@@ -517,13 +513,13 @@
                             }
                             scheduleConnectionRetry(connectRetryTime);
                         } else {
+                            //Send the config packet
+                            isisChannelHandler.sentConfigPacket(configPacket);
                             connectRetryCounter++;
                             log.info("Connected to remote host {}, Connect Counter {}", IsisConstants.SHOST,
                                      connectRetryCounter);
                             disconnectExecutor();
-                            isisChannelHandler.initializeInterfaceMap();
-                            //Send the config packet
-                            isisChannelHandler.sentConfigPacket(configPacket);
+
                             return;
                         }
                     }
diff --git a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisInterface.java b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisInterface.java
index 134a48b..864b6e5 100644
--- a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisInterface.java
+++ b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/DefaultIsisInterface.java
@@ -88,6 +88,7 @@
     private IsisLsdb isisLsdb = null;
     private List<Ip4Address> allConfiguredInterfaceIps = null;
     private Channel channel;
+    private boolean helloSenderStarted = false;
 
     /**
      * Returns ISIS LSDB instance.
@@ -1103,12 +1104,14 @@
      */
     public void startHelloSender(Channel channel) {
         log.debug("IsisInterfaceImpl::startHelloSender");
-
-        isisHelloPduSender = new IsisHelloPduSender(channel, this);
-        exServiceHello = Executors.newSingleThreadScheduledExecutor();
-        final ScheduledFuture<?> helloHandle =
-                exServiceHello.scheduleAtFixedRate(isisHelloPduSender, 0,
-                                                   helloInterval, TimeUnit.SECONDS);
+        if (!helloSenderStarted) {
+            isisHelloPduSender = new IsisHelloPduSender(channel, this);
+            exServiceHello = Executors.newSingleThreadScheduledExecutor();
+            final ScheduledFuture<?> helloHandle =
+                    exServiceHello.scheduleAtFixedRate(isisHelloPduSender, 0,
+                                                       helloInterval, TimeUnit.SECONDS);
+            helloSenderStarted = true;
+        }
     }
 
     /**
@@ -1117,5 +1120,6 @@
     public void stopHelloSender() {
         log.debug("IsisInterfaceImpl::stopHelloSender");
         exServiceHello.shutdown();
+        helloSenderStarted = false;
     }
 }
\ No newline at end of file
diff --git a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/IsisChannelHandler.java b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/IsisChannelHandler.java
index 86c6853..73a5626 100644
--- a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/IsisChannelHandler.java
+++ b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/IsisChannelHandler.java
@@ -50,7 +50,7 @@
 
     private static final Logger log = LoggerFactory.getLogger(IsisChannelHandler.class);
     private static Map<Integer, Object> isisDb = null;
-    private Channel channel;
+    private Channel channel = null;
     private Controller controller;
     private List<IsisProcess> processes = null;
     private List<ScheduledExecutorService> executorList = new ArrayList<>();
@@ -76,8 +76,11 @@
     public void initializeInterfaceMap() {
         for (IsisProcess process : processes) {
             for (IsisInterface isisInterface : process.isisInterfaceList()) {
-                isisInterfaceMap.put(isisInterface.interfaceIndex(), isisInterface);
-                interfaceIps.add(isisInterface.interfaceIpAddress());
+                IsisInterface anInterface = isisInterfaceMap.get(isisInterface.interfaceIndex());
+                if (anInterface == null) {
+                    isisInterfaceMap.put(isisInterface.interfaceIndex(), isisInterface);
+                    interfaceIps.add(isisInterface.interfaceIpAddress());
+                }
             }
         }
         //Initializes the interface with all interface ip details - for ls pdu generation
@@ -150,6 +153,7 @@
                               + configPacket.length);
             sentConfigPacket(configPacket);
         }
+        initializeInterfaceMap();
         //start the hello timer
         startHelloSender();
         //Initialize Database
@@ -168,6 +172,7 @@
         log.debug("IsisChannelHandler::channelDisconnected...!!!");
         if (controller != null) {
             controller.connectPeer();
+            stopHelloSender();
         }
     }
 
@@ -255,8 +260,11 @@
      */
     public void stopHelloSender() {
         log.debug("ISISChannelHandler::stopHelloTimer ");
-        for (ScheduledExecutorService exServiceHello : executorList) {
-            exServiceHello.shutdown();
+        log.debug("IsisController::startHelloSender");
+        Set<Integer> interfaceIndexes = isisInterfaceMap.keySet();
+        for (Integer interfaceIndex : interfaceIndexes) {
+            IsisInterface isisInterface = isisInterfaceMap.get(interfaceIndex);
+            isisInterface.stopHelloSender();
         }
     }
 
diff --git a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdbAge.java b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdbAge.java
index 5a48798..9ceb97c 100644
--- a/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdbAge.java
+++ b/protocols/isis/ctl/src/main/java/org/onosproject/isis/controller/impl/lsdb/DefaultIsisLsdbAge.java
@@ -44,6 +44,7 @@
     private int ageCounterRollOver = 0;
     private IsisLspQueueConsumer queueConsumer = null;
     private BlockingQueue<LspWrapper> lsaQueue = new ArrayBlockingQueue<>(1024);
+    private boolean timerStarted = false;
 
     /**
      * Creates an instance of LSDB age.
@@ -128,9 +129,12 @@
      * Starts the aging timer and queue consumer.
      */
     public void startDbAging() {
-        startDbAgeTimer();
-        queueConsumer = new IsisLspQueueConsumer(lsaQueue);
-        new Thread(queueConsumer).start();
+        if (!timerStarted) {
+            startDbAgeTimer();
+            queueConsumer = new IsisLspQueueConsumer(lsaQueue);
+            new Thread(queueConsumer).start();
+            timerStarted = true;
+        }
     }
 
     /**
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/Csnp.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/Csnp.java
index 80dc5b1..685be07 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/Csnp.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/Csnp.java
@@ -188,7 +188,9 @@
             TlvType tlvValue = TlvType.get(tlvHeader.tlvType());
             if (tlvValue != null) {
                 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
-                this.variableLengths.add(tlv);
+                if (tlv != null) {
+                    this.variableLengths.add(tlv);
+                }
             } else {
                 channelBuffer.readBytes(tlvHeader.tlvLength());
             }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/L1L2HelloPdu.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/L1L2HelloPdu.java
index 9737e1a..dd5eb7f 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/L1L2HelloPdu.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/L1L2HelloPdu.java
@@ -145,7 +145,9 @@
             TlvType tlvType = TlvType.get(tlvHeader.tlvType());
             if (tlvType != null) {
                 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
-                this.variableLengths.add(tlv);
+                if (tlv != null) {
+                    this.variableLengths.add(tlv);
+                }
             } else {
                 channelBuffer.readBytes(tlvHeader.tlvLength());
             }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/LsPdu.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/LsPdu.java
index f6bd85c..b7c40a3 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/LsPdu.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/LsPdu.java
@@ -354,7 +354,9 @@
             TlvType tlvValue = TlvType.get(tlvHeader.tlvType());
             if (tlvValue != null) {
                 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
-                this.variableLengths.add(tlv);
+                if (tlv != null) {
+                    this.variableLengths.add(tlv);
+                }
             } else {
                 channelBuffer.readBytes(tlvHeader.tlvLength());
             }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/P2PHelloPdu.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/P2PHelloPdu.java
index e236610..cb5bd05 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/P2PHelloPdu.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/pdu/P2PHelloPdu.java
@@ -124,7 +124,9 @@
             TlvType tlvType = TlvType.get(tlvHeader.tlvType());
             if (tlvType != null) {
                 IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
-                this.variableLengths.add(tlv);
+                if (tlv != null) {
+                    this.variableLengths.add(tlv);
+                }
             } else {
                 channelBuffer.readBytes(tlvHeader.tlvLength());
             }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IpExtendedReachabilityTlv.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IpExtendedReachabilityTlv.java
index f1549e4..90b79cc 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IpExtendedReachabilityTlv.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IpExtendedReachabilityTlv.java
@@ -205,8 +205,11 @@
                 tlvHeader.setTlvLength(channelBuffer.readByte());
                 SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
                 if (tlvValue != null) {
-                    this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
-                                                           channelBuffer.readBytes(tlvHeader.tlvLength())));
+                    TrafficEngineeringSubTlv subTlv =
+                            SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
+                    if (subTlv != null) {
+                        this.addSubTlv(subTlv);
+                    }
                 } else {
                     channelBuffer.readBytes(tlvHeader.tlvLength());
                 }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java
index b2eb6c9..d8e75b1 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java
@@ -49,9 +49,10 @@
         this.neighbors.add(neighbor);
     }
 
+
     @Override
     public void readFrom(ChannelBuffer channelBuffer) {
-        while (channelBuffer.readableBytes() >= IsisUtil.EIGHT_BYTES + IsisUtil.THREE_BYTES) {
+        while (channelBuffer.readableBytes() >= (IsisUtil.EIGHT_BYTES + IsisUtil.THREE_BYTES)) {
             NeighborForExtendedIs extendedIs = new NeighborForExtendedIs();
             extendedIs.readFrom(channelBuffer);
             this.addNeighbor(extendedIs);
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/LspEntriesTlv.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/LspEntriesTlv.java
index e43d4b4..02199d4 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/LspEntriesTlv.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/LspEntriesTlv.java
@@ -18,6 +18,7 @@
 import com.google.common.base.MoreObjects;
 import com.google.common.primitives.Bytes;
 import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.isis.io.util.IsisUtil;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -58,9 +59,9 @@
 
     @Override
     public void readFrom(ChannelBuffer channelBuffer) {
-        while (channelBuffer.readableBytes() >= 16) {
+        while (channelBuffer.readableBytes() >= (IsisUtil.EIGHT_BYTES * 2)) {
             LspEntry lspEntry = new LspEntry();
-            lspEntry.readFrom(channelBuffer.readBytes(16));
+            lspEntry.readFrom(channelBuffer.readBytes(IsisUtil.EIGHT_BYTES * 2));
             lspEntryList.add(lspEntry);
         }
     }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java
index 88b8462..0bc57b4 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java
@@ -101,8 +101,11 @@
                 int tlvLength = tlvHeader.tlvLength();
                 if (tlvValue != null) {
                     if (channelBuffer.readableBytes() >= tlvLength) {
-                        this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
-                                                               channelBuffer.readBytes(tlvHeader.tlvLength())));
+                        TrafficEngineeringSubTlv subTlv =
+                                SubTlvFinder.findSubTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength()));
+                        if (subTlv != null) {
+                            this.addSubTlv(subTlv);
+                        }
                     }
                 } else {
                     if (channelBuffer.readableBytes() >= tlvLength) {