diff options
Diffstat (limited to 'ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin')
2 files changed, 38 insertions, 19 deletions
diff --git a/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BasicAuthSshClientService.kt b/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BasicAuthSshClientService.kt index 67d5d5153..adbde0f87 100644 --- a/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BasicAuthSshClientService.kt +++ b/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BasicAuthSshClientService.kt @@ -36,6 +36,7 @@ open class BasicAuthSshClientService(private val basicAuthSshClientProperties: B private lateinit var sshClient: SshClient private lateinit var clientSession: ClientSession + var channel: ChannelExec? = null override suspend fun startSessionNB(): ClientSession { sshClient = SshClient.setUpDefaultClient() @@ -69,31 +70,29 @@ open class BasicAuthSshClientService(private val basicAuthSshClientProperties: B override suspend fun executeCommandNB(command: String, timeOut: Long): String { log.debug("Executing host($clientSession) command($command)") - var channel: ChannelExec? = null - try { - channel = clientSession.createExecChannel(command) - //TODO("Convert to streaming ") - val outputStream = ByteArrayOutputStream() - channel.out = outputStream - channel.err = outputStream - channel.open().await() - val waitMask = channel.waitFor(Collections.unmodifiableSet(EnumSet.of(ClientChannelEvent.CLOSED)), timeOut) - if (waitMask.contains(ClientChannelEvent.TIMEOUT)) { - throw BluePrintProcessorException("Failed to retrieve command result in time: $command") - } - val exitStatus = channel.exitStatus - ClientChannel.validateCommandExitStatusCode(command, exitStatus!!) - return outputStream.toString() - } finally { - if (channel != null) - channel.close() + channel = clientSession.createExecChannel(command) + checkNotNull(channel) { "failed to create Channel for the command : $command" } + + //TODO("Convert to streaming ") + val outputStream = ByteArrayOutputStream() + channel!!.out = outputStream + channel!!.err = outputStream + channel!!.open().await() + val waitMask = channel!!.waitFor(Collections.unmodifiableSet(EnumSet.of(ClientChannelEvent.CLOSED)), timeOut) + if (waitMask.contains(ClientChannelEvent.TIMEOUT)) { + throw BluePrintProcessorException("Failed to retrieve command result in time: $command") } + val exitStatus = channel!!.exitStatus + ClientChannel.validateCommandExitStatusCode(command, exitStatus!!) + return outputStream.toString() } override suspend fun closeSessionNB() { + if (channel != null) + channel!!.close() if (sshClient.isStarted) { sshClient.stop() - log.debug("SSH Client Service stopped successfully") } + log.debug("SSH Client Service stopped successfully") } } diff --git a/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BluePrintSshLibPropertyService.kt b/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BluePrintSshLibPropertyService.kt index 9971c500f..1950b71aa 100644 --- a/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BluePrintSshLibPropertyService.kt +++ b/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/service/BluePrintSshLibPropertyService.kt @@ -16,16 +16,23 @@ package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service +import com.fasterxml.jackson.databind.JsonNode import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BasicAuthSshClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.ssh.SshClientProperties import org.onap.ccsdk.cds.blueprintsprocessor.ssh.SshLibConstants import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.stereotype.Service @Service(SshLibConstants.SERVICE_BLUEPRINT_SSH_LIB_PROPERTY) open class BluePrintSshLibPropertyService(private var bluePrintProperties: BluePrintProperties) { + fun blueprintSshClientService(jsonNode: JsonNode): BlueprintSshClientService { + val restClientProperties = sshClientProperties(jsonNode) + return blueprintSshClientService(restClientProperties) + } + fun blueprintSshClientService(selector: String): BlueprintSshClientService { val prefix = "${SshLibConstants.PROPERTY_SSH_CLIENT_PREFIX}$selector" val sshClientProperties = sshClientProperties(prefix) @@ -44,6 +51,19 @@ open class BluePrintSshLibPropertyService(private var bluePrintProperties: BlueP } } + fun sshClientProperties(jsonNode: JsonNode): SshClientProperties { + val type = jsonNode.get("type").textValue() + return when (type) { + SshLibConstants.TYPE_BASIC_AUTH -> { + JacksonUtils.readValue(jsonNode, + BasicAuthSshClientProperties::class.java)!! + } + else -> { + throw BluePrintProcessorException("SSH adaptor($type) is not supported") + } + } + } + private fun blueprintSshClientService(sshClientProperties: SshClientProperties): BlueprintSshClientService { when (sshClientProperties) { |