Merge "Switch initial config files format to xml and add autodetect adapter for confi...
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / osgi / NetconfSSHActivator.java
index 3b513790bd128806a7ef1bec2561bd90fee6420a..1bce3143d5a8fc29831bc5b90a62fdad8d9376ad 100644 (file)
@@ -8,6 +8,8 @@
 package org.opendaylight.controller.netconf.osgi;
 
 import com.google.common.base.Optional;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.net.InetSocketAddress;
 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
@@ -42,7 +44,7 @@ public class NetconfSSHActivator implements BundleActivator{
     ServiceTrackerCustomizer<IUserManager, IUserManager> customizer = new ServiceTrackerCustomizer<IUserManager, IUserManager>(){
         @Override
         public IUserManager addingService(ServiceReference<IUserManager> reference) {
-            logger.info("Service IUserManager added, let there be SSH bridge.");
+            logger.info("Service {} added, let there be SSH bridge.", reference);
             iUserManager =  context.getService(reference);
             try {
                 onUserManagerFound(iUserManager);
@@ -53,13 +55,13 @@ public class NetconfSSHActivator implements BundleActivator{
         }
         @Override
         public void modifiedService(ServiceReference<IUserManager> reference, IUserManager service) {
-            logger.info("Replacing modified service IUserManager in netconf SSH.");
+            logger.info("Replacing modified service {} in netconf SSH.", reference);
             server.addUserManagerService(service);
         }
         @Override
         public void removedService(ServiceReference<IUserManager> reference, IUserManager service) {
-            logger.info("Removing service IUserManager from netconf SSH. " +
-                    "SSH won't authenticate users until IUserManeger service will be started.");
+            logger.info("Removing service {} from netconf SSH. " +
+                    "SSH won't authenticate users until IUserManeger service will be started.", reference);
             removeUserManagerService();
         }
     };
@@ -85,7 +87,28 @@ public class NetconfSSHActivator implements BundleActivator{
                 EXCEPTION_MESSAGE, true);
 
         if (sshSocketAddressOptional.isPresent()){
-            AuthProvider authProvider = new AuthProvider(iUserManager);
+            String path = NetconfConfigUtil.getPrivateKeyPath(context);
+            path = path.replace("\\", "/");
+            if (path.equals("")){
+                throw new Exception("Missing netconf.ssh.pk.path key in configuration file.");
+            }
+            FileInputStream fis = null;
+            try {
+                fis = new FileInputStream(path);
+            } catch (FileNotFoundException e){
+                throw new Exception("Missing file described by netconf.ssh.pk.path key in configuration file.");
+            } catch (SecurityException e){
+                throw new Exception("Read access denied to file described by netconf.ssh.pk.path key in configuration file.");
+            }
+            AuthProvider authProvider = null;
+            try {
+                authProvider = new AuthProvider(iUserManager,fis);
+            } catch (Exception e){
+                if (fis!=null){
+                    fis.close();
+                }
+                throw (e);
+            }
             this.server = NetconfSSHServer.start(sshSocketAddressOptional.get().getPort(),tcpSocketAddress,authProvider);
             Thread serverThread = new  Thread(server,"netconf SSH server thread");
             serverThread.setDaemon(true);