Added in a second timeout for netconfConnectTimeout

Change-Id: I17c68730b68f7d0db899753dbcea2a570acba8e3
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java
index 0e34a39..00178bc 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfControllerImpl.java
@@ -60,6 +60,12 @@
 @Component(immediate = true)
 @Service
 public class NetconfControllerImpl implements NetconfController {
+    private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5;
+    private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout";
+    @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS,
+            label = "Time (in seconds) to wait for a NETCONF connect.")
+    protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
+
     private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout";
     private static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5;
     @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS,
@@ -103,6 +109,7 @@
     public void modified(ComponentContext context) {
         if (context == null) {
             netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
+            netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
             log.info("No component configuration");
             return;
         }
@@ -110,17 +117,33 @@
         Dictionary<?, ?> properties = context.getProperties();
 
         int newNetconfReplyTimeout;
+        int newNetconfConnectTimeout;
         try {
             String s = get(properties, PROP_NETCONF_REPLY_TIMEOUT);
             newNetconfReplyTimeout = isNullOrEmpty(s) ?
                     netconfReplyTimeout : Integer.parseInt(s.trim());
+
+            s = get(properties, PROP_NETCONF_CONNECT_TIMEOUT);
+            newNetconfConnectTimeout = isNullOrEmpty(s) ?
+                    netconfConnectTimeout : Integer.parseInt(s.trim());
+
         } catch (NumberFormatException e) {
             log.warn("Component configuration had invalid value", e);
             return;
         }
 
+        if (newNetconfConnectTimeout < 0) {
+            log.warn("netconfConnectTimeout is invalid - less than 0");
+            return;
+        } else if (newNetconfReplyTimeout <= 0) {
+            log.warn("netconfReplyTimeout is invalid - 0 or less.");
+            return;
+        }
+
         netconfReplyTimeout = newNetconfReplyTimeout;
-        log.info("Settings: {} = {}", PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
+        netconfConnectTimeout = newNetconfConnectTimeout;
+        log.info("Settings: {} = {}, {} = {}",
+                PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
     }
 
     @Override
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java
index aef6942..b080b4e 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfSessionImpl.java
@@ -50,8 +50,6 @@
     private static final Logger log = LoggerFactory
             .getLogger(NetconfSessionImpl.class);
 
-
-    private static final int CONNECTION_TIMEOUT = 0;
     private static final String ENDPATTERN = "]]>]]>";
     private static final String MESSAGE_ID_STRING = "message-id";
     private static final String HELLO = "<hello";
@@ -111,8 +109,10 @@
     private void startConnection() throws NetconfException {
         if (!connectionActive) {
             netconfConnection = new Connection(deviceInfo.ip().toString(), deviceInfo.port());
+            int connectTimeout = NetconfControllerImpl.netconfConnectTimeout;
+
             try {
-                netconfConnection.connect(null, CONNECTION_TIMEOUT, 5000);
+                netconfConnection.connect(null, 1000 * connectTimeout, 1000 * connectTimeout);
             } catch (IOException e) {
                 throw new NetconfException("Cannot open a connection with device" + deviceInfo, e);
             }
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
index 45304ea..25e14ae 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.onosproject.netconf.ctl;
 
 import org.easymock.EasyMock;
@@ -138,21 +137,28 @@
      */
     @Test
     public void testActivate() {
-        assertEquals("Incorrect NetConf session timeout, should be default",
-                     5, ctrl.netconfReplyTimeout);
+        assertEquals("Incorrect NetConf connect timeout, should be default",
+                     5, ctrl.netconfConnectTimeout);
+        assertEquals("Incorrect NetConf reply timeout, should be default",
+                5, ctrl.netconfReplyTimeout);
         ctrl.activate(null);
-        assertEquals("Incorrect NetConf session timeout, should be default",
-                     5, ctrl.netconfReplyTimeout);
-    }
+        assertEquals("Incorrect NetConf connect timeout, should be default",
+                     5, ctrl.netconfConnectTimeout);
+        assertEquals("Incorrect NetConf reply timeout, should be default",
+                5, ctrl.netconfReplyTimeout);    }
 
     /**
      * Test modification of component configuration.
      */
     @Test
     public void testModified() {
+        assertEquals("Incorrect NetConf connect timeout, should be default",
+                    5, ctrl.netconfConnectTimeout);
         assertEquals("Incorrect NetConf session timeout, should be default",
                      5, ctrl.netconfReplyTimeout);
         ctrl.modified(context);
+        assertEquals("Incorrect NetConf connect timeout, should be default",
+                    2, ctrl.netconfConnectTimeout);
         assertEquals("Incorrect NetConf session timeout",
                      1, ctrl.netconfReplyTimeout);
     }
@@ -388,7 +394,9 @@
 
         @Override
         public Object get(Object key) {
-            if (key.equals("netconfReplyTimeout")) {
+            if (key.equals("netconfConnectTimeout")) {
+                return "2";
+            } else if (key.equals("netconfReplyTimeout")) {
                 return "1";
             }
             return null;