connect netconf devices through ssh key
Change-Id: I1a0961ffffd33559f903ead0634dbb5492e9a154
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceInfo.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceInfo.java
index 4c900ea..2468562 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceInfo.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfDeviceInfo.java
@@ -22,7 +22,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
@@ -39,7 +38,7 @@
private String password;
private IpAddress ipAddress;
private int port;
- private File keyFile;
+ private char[] key;
private DeviceId deviceId;
@@ -80,7 +79,7 @@
this.password = password;
this.ipAddress = ipAddress;
this.port = port;
- this.keyFile = new File(keyString);
+ this.key = keyString.toCharArray();
}
/**
@@ -120,12 +119,12 @@
}
/**
- * Exposes the keyFile of the controller.
+ * Exposes the key of the controller.
*
* @return int port address
*/
- public File getKeyFile() {
- return keyFile;
+ public char[] getKey() {
+ return key;
}
/**
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 00178bc..afe791c 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
@@ -26,9 +26,11 @@
import org.apache.felix.scr.annotations.Service;
import org.onlab.packet.IpAddress;
import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.key.DeviceKey;
import org.onosproject.net.key.DeviceKeyId;
import org.onosproject.net.key.DeviceKeyService;
import org.onosproject.net.key.UsernamePassword;
@@ -201,15 +203,31 @@
}
}
try {
- UsernamePassword deviceKey = deviceKeyService.getDeviceKey(
- DeviceKeyId.deviceKeyId(deviceId.toString())).asUsernamePassword();
+ DeviceKey deviceKey = deviceKeyService.getDeviceKey(
+ DeviceKeyId.deviceKeyId(deviceId.toString()));
+ NetconfDeviceInfo deviceInfo = null;
+ if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
+ UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
- NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(deviceKey.username(),
- deviceKey.password(),
- IpAddress.valueOf(ip),
- port);
+ deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
+ usernamepasswd.password(),
+ IpAddress.valueOf(ip),
+ port);
+
+ } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
+ String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
+ String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
+ String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
+
+ deviceInfo = new NetconfDeviceInfo(username,
+ password,
+ IpAddress.valueOf(ip),
+ port,
+ sshkey);
+ } else {
+ log.error("Unknown device key for device {}", deviceId);
+ }
NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
-
netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
return netconfDevicedevice;
} catch (NullPointerException e) {
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 59cc125..451a2a1 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
@@ -118,12 +118,14 @@
}
boolean isAuthenticated;
try {
- if (deviceInfo.getKeyFile() != null) {
+ if (deviceInfo.getKey() != null) {
+ log.debug("Authenticating with key to device {} with username {}",
+ deviceInfo.getDeviceId(), deviceInfo.name());
isAuthenticated = netconfConnection.authenticateWithPublicKey(
- deviceInfo.name(), deviceInfo.getKeyFile(),
- deviceInfo.password());
+ deviceInfo.name(), deviceInfo.getKey(),
+ deviceInfo.password().equals("") ? null : deviceInfo.password());
} else {
- log.debug("Authenticating to device {} with username {}",
+ log.debug("Authenticating to device {} with username {} with password",
deviceInfo.getDeviceId(), deviceInfo.name());
isAuthenticated = netconfConnection.authenticateWithPassword(
deviceInfo.name(), deviceInfo.password());