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;
+        }
     }
 
     /**