ONOS-3791 NETCONF session factory and exception on device connection

Change-Id: I7c6651a4f76537056a2dc8f94d54818b5b238b9a
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSessionFactory.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSessionFactory.java
new file mode 100644
index 0000000..a899676
--- /dev/null
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSessionFactory.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.netconf;
+
+/**
+ * Abstract interface for the creation of a NETCONF session.
+ */
+public interface NetconfSessionFactory {
+
+    NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo)
+            throws NetconfException;
+}
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 cdcfbf8..251a74b 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
@@ -46,7 +46,7 @@
     public static final Logger log = LoggerFactory
             .getLogger(NetconfControllerImpl.class);
 
-    public Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
+    private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
 
     protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
 
@@ -130,6 +130,4 @@
     public Map<DeviceId, NetconfDevice> getDevicesMap() {
         return netconfDeviceMap;
     }
-
-
 }
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfDeviceImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfDeviceImpl.java
index 5a8f499..bbc3320 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfDeviceImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/NetconfDeviceImpl.java
@@ -20,6 +20,7 @@
 import org.onosproject.netconf.NetconfDeviceInfo;
 import org.onosproject.netconf.NetconfException;
 import org.onosproject.netconf.NetconfSession;
+import org.onosproject.netconf.NetconfSessionFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,12 +36,15 @@
 
     private NetconfDeviceInfo netconfDeviceInfo;
     private boolean deviceState = false;
+    protected NetconfSessionFactory sessionFactory = new SshNetconfSessionFactory();
     private NetconfSession netconfSession;
 
-    public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo) throws NetconfException {
+
+    public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo)
+            throws NetconfException {
         netconfDeviceInfo = deviceInfo;
         try {
-            netconfSession = new NetconfSessionImpl(netconfDeviceInfo);
+            netconfSession = sessionFactory.createNetconfSession(deviceInfo);
         } catch (IOException e) {
             throw new NetconfException("Cannot create connection and session for device " +
                                                deviceInfo, e);
@@ -72,4 +76,13 @@
     public NetconfDeviceInfo getDeviceInfo() {
         return netconfDeviceInfo;
     }
+
+    public class SshNetconfSessionFactory implements NetconfSessionFactory {
+
+        @Override
+        public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
+            return new NetconfSessionImpl(netconfDeviceInfo);
+        }
+    }
+
 }
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 7f544dc..9f72d83 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
@@ -401,6 +401,4 @@
             completedReply.complete(event.getMessagePayload());
         }
     }
-
-
 }
diff --git a/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java b/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java
index ea3bfcc..4901c13 100644
--- a/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java
+++ b/providers/netconf/device/src/main/java/org/onosproject/provider/netconf/device/impl/NetconfDeviceProvider.java
@@ -46,6 +46,7 @@
 import org.onosproject.netconf.NetconfDevice;
 import org.onosproject.netconf.NetconfDeviceInfo;
 import org.onosproject.netconf.NetconfDeviceListener;
+import org.onosproject.netconf.NetconfException;
 import org.slf4j.Logger;
 
 import java.io.IOException;
@@ -190,10 +191,11 @@
                                                                        addr.ip(),
                                                                        addr.port()));
                                      } catch (IOException e) {
-                                         log.info("Can't connect to NETCONF " +
-                                                          "device on {}:{}",
-                                                  addr.ip(),
-                                                  addr.port());
+                                         throw new RuntimeException(
+                                                 new NetconfException(
+                                                         "Can't connect to NETCONF " +
+                                                                 "device on " + addr.ip() +
+                                                                 ":" + addr.port(), e));
                                      }
                                  }
                         );