diff options
author | Cherukuri, Venkatanaresh (vn166g) <vn166g@att.com> | 2019-02-11 10:36:36 -0500 |
---|---|---|
committer | Cherukuri, Venkatanaresh (vn166g) <vn166g@att.com> | 2019-02-11 10:36:36 -0500 |
commit | d4b9a331a88feea62872c4ae85f4ae1b738c6f60 (patch) | |
tree | fcce9a8e2258b41f13ae5bef6a3172488ab94c06 /ms/blueprintsprocessor/functions | |
parent | 8d865c28451d63815fb1fd5d1a52800ee33dde91 (diff) |
Blueprint modeled Netconf capability components
Adding Netconf Executor Function module
Change-Id: I8b896fef84a465db2b9365d038b611e9fdf793ea
Issue-ID: CCSDK-790
Signed-off-by: Cherukuri, Venkatanaresh (vn166g) <vn166g@att.com>
Diffstat (limited to 'ms/blueprintsprocessor/functions')
30 files changed, 2269 insertions, 123 deletions
diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt index e0fd7350..4612ddaf 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt @@ -17,6 +17,7 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.ComponentJythonExecutor import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.PythonExecutorProperty import org.slf4j.LoggerFactory @@ -30,9 +31,18 @@ open class ComponentNetconfExecutor(private val netconfExecutorConfiguration: Ne private val pythonExecutorProperty: PythonExecutorProperty) : ComponentJythonExecutor(pythonExecutorProperty) { - private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java) + private val log = LoggerFactory.getLogger(ComponentNetconfExecutor::class.java) + lateinit var deviceInfo: DeviceInfo + override fun process(executionServiceInput: ExecutionServiceInput) { - super.process(executionServiceInput) + + super.process(executionServiceInput) + + + } + + fun setdeviceInfo(deviceInfo: DeviceInfo) { + this.deviceInfo = deviceInfo } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfException.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfException.kt new file mode 100644 index 00000000..37aa63da --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfException.kt @@ -0,0 +1,45 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor + +import java.io.IOException + +class NetconfException : IOException { + + var code: Int = 100 + + constructor(cause: Throwable) : super(cause) + constructor(message: String) : super(message) + constructor(message: String, cause: Throwable) : super(message, cause) + constructor(cause: Throwable, message: String, vararg args: Any?) : super(String.format(message, *args), cause) + + constructor(code: Int, cause: Throwable) : super(cause) { + this.code = code + } + + constructor(code: Int, message: String) : super(message) { + this.code = code + } + + constructor(code: Int, message: String, cause: Throwable) : super(message, cause) { + this.code = code + } + + constructor(code: Int, cause: Throwable, message: String, vararg args: Any?) + : super(String.format(message, *args), cause) { + this.code = code + } +} diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfRpcService.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfRpcService.kt index 5f1b38da..d5385054 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfRpcService.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfRpcService.kt @@ -16,31 +16,380 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor +import com.fasterxml.jackson.databind.JsonNode +import com.google.common.base.Preconditions +import org.apache.commons.collections.CollectionUtils import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.core.NetconfSessionFactory +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.DeviceResponse +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfAdaptorConstant import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.NetconfRpcClientService import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.NetconfSession +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils.RpcMessageUtils +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils +import org.slf4j.LoggerFactory import org.springframework.beans.factory.config.ConfigurableBeanFactory import org.springframework.context.annotation.Scope import org.springframework.stereotype.Service +import java.util.* +import java.util.concurrent.TimeUnit + @Service("netconf-rpc-service") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -class NetconfRpcService { +class NetconfRpcService : NetconfRpcClientService { + + val log = LoggerFactory.getLogger(NetconfRpcService::class.java) lateinit var deviceInfo: DeviceInfo lateinit var netconfSession: NetconfSession + private val applyConfigIds = ArrayList<String>() + private val recordedApplyConfigIds = ArrayList<String>() + private val DEFAULT_MESSAGE_TIME_OUT = 30 + + @Throws(NetconfException::class) + fun NetconfRpcService(capabilityProperty: MutableMap<String, JsonNode> ) { + try { + Preconditions.checkNotNull(capabilityProperty, "missing netconfDeviceInfo in netconf rpc client") + connect(getNetconfDeviceInfo(capabilityProperty)) + log.info("NetconfRpcService initialised with deviceInfo {}", deviceInfo) + //configPersistService = ConfigPersistService(configResourceService) + + } catch (e: NetconfException) { + publishMessage(String.format("Netconf Device Connection Failed, %s", e.message)) + throw NetconfException("Netconf Device Connection Failed,$deviceInfo",e) + } + + } + + fun setdeviceInfo(deviceInfo: DeviceInfo) { + this.deviceInfo = deviceInfo + } + + fun getNetconfDeviceInfo(capabilityProperty: MutableMap<String, JsonNode> ):DeviceInfo{ + val netconfDeviceInfo = JacksonUtils.getInstanceFromMap(capabilityProperty, DeviceInfo::class.java) + this.deviceInfo = netconfDeviceInfo + return netconfDeviceInfo + } + fun connect(netconfDeviceInfo: DeviceInfo) { + log.info("in the connect method") + setdeviceInfo(netconfDeviceInfo) netconfSession = NetconfSessionFactory.instance("DEFAULT_NETCONF_SESSION", netconfDeviceInfo) - // TODO + publishMessage("Netconf Device Connection Established"); + } - fun disconnect() { + override fun disconnect() { netconfSession.close() } - fun reconnect() { + override fun reconnect() { disconnect() connect(deviceInfo) } + + override fun getConfig(messageId: String, messageContent: String, configTarget: String, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + log.info("in the NetconfRpcService "+messageId) + try { + val message = RpcMessageUtils.getConfig(messageId, configTarget, messageContent) + output = asyncRpc(message, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = e.message + } + + return output + } + + override fun deleteConfig(messageId: String, configTarget: String, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val deleteConfigMessage = RpcMessageUtils.deleteConfig(messageId, configTarget) + output.requestMessage = deleteConfigMessage + output = asyncRpc(deleteConfigMessage, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = "failed in delete config command " + e.message + } + + return output + } + + override fun lock(messageId: String, configTarget: String, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val lockMessage = RpcMessageUtils.lock(messageId, configTarget) + output.requestMessage = lockMessage + output = asyncRpc(lockMessage, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = "failed in lock command " + e.message + } + + return output + } + + override fun unLock(messageId: String, configTarget: String, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val unlockMessage = RpcMessageUtils.unlock(messageId, configTarget) + output.requestMessage = unlockMessage + output = asyncRpc(unlockMessage, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = "failed in lock command " + e.message + } + + return output + } + + override fun commit(messageId: String, message: String, discardChanges: Boolean, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val messageContent = RpcMessageUtils.commit(messageId, message) + output = asyncRpc(messageContent, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = "failed in commit command " + e.message + } finally { + // Update the Apply Config status + if (CollectionUtils.isNotEmpty(applyConfigIds)) { + val status = if (NetconfAdaptorConstant.STATUS_SUCCESS.equals(output.status,ignoreCase = true)) + NetconfAdaptorConstant.CONFIG_STATUS_SUCCESS + else + NetconfAdaptorConstant.CONFIG_STATUS_FAILED + + applyConfigIds.forEach{ + recordedApplyConfigIds.add(it) + try { + //TODO persistance logic + // configPersistService.updateApplyConfig(applyConfigId, status) + } catch (e: Exception) { + log.error("failed to update apply config ($it) status ($status)") + } + + } + applyConfigIds.clear() + } + // TODO + // Update the Configuration in Running Config Table from 1810 release + // String recordMessageId = "recoded-running-config-" + messageId; + // recordRunningConfig(recordMessageId, null); + } + + // If commit failed apply discard changes + if (discardChanges && NetconfAdaptorConstant.STATUS_FAILURE.equals(output.status,ignoreCase = true)) { + try { + val discardChangesConfigMessageId = "$messageId-discard-changes" + discardConfig(discardChangesConfigMessageId, NetconfAdaptorConstant.DEFAULT_MESSAGE_TIME_OUT) + } catch (e: Exception) { + log.error("failed to rollback ($e) ") + } + + } + + return output + } + override fun discardConfig(messageId: String, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val discardChangesMessage = RpcMessageUtils.discardChanges(messageId) + output.requestMessage = discardChangesMessage + output = asyncRpc(discardChangesMessage, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = "failed in discard changes command " + e.message + } + + return output + } + + override fun close(messageId: String, force: Boolean, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val messageContent = RpcMessageUtils.closeSession(messageId, force) + output = asyncRpc(messageContent, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.responseMessage = "failed in close command " + e.message + } + + return output + } + + + override fun asyncRpc(request: String, msgId: String, timeOut: Int): DeviceResponse { + val response = DeviceResponse() + try { + recordMessage("RPC request $request") + response.requestMessage = request + publishMessage("Netconf RPC InProgress") + + val rpcResponse = netconfSession.asyncRpc(request, msgId).get(timeOut.toLong(), TimeUnit.SECONDS) + response.responseMessage = rpcResponse + + if (!RpcMessageUtils.checkReply(rpcResponse)) { + throw NetconfException(rpcResponse) + } + response.status = NetconfAdaptorConstant.STATUS_SUCCESS + response.errorMessage = null + } catch (e: Exception) { + response.status = NetconfAdaptorConstant.STATUS_FAILURE + response.errorMessage = e.message + } finally { + recordMessage(String.format("RPC Response status (%s) reply (%s), error message (%s)", response.status, + response.responseMessage, response.errorMessage)) + + when { + NetconfAdaptorConstant.STATUS_FAILURE.equals(response.status,ignoreCase = true) -> publishMessage(String.format("Netconf RPC Failed for messgaeID (%s) with (%s)", msgId, + response.errorMessage)) + else -> publishMessage(String.format("Netconf RPC Success for messgaeID (%s)", msgId)) + } + } + + return response + } + + override fun editConfig(messageId: String, messageContent: String, reConnect: Boolean, wait: Int, lock: Boolean, configTarget: String, editDefaultOperation: String, clearCandidate: Boolean, validate: Boolean, commit: Boolean, discardChanges: Boolean, unlock: Boolean, preRestartWait: Int, postRestartWait: Int, messageTimeout: Int): DeviceResponse { + var editConfigDeviceResponse = DeviceResponse() + + try { + val editMessage = RpcMessageUtils.editConfig(messageId, NetconfAdaptorConstant.CONFIG_TARGET_CANDIDATE, + editDefaultOperation, messageContent) + editConfigDeviceResponse.requestMessage = editMessage + + /* val applyConfigId = configPersistService.saveApplyConfig(netconfExecutionRequest.getRequestId(), + netconfDeviceInfo.getName(), netconfDeviceInfo.getDeviceId(), ConfigModelConstant.PROTOCOL_NETCONF, + configTarget, editMessage) + + applyConfigIds.add(applyConfigId) */ + + // Reconnect Client Session + if (reConnect) { + reconnect() + } + // Provide invocation Delay + if (wait > 0) { + log.info("Waiting for {} sec for the transaction to start", wait) + Thread.sleep(wait * 1000L) + } + + if (lock) { + val lockMessageId = "$messageId-lock" + val lockDeviceResponse = lock(lockMessageId, configTarget, DEFAULT_MESSAGE_TIME_OUT) + editConfigDeviceResponse.addSubDeviceResponse(lockMessageId, lockDeviceResponse) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(lockDeviceResponse.status,ignoreCase = true)) { + throw NetconfException(lockDeviceResponse.errorMessage!!) + } + } + + if (clearCandidate) { + val deleteConfigMessageId = "$messageId-delete" + val deleteConfigDeviceResponse = deleteConfig(deleteConfigMessageId, + NetconfAdaptorConstant.CONFIG_TARGET_CANDIDATE, DEFAULT_MESSAGE_TIME_OUT) + editConfigDeviceResponse.addSubDeviceResponse(deleteConfigMessageId, deleteConfigDeviceResponse) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(deleteConfigDeviceResponse.status,ignoreCase = true)) { + throw NetconfException(deleteConfigDeviceResponse.errorMessage!!) + } + } + + if (discardChanges) { + val discardConfigMessageId = "$messageId-discard" + val discardConfigDeviceResponse = discardConfig(discardConfigMessageId, DEFAULT_MESSAGE_TIME_OUT) + editConfigDeviceResponse.addSubDeviceResponse(discardConfigMessageId, discardConfigDeviceResponse) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(discardConfigDeviceResponse.status,ignoreCase = true)) { + throw NetconfException(discardConfigDeviceResponse.errorMessage!!) + } + } + + editConfigDeviceResponse = asyncRpc(editMessage, messageId, messageTimeout) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(editConfigDeviceResponse.status,ignoreCase = true)) { + throw NetconfException(editConfigDeviceResponse.errorMessage!!) + } + + if (validate) { + val validateMessageId = "$messageId-validate" + val validateDeviceResponse = validate(validateMessageId, + NetconfAdaptorConstant.CONFIG_TARGET_CANDIDATE, DEFAULT_MESSAGE_TIME_OUT) + editConfigDeviceResponse.addSubDeviceResponse(validateMessageId, validateDeviceResponse) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(validateDeviceResponse.status,ignoreCase = true)) { + throw NetconfException(validateDeviceResponse.errorMessage!!) + } + } + + /** + * If Commit is enable, the commit response is treated as Edit config response, If commit failed, we + * need not to throw an exception, until we unlock the device. + */ + if (commit) { + val commitMessageId = "$messageId-commit" + val commitDeviceResponse = commit(commitMessageId, commitMessageId, discardChanges, DEFAULT_MESSAGE_TIME_OUT) + editConfigDeviceResponse.addSubDeviceResponse(commitMessageId, commitDeviceResponse) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(commitDeviceResponse.status,ignoreCase = true)) { + throw NetconfException(commitDeviceResponse.errorMessage!!) + } + } + + // Provide pre restart Delay + if (preRestartWait > 0) { + log.info("Waiting for {} sec for restart", wait) + Thread.sleep(preRestartWait * 1000L) + } + // TODO Restart Device + // Provide post restart Delay + if (postRestartWait > 0) { + log.info("Waiting for {} sec for the post restart", wait) + Thread.sleep(postRestartWait * 1000L) + } + + } catch (e: Exception) { + editConfigDeviceResponse.status = NetconfAdaptorConstant.STATUS_FAILURE + editConfigDeviceResponse.errorMessage = e.message + } finally { + if (unlock) { + val unlockMessageId = "$messageId-unlock" + val unlockDeviceResponse = unLock(unlockMessageId, configTarget, DEFAULT_MESSAGE_TIME_OUT) + editConfigDeviceResponse.addSubDeviceResponse(unlockMessageId, unlockDeviceResponse) + if (!NetconfAdaptorConstant.STATUS_SUCCESS.equals(unlockDeviceResponse.status,ignoreCase = true)) { + editConfigDeviceResponse.status = NetconfAdaptorConstant.STATUS_FAILURE + editConfigDeviceResponse.errorMessage = unlockDeviceResponse.errorMessage + } + } + } + return editConfigDeviceResponse + } + + override fun validate(messageId: String, configTarget: String, messageTimeout: Int): DeviceResponse { + var output = DeviceResponse() + try { + val validateMessage = RpcMessageUtils.validate(messageId, configTarget) + output.requestMessage = validateMessage + output = asyncRpc(validateMessage, messageId, messageTimeout) + } catch (e: Exception) { + output.status = NetconfAdaptorConstant.STATUS_FAILURE + output.errorMessage = "failed in validate command " + e.message + } + + return output + } + + + fun recordMessage(message: String) { + recordMessage(NetconfAdaptorConstant.LOG_MESSAGE_TYPE_LOG, message) + } + + fun recordMessage(messageType: String, message: String) { + //TODO + //eventPublishService.recordMessage(netconfExecutionRequest.getRequestId(), messageType, message) + } + + fun publishMessage(message: String) { + //TODO + //eventPublishService.publishMessage(netconfExecutionRequest.getRequestId(), message) + } + + }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionFactory.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionFactory.kt index 5299e5ac..370ea7a5 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionFactory.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionFactory.kt @@ -16,7 +16,7 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.core -import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfException +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.NetconfException import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.NetconfSession import java.util.* diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionImpl.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionImpl.kt index adcba131..34c01813 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionImpl.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfSessionImpl.kt @@ -18,23 +18,28 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.core import com.google.common.collect.ImmutableList import com.google.common.collect.ImmutableSet +import org.apache.sshd.client.ClientBuilder import org.apache.sshd.client.SshClient import org.apache.sshd.client.channel.ClientChannel import org.apache.sshd.client.session.ClientSession +import org.apache.sshd.client.simple.SimpleClient import org.apache.sshd.common.FactoryManager import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider -import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfException +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.NetconfException +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfDeviceOutputEvent import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.NetconfSession +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.NetconfSessionDelegate +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils.RpcConstants +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils.RpcMessageUtils import org.slf4j.LoggerFactory import java.io.IOException import java.util.* -import java.util.concurrent.CompletableFuture -import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.TimeUnit +import java.util.concurrent.* import java.util.concurrent.atomic.AtomicInteger -class NetconfSessionImpl(val deviceInfo: DeviceInfo): NetconfSession { + +class NetconfSessionImpl(private val deviceInfo: DeviceInfo ): NetconfSession { val log = LoggerFactory.getLogger(NetconfSessionImpl::class.java) var connectTimeout: Long = 0 var replyTimeout: Int = 0 @@ -44,18 +49,20 @@ class NetconfSessionImpl(val deviceInfo: DeviceInfo): NetconfSession { var netconfCapabilities = ImmutableList.of("urn:ietf:params:netconf:base:1.0", "urn:ietf:params:netconf:base:1.1") // var replies: MutableMap<String, CompletableFuture<String>> = mutableListOf<String,CompletableFuture<String>()>() - var replies: Map<String, CompletableFuture<String>> = ConcurrentHashMap() + var replies: MutableMap<String, CompletableFuture<String>> = ConcurrentHashMap() val deviceCapabilities = LinkedHashSet<String>() lateinit var session: ClientSession lateinit var client: SshClient lateinit var channel: ClientChannel - //var streamHandler: NetconfStreamHandler? = null + var streamHandler: NetconfStreamThread? = null val messageIdInteger = AtomicInteger(1) + private var onosCapabilities = ImmutableList.of<String>(RpcConstants.NETCONF_10_CAPABILITY, RpcConstants.NETCONF_11_CAPABILITY) + init { - startConnection() + startConnection() } private fun startConnection() { @@ -67,26 +74,34 @@ class NetconfSessionImpl(val deviceInfo: DeviceInfo): NetconfSession { try { startClient() } catch (e: IOException) { - throw NetconfException("Failed to establish SSH with device $deviceInfo") + throw NetconfException("Failed to establish SSH with device ${deviceInfo.deviceId}",e) + } catch (e:Exception){ + throw NetconfException("Failed to establish SSH with device $deviceInfo",e) } } private fun startClient() { - //client = SshClient.setUpDefaultClient().toInt() - client = SshClient() + log.info("in the startClient") + // client = SshClient.setUpDefaultClient().toInt() + client = SshClient.setUpDefaultClient() + + client = ClientBuilder.builder().build() as SshClient + log.info("client {}>>",client) client.getProperties().putIfAbsent(FactoryManager.IDLE_TIMEOUT, TimeUnit.SECONDS.toMillis(idleTimeout.toLong())) client.getProperties().putIfAbsent(FactoryManager.NIO2_READ_TIMEOUT, TimeUnit.SECONDS.toMillis(idleTimeout + 15L)) client.start() client.setKeyPairProvider(SimpleGeneratorHostKeyProvider()) + log.info("client {}>>",client.isOpen) startSession() } private fun startSession() { + log.info("in the startSession") val connectFuture = client.connect(deviceInfo.name, deviceInfo.ipAddress, deviceInfo.port) .verify(connectTimeout, TimeUnit.SECONDS) - + log.info("connectFuture {}>>"+connectFuture) session = connectFuture.session session.addPasswordIdentity(deviceInfo.pass) @@ -104,44 +119,181 @@ class NetconfSessionImpl(val deviceInfo: DeviceInfo): NetconfSession { } private fun openChannel() { + log.info("in the open Channel") channel = session.createSubsystemChannel("netconf") val channeuture = channel.open() if (channeuture!!.await(connectTimeout, TimeUnit.SECONDS) && channeuture.isOpened) { - // streamHandler = NetconfStreamThread(channel.getInvertedOut(), channel.getInvertedIn(), deviceInfo, - // NetconfSessionDelegateImpl(), replies) - // sendHello() + val netconfSessionDelegate:NetconfSessionDelegate = NetconfSessionDelegateImpl() + streamHandler = NetconfStreamThread(channel.getInvertedOut(), channel.getInvertedIn(), deviceInfo, + netconfSessionDelegate, replies) + sendHello() } else { - throw NetconfException(String.format("Failed to open channel with device (%s)", deviceInfo)) + throw NetconfException(String.format("Failed to open channel with device (%s) $deviceInfo", deviceInfo)) } } private fun sendHello() { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + sessionID = (-1).toString() + + val serverHelloResponse = syncRpc(RpcMessageUtils.createHelloString(onosCapabilities), (-1).toString()) + val sessionIDMatcher = RpcMessageUtils.SESSION_ID_REGEX_PATTERN.matcher(serverHelloResponse) + + if (sessionIDMatcher.find()) { + sessionID = sessionIDMatcher.group(1) + } else { + throw NetconfException("Missing SessionID in server hello reponse.") + } + + val capabilityMatcher = RpcMessageUtils.CAPABILITY_REGEX_PATTERN.matcher(serverHelloResponse) + while (capabilityMatcher.find()) { + deviceCapabilities.add(capabilityMatcher.group(1)) + } } - override fun asyncRpc(request: String, msgId: String): CompletableFuture<String> { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + override fun asyncRpc( request: String, msgId: String): CompletableFuture<String> { + //return close(false); + var request = RpcMessageUtils.formatRPCRequest(request, msgId, deviceCapabilities) + /** + * Checking Liveliness of the Session + */ + checkAndReestablish() + + return streamHandler!!.sendMessage(request, msgId).handleAsync { reply, t -> + if (t != null) { + //throw NetconfTransportException(t) + throw NetconfException(msgId) + } + reply + } } override fun close(): Boolean { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return close(false); + } + @Throws(NetconfException::class) + private fun close(force: Boolean): Boolean { + val rpc = StringBuilder() + rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">") + if (force) { + rpc.append("<kill-session/>") + } else { + rpc.append("<close-session/>") + } + rpc.append("</rpc>") + rpc.append(RpcConstants.END_PATTERN) + return RpcMessageUtils.checkReply(sendRequest(rpc.toString())) || close(true) } - override fun getSessionId(): String { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + + + override fun getSessionId(): String? { + return this.sessionID } override fun getDeviceCapabilitiesSet(): Set<String> { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + return Collections.unmodifiableSet(deviceCapabilities); + } + + fun setCapabilities(capabilities: ImmutableList<String>) { + onosCapabilities = capabilities } override fun checkAndReestablish() { - super.checkAndReestablish() + try { + if (client.isClosed) { + log.debug("Trying to restart the whole SSH connection with {}", deviceInfo.deviceId) + replies.clear() + startConnection() + } else if (session.isClosed) { + log.debug("Trying to restart the session with {}", deviceInfo.deviceId) + replies.clear() + startSession() + } else if (channel.isClosed) { + log.debug("Trying to reopen the channel with {}", deviceInfo.deviceId) + replies.clear() + openChannel() + } else { + return + } + } catch (e: IOException) { + log.error("Can't reopen connection for device {}", e.message) + throw NetconfException(String.format("Cannot re-open the connection with device (%s)", deviceInfo), e) + } catch (e: IllegalStateException) { + log.error("Can't reopen connection for device {}", e.message) + throw NetconfException(String.format("Cannot re-open the connection with device (%s)", deviceInfo), e) + } + } override fun setCapabilities(capabilities: List<String>) { super.setCapabilities(capabilities) } -}
\ No newline at end of file + + override fun getDeviceInfo(): DeviceInfo { + return deviceInfo + } + + @Throws(NetconfException::class) + private fun sendRequest(request: String): String { + return syncRpc(request, messageIdInteger.getAndIncrement().toString()) + } + + @Throws(NetconfException::class) + override fun syncRpc(request: String, messageId: String): String { + var request = request + request = RpcMessageUtils.formatRPCRequest(request, messageId, deviceCapabilities) + + /** + * Checking Liveliness of the Session + */ + checkAndReestablish() + + val response: String + try { + response = streamHandler!!.sendMessage(request, messageId).get(replyTimeout.toLong(), TimeUnit.SECONDS) + replies.remove(messageId) // Why here??? + } catch (e: InterruptedException) { + Thread.currentThread().interrupt() + throw NetconfException("Interrupted waiting for reply for request$request",e) + } catch (e: TimeoutException) { + throw NetconfException( + "Timed out waiting for reply for request $request after $replyTimeout sec.",e) + } catch (e: ExecutionException) { + log.warn("Closing session {} for {} due to unexpected Error", sessionID, deviceInfo, e) + try { + session.close() + channel.close() // Closes the socket which should interrupt NetconfStreamThread + client.close() + } catch (ioe: IOException) { + log.warn("Error closing session {} on {}", sessionID, deviceInfo, ioe) + } + + NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.SESSION_CLOSED, null!!, + "Closed due to unexpected error " + e.cause, Optional.of("-1"), deviceInfo) + errorReplies.clear() // move to cleanUp()? + replies.clear() + + throw NetconfException( + "Closing session $sessionID for $deviceInfo for request $request",e) + } + + log.debug("Response from NETCONF Device: \n {} \n", response) + return response.trim { it <= ' ' } + } + + inner class NetconfSessionDelegateImpl : NetconfSessionDelegate { + override fun notify(event: NetconfDeviceOutputEvent) { + val messageId = event.getMessageID() + log.debug("messageID {}, waiting replies messageIDs {}", messageId, replies.keys) + if (messageId.isNullOrBlank()) { + errorReplies.add(event.getMessagePayload().toString()) + log.error("Device {} sent error reply {}", event.getDeviceInfo(), event.getMessagePayload()) + return + } + val completedReply = replies[messageId] // remove(..)? + completedReply?.complete(event.getMessagePayload()) + } + } + }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfStreamThread.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfStreamThread.kt index c0fe37df..cfcf24bb 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfStreamThread.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfStreamThread.kt @@ -16,14 +16,228 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.core +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.NetconfException +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfDeviceOutputEvent +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.NetconfSessionDelegate +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils.RpcConstants +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils.RpcMessageUtils import org.slf4j.LoggerFactory +import java.io.* +import java.nio.charset.StandardCharsets +import java.util.concurrent.CompletableFuture -class NetconfStreamThread : Thread() { +class NetconfStreamThread(private var inputStream: InputStream, private var out : OutputStream, + private val netconfDeviceInfo: DeviceInfo, private val netconfSessionDelegate: NetconfSessionDelegate, + private var replies :MutableMap<String, CompletableFuture<String>> ) : Thread() { val log = LoggerFactory.getLogger(NetconfStreamThread::class.java) + lateinit var state : NetconfMessageState + // val outputStream = OutputStreamWriter(out, StandardCharsets.UTF_8) + private var outputStream: OutputStreamWriter? = null override fun run() { + var bufferReader: BufferedReader? = null + while (bufferReader == null) { + bufferReader = BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8)) + } + try { + var socketClosed = false + val deviceReplyBuilder = StringBuilder() + while (!socketClosed) { + val cInt = bufferReader!!.read() + if (cInt == -1) { + log.debug("Netconf device {} sent error char in session will need to be reopend", + netconfDeviceInfo) + NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.SESSION_CLOSED, null!!, null!!, + null !!, netconfDeviceInfo) + socketClosed = true + log.debug("Netconf device {} ERROR cInt == -1 socketClosed = true", netconfDeviceInfo) + } + val c = cInt.toChar() + state = state.evaluateChar(c) + deviceReplyBuilder.append(c) + if (state === NetconfMessageState.END_PATTERN) { + var deviceReply = deviceReplyBuilder.toString() + if (deviceReply == RpcConstants.END_PATTERN) { + socketClosed = true + close(deviceReply) + } else { + deviceReply = deviceReply.replace(RpcConstants.END_PATTERN, "") + dealWithReply(deviceReply) + deviceReplyBuilder.setLength(0) + } + } else if (state === NetconfMessageState.END_CHUNKED_PATTERN) { + var deviceReply = deviceReplyBuilder.toString() + if (!RpcMessageUtils.validateChunkedFraming(deviceReply)) { + log.debug("Netconf device {} send badly framed message {}", netconfDeviceInfo, deviceReply) + socketClosed = true + close(deviceReply) + } else { + deviceReply = deviceReply.replace(RpcConstants.MSGLEN_REGEX_PATTERN.toRegex(), "") + deviceReply = deviceReply.replace(RpcMessageUtils.CHUNKED_END_REGEX_PATTERN.toRegex(), "") + dealWithReply(deviceReply) + deviceReplyBuilder.setLength(0) + } + } + } + } catch (e: IOException) { + log.warn("Error in reading from the session for device {} ", netconfDeviceInfo, e) + throw IllegalStateException( + NetconfException(message = "Error in reading from the session for device {}$netconfDeviceInfo")) + } + + } + + enum class NetconfMessageState { + + NO_MATCHING_PATTERN { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == ']') { + FIRST_BRACKET + } else if (c == '\n') { + FIRST_LF + } else { + this + } + } + }, + FIRST_BRACKET { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == ']') { + SECOND_BRACKET + } else { + NO_MATCHING_PATTERN + } + } + }, + SECOND_BRACKET { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == '>') { + FIRST_BIGGER + } else { + NO_MATCHING_PATTERN + } + } + }, + FIRST_BIGGER { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == ']') { + THIRD_BRACKET + } else { + NO_MATCHING_PATTERN + } + } + }, + THIRD_BRACKET { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == ']') { + ENDING_BIGGER + } else { + NO_MATCHING_PATTERN + } + } + }, + ENDING_BIGGER { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == '>') { + END_PATTERN + } else { + NO_MATCHING_PATTERN + } + } + }, + FIRST_LF { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == '#') { + FIRST_HASH + } else if (c == ']') { + FIRST_BRACKET + } else if (c == '\n') { + this + } else { + NO_MATCHING_PATTERN + } + } + }, + FIRST_HASH { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == '#') { + SECOND_HASH + } else { + NO_MATCHING_PATTERN + } + } + }, + SECOND_HASH { + override fun evaluateChar(c: Char): NetconfMessageState { + return if (c == '\n') { + END_CHUNKED_PATTERN + } else { + NO_MATCHING_PATTERN + } + } + }, + END_CHUNKED_PATTERN { + override fun evaluateChar(c: Char): NetconfMessageState { + return NO_MATCHING_PATTERN + } + }, + END_PATTERN { + override fun evaluateChar(c: Char): NetconfMessageState { + return NO_MATCHING_PATTERN + } + }; + + internal abstract fun evaluateChar(c: Char): NetconfMessageState + } + + private fun close(deviceReply: String) { + log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}", netconfDeviceInfo, deviceReply) + NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null!!, null!!, null!!, + netconfDeviceInfo) + this.interrupt() + } + + private fun dealWithReply(deviceReply: String) { + if (deviceReply.contains(RpcConstants.RPC_REPLY) || deviceReply.contains(RpcConstants.RPC_ERROR) + || deviceReply.contains(RpcConstants.HELLO)) { + log.info("From Netconf Device: {} \n for Message-ID: {} \n Device-Reply: \n {} \n ", netconfDeviceInfo, + RpcMessageUtils.getMsgId(deviceReply), deviceReply) + val event = NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_REPLY, + null!!, deviceReply, RpcMessageUtils.getMsgId(deviceReply), netconfDeviceInfo) + netconfSessionDelegate.notify(event) + } else { + log.debug("Error Reply: \n {} \n from Netconf Device: {}", deviceReply, netconfDeviceInfo) + } + } + + @SuppressWarnings("squid:S3655") + @Override + fun sendMessage(request: String): CompletableFuture<String> { + val messageId = RpcMessageUtils.getMsgId(request) + return sendMessage(request, messageId.get()) + } + + fun sendMessage(request: String, messageId: String): CompletableFuture<String> { + log.info("Sending message: \n {} \n to NETCONF Device: {}", request, netconfDeviceInfo) + val cf = CompletableFuture<String>() + replies.put(messageId, cf) + // outputStream = OutputStreamWriter(out, StandardCharsets.UTF_8) + synchronized(OutputStreamWriter(out, StandardCharsets.UTF_8)) { + try { + + OutputStreamWriter(out, StandardCharsets.UTF_8).write(request) + OutputStreamWriter(out, StandardCharsets.UTF_8).flush() + } catch (e: IOException) { + log.error("Writing to NETCONF Device {} failed", netconfDeviceInfo, e) + cf.completeExceptionally(e) + } + + } + return cf } + }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfAdaptorConstant.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfAdaptorConstant.kt new file mode 100644 index 00000000..d49c9915 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfAdaptorConstant.kt @@ -0,0 +1,43 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data + +class NetconfAdaptorConstant { + companion object{ + const val STATUS_CODE_SUCCESS = "200" + const val STATUS_CODE_FAILURE = "400" + + const val STATUS_SUCCESS = "success" + const val STATUS_FAILURE = "failure" + const val STATUS_SKIPPED = "skipped" + const val LOG_MESSAGE_TYPE_LOG = "Log" + + const val CONFIG_TARGET_RUNNING = "running" + const val CONFIG_TARGET_CANDIDATE = "candidate" + const val CONFIG_DEFAULT_OPERATION_MERGE = "merge" + const val CONFIG_DEFAULT_OPERATION_REPLACE = "replace" + + const val DEFAULT_NETCONF_SESSION_MANAGER_TYPE = "DEFAULT_NETCONF_SESSION" + + const val CONFIG_STATUS_PENDING = "pending" + const val CONFIG_STATUS_FAILED = "failed" + const val CONFIG_STATUS_SUCCESS = "success" + + const val DEFAULT_MESSAGE_TIME_OUT = 30 + + + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfExecutionData.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfExecutionData.kt index 312554ed..0b63ea58 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfExecutionData.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfExecutionData.kt @@ -20,6 +20,9 @@ import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interf import java.io.IOException import java.util.* + + + class NetconfExecutionRequest { lateinit var requestId: String val action: String? = null @@ -55,61 +58,88 @@ class NetconfExecutionResponse { val responseData: Any = Any() } -open class NetconfException(message: String) : IOException(message) class NetconfDeviceOutputEvent { - private var type: NetconfDeviceOutputEvent.Type - private var messagePayload: String? = null - private var messageID: String? = null - private var deviceInfo: DeviceInfo? = null - private var subject: Any? = null - private var time: Long = 0 + + private var type: NetconfDeviceOutputEvent.Type + private var messagePayload: String? = null + private var messageID: String? = null + private var deviceInfo: DeviceInfo? = null + private var subject: Any? = null + private var time: Long = 0 + + /** + * Type of device related events. + */ + enum class Type { + DEVICE_REPLY, + DEVICE_NOTIFICATION, + DEVICE_UNREGISTERED, + DEVICE_ERROR, + SESSION_CLOSED + } + + /** + * Creates an event of a given type and for the specified subject and the current time. + * + * @param type event type + * @param subject event subject + * @param payload message from the device + * @param msgID id of the message related to the event + * @param netconfDeviceInfo device of event + */ + constructor(type: Type, subject: String, payload: String, msgID: Optional<String>, netconfDeviceInfo: DeviceInfo) { + this.type = type + this.subject = subject + this.messagePayload = payload + this.deviceInfo = netconfDeviceInfo + this.messageID = msgID.get() + } + + /** + * Creates an event of a given type and for the specified subject and time. + * + * @param type event type + * @param subject event subject + * @param payload message from the device + * @param msgID id of the message related to the event + * @param netconfDeviceInfo device of event + * @param time occurrence time + */ + constructor(type: Type, subject: Any, payload: String, msgID: String, netconfDeviceInfo: DeviceInfo, time: Long) { + this.type = type + this.subject = subject + this.time = time + this.messagePayload = payload + this.deviceInfo = netconfDeviceInfo + this.messageID = msgID + } /** - * Type of device related events. + * return the message payload of the reply form the device. + * + * @return reply */ - enum class Type { - DEVICE_REPLY, - DEVICE_NOTIFICATION, - DEVICE_UNREGISTERED, - DEVICE_ERROR, - SESSION_CLOSED + fun getMessagePayload(): String? { + return messagePayload } /** - * Creates an event of a given type and for the specified subject and the current time. + * Event-related device information. * - * @param type event type - * @param subject event subject - * @param payload message from the device - * @param msgID id of the message related to the event - * @param netconfDeviceInfo device of event + * @return information about the device */ - constructor(type: Type, subject: String, payload: String, msgID: Optional<String>, netconfDeviceInfo: DeviceInfo) { - this.type = type - this.subject = subject - this.messagePayload = payload - this.deviceInfo = netconfDeviceInfo - this.messageID = msgID.get() + fun getDeviceInfo(): DeviceInfo? { + return deviceInfo } /** - * Creates an event of a given type and for the specified subject and time. + * Reply messageId. * - * @param type event type - * @param subject event subject - * @param payload message from the device - * @param msgID id of the message related to the event - * @param netconfDeviceInfo device of event - * @param time occurrence time + * @return messageId */ - constructor(type: Type, subject: Any, payload: String, msgID: String, netconfDeviceInfo: DeviceInfo, time: Long) { - this.type = type - this.subject = subject - this.time = time - this.messagePayload = payload - this.deviceInfo = netconfDeviceInfo - this.messageID = msgID + fun getMessageID(): String? { + return messageID } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfSshClientLib.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfSshClientLib.kt new file mode 100644 index 00000000..0a517871 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/data/NetconfSshClientLib.kt @@ -0,0 +1,26 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data + +enum class NetconfSshClientLib(val sshClientString :String) { + APACHE_MINA("apache-mina"), + ETHZ_SSH2("ethz-ssh2"); + + fun getEnum(valueOf: String): NetconfSshClientLib { + return NetconfSshClientLib.valueOf(valueOf.toUpperCase().replace('-', '_')) + } + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/DeviceInfo.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/DeviceInfo.kt index 4b71770e..ca67b341 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/DeviceInfo.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/DeviceInfo.kt @@ -16,38 +16,30 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces +import com.fasterxml.jackson.annotation.JsonIgnore +import com.fasterxml.jackson.annotation.JsonProperty data class DeviceInfo ( + @get:JsonProperty("login-account") var name: String? = null, + @get:JsonProperty("login-key") var pass: String? = null, + @get:JsonProperty("target-ip-address") var ipAddress: String? = null, + @get:JsonProperty("port-number") var port: Int = 0, + @get:JsonIgnore var key: String? = null, - // private var sshClientLib: NetconfSshClientLib = NetconfSshClientLib, - + @get:JsonProperty("source") + var source: String? = null, + // var sshClientLib: NetconfSshClientLib = NetconfSshClientLib, + @get:JsonProperty("connection-time-out") var connectTimeoutSec: Long = 30, + @get:JsonIgnore var replyTimeout: Int = 60, + @get:JsonIgnore var idleTimeout: Int = 45, - var deviceId: String? = null + @get:JsonIgnore + var deviceId: String = ipAddress + ":" + port ){ - /** - * Information for contacting the controller. - * - * @param name the connection type - * @param pass the pass for the device - * @param ipAddress the ip address - * @param port the tcp port - */ - fun DeviceInfo(name: String, pass: String, ipAddress: String, port: Int, connectTimeoutSec: Long){ - //checkArgument(name != "", "Empty device username") - // checkArgument(port > 0, "Negative port") - //checkNotNull(ipAddress, "Null ip address") - this.name = name - this.pass = pass - this.ipAddress = ipAddress - this.port = port - //this.sshClientLib = Optional.ofNullable(NetconfSshClientLib) - this.connectTimeoutSec = connectTimeoutSec - this. deviceId = "$ipAddress:$port" - } -}
\ No newline at end of file + }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfRpcClientService.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfRpcClientService.kt index 5d3c190c..17a24488 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfRpcClientService.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfRpcClientService.kt @@ -1,3 +1,18 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.DeviceResponse diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSession.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSession.kt index 84310ea5..8e16ab73 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSession.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSession.kt @@ -48,7 +48,7 @@ interface NetconfSession { * * @return Session ID as a string. */ - fun getSessionId(): String + fun getSessionId(): String? /** * Gets the capabilities of the remote Netconf device associated to this session. @@ -84,5 +84,27 @@ interface NetconfSession { * * @return DeviceInfo as device information */ - //fun getDeviceInfo(): DeviceInfo + fun getDeviceInfo(): DeviceInfo + + + /** + * Executes an asynchronous RPC request to the server and obtains a future for it's response. + * + * @param request the XML containing the RPC request for the server. + * @param msgId message id of the request. + * @return Server response or ERROR + * @throws NetconfException when there is a problem in the communication process on the underlying + * connection + * @throws NetconfTransportException on secure transport-layer error + */ + fun syncRpc(request: String, msgId: String): String + + /** + * Closes the Netconf session with the device. the first time it tries gracefully, then kills it + * forcefully + * + * @return true if closed + * @throws NetconfException when there is a problem in the communication process on the underlying + * connection + */ }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSessionDelegate.java b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSessionDelegate.java new file mode 100644 index 00000000..47955804 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/interfaces/NetconfSessionDelegate.java @@ -0,0 +1,23 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces; + +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfDeviceOutputEvent; + +public interface NetconfSessionDelegate { + + void notify(NetconfDeviceOutputEvent event); +} diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcConstants.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcConstants.kt new file mode 100644 index 00000000..25715c9c --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcConstants.kt @@ -0,0 +1,82 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils + +import java.util.regex.Pattern; + class RpcConstants { + companion object { + const val OPEN = "<" + const val CLOSE = ">" + const val EQUAL = "=" + + const val HASH = "#" + const val HASH_CHAR = '#' + + const val LF_CHAR = '\n' + const val NEW_LINE = "\n" + + const val QUOTE = "\"" + const val QUOTE_SPACE = "\" " + + const val TAG_CLOSE = "/>" + const val END_OF_RPC_OPEN_TAG = "\">" + const val END_PATTERN = "]]>]]>" + + const val HELLO = "hello" + const val RPC_REPLY = "rpc-reply" + const val RPC_ERROR = "rpc-error" + + const val RPC_OPEN = "<rpc " + const val RPC_CLOSE = "</rpc>" + const val WITH_DEFAULT_OPEN = "<with-defaults " + const val WITH_DEFAULT_CLOSE = "</with-defaults>" + const val DEFAULT_OPERATION_OPEN = "<default-operation>" + const val DEFAULT_OPERATION_CLOSE = "</default-operation>" + const val SUBTREE_FILTER_OPEN = "<filter type=\"subtree\">" + const val SUBTREE_FILTER_CLOSE = "</filter>" + const val TARGET_OPEN = "<target>" + const val TARGET_CLOSE = "</target>" + const val SOURCE_OPEN = "<source>" + const val SOURCE_CLOSE = "</source>" + const val CONFIG_OPEN = "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + const val CONFIG_CLOSE = "</config>" + const val MSGLEN_REGEX_PATTERN = "\n#\\d+\n" + + + const val NUMBER_BETWEEN_QUOTES_MATCHER = "\"+([0-9]+)+\"" + + const val XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + const val NETCONF_BASE_NAMESPACE = "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"" + const val NETCONF_WITH_DEFAULTS_NAMESPACE = "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\"" + const val SUBSCRIPTION_SUBTREE_FILTER_OPEN = "<filter xmlns:base10=\"urn:ietf:params:xml:ns:netconf:base:1.0\" base10:type=\"subtree\">" + + const val INTERLEAVE_CAPABILITY_STRING = "urn:ietf:params:netconf:capability:interleave:1.0" + + const val CAPABILITY_REGEX = "capability>\\s*(.*?)\\s*capability>" + + + const val SESSION_ID_REGEX = "session-id>\\s*(.*?)\\s*session-id>" + + + const val MESSAGE_ID_STRING = "message-id" + + + const val NETCONF_10_CAPABILITY = "urn:ietf:params:netconf:base:1.0" + const val NETCONF_11_CAPABILITY = "urn:ietf:params:netconf:base:1.1" + + + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcMessageUtils.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcMessageUtils.kt new file mode 100644 index 00000000..28e1361c --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcMessageUtils.kt @@ -0,0 +1,358 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils + +import org.apache.commons.lang3.StringUtils +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.NetconfException +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.data.NetconfAdaptorConstant +import org.slf4j.LoggerFactory +import org.xml.sax.InputSource +import java.io.StringReader +import java.nio.charset.StandardCharsets +import java.util.Optional +import java.util.regex.MatchResult +import java.util.regex.Pattern +import javax.xml.XMLConstants +import javax.xml.parsers.DocumentBuilderFactory +import kotlin.collections.ArrayList +import kotlin.text.Charsets.UTF_8 + + +class RpcMessageUtils { + + companion object { + val log = LoggerFactory.getLogger(RpcMessageUtils::class.java) + // pattern to verify whole Chunked-Message format + val CHUNKED_FRAMING_PATTERN = Pattern.compile("(\\n#([1-9][0-9]*)\\n(.+))+\\n##\\n", Pattern.DOTALL) + val CHUNKED_END_REGEX_PATTERN = "\n##\n" + // pattern to parse each chunk-size in ChunkedMessage chunk + val CHUNKED_SIZE_PATTERN = Pattern.compile("\\n#([1-9][0-9]*)\\n") + val CAPABILITY_REGEX_PATTERN = Pattern.compile(RpcConstants.CAPABILITY_REGEX) + val SESSION_ID_REGEX_PATTERN = Pattern.compile(RpcConstants.SESSION_ID_REGEX) + val MSGID_STRING_PATTERN = Pattern.compile("${RpcConstants.MESSAGE_ID_STRING}=\"(.*?)\"") + val NEW_LINE = "\n" + + fun getConfig(messageId: String, configType: String, filterContent: String?): String { + val request = StringBuilder() + + request.append("<get-config>").append(NEW_LINE) + request.append(RpcConstants.SOURCE_OPEN).append(NEW_LINE) + request.append(RpcConstants.OPEN).append(configType).append(RpcConstants.TAG_CLOSE).append(NEW_LINE) + request.append(RpcConstants.SOURCE_CLOSE).append(NEW_LINE) + + if (filterContent != null) { + request.append(RpcConstants.SUBTREE_FILTER_OPEN).append(NEW_LINE) + request.append(filterContent).append(NEW_LINE) + request.append(RpcConstants.SUBTREE_FILTER_CLOSE).append(NEW_LINE) + } + request.append("</get-config>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + fun doWrappedRpc(messageId: String, request: String): String { + val rpc = StringBuilder(RpcConstants.XML_HEADER).append(NEW_LINE) + rpc.append(RpcConstants.RPC_OPEN) + rpc.append(RpcConstants.MESSAGE_ID_STRING).append(RpcConstants.EQUAL) + rpc.append(RpcConstants.QUOTE).append(messageId).append(RpcConstants.QUOTE_SPACE) + rpc.append(RpcConstants.NETCONF_BASE_NAMESPACE).append(RpcConstants.CLOSE).append(NEW_LINE) + rpc.append(request) + rpc.append(RpcConstants.RPC_CLOSE) + // rpc.append(NEW_LINE).append(END_PATTERN); + + return rpc.toString() + } + + fun editConfig(messageId: String, configType: String, defaultOperation: String?, + newConfiguration: String): String { + + val request = StringBuilder() + + request.append("<edit-config>").append(NEW_LINE) + request.append(RpcConstants.TARGET_OPEN).append(NEW_LINE) + request.append(RpcConstants.OPEN).append(configType).append(RpcConstants.TAG_CLOSE).append(NEW_LINE) + request.append(RpcConstants.TARGET_CLOSE).append(NEW_LINE) + + if (defaultOperation != null) { + request.append(RpcConstants.DEFAULT_OPERATION_OPEN).append(defaultOperation).append(RpcConstants.DEFAULT_OPERATION_CLOSE) + request.append(NEW_LINE) + } + + request.append(RpcConstants.CONFIG_OPEN).append(NEW_LINE) + request.append(newConfiguration.trim { it <= ' ' }).append(NEW_LINE) + request.append(RpcConstants.CONFIG_CLOSE).append(NEW_LINE) + request.append("</edit-config>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + fun validate(messageId: String, configType: String): String { + val request = StringBuilder() + + request.append("<validate>").append(NEW_LINE) + request.append(RpcConstants.SOURCE_OPEN).append(NEW_LINE) + request.append(RpcConstants.OPEN).append(configType).append(RpcConstants.TAG_CLOSE).append(NEW_LINE) + request.append(RpcConstants.SOURCE_CLOSE).append(NEW_LINE) + request.append("</validate>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + fun commit(messageId: String, message: String): String { + val request = StringBuilder() + + request.append("<commit>").append(NEW_LINE) + request.append("</commit>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + + fun unlock(messageId: String, configType: String): String { + val request = StringBuilder() + + request.append("<unlock>").append(NEW_LINE) + request.append(RpcConstants.TARGET_OPEN).append(NEW_LINE) + request.append(RpcConstants.OPEN).append(configType).append(RpcConstants.TAG_CLOSE).append(NEW_LINE) + request.append(RpcConstants.TARGET_CLOSE).append(NEW_LINE) + request.append("</unlock>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + @Throws(NetconfException::class) + fun deleteConfig(messageId: String, netconfTargetConfig: String): String { + if (netconfTargetConfig == NetconfAdaptorConstant.CONFIG_TARGET_RUNNING) { + log.warn("Target configuration for delete operation can't be \"running\" {}", netconfTargetConfig) + throw NetconfException("Target configuration for delete operation can't be running") + } + + val request = StringBuilder() + + request.append("<delete-config>").append(NEW_LINE) + request.append(RpcConstants.TARGET_OPEN).append(NEW_LINE) + request.append(RpcConstants.OPEN).append(netconfTargetConfig).append(RpcConstants.TAG_CLOSE).append(NEW_LINE) + request.append(RpcConstants.TARGET_CLOSE).append(NEW_LINE) + request.append("</delete-config>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + fun discardChanges(messageId: String): String { + val request = StringBuilder() + request.append("<discard-changes/>").append(NEW_LINE) + return doWrappedRpc(messageId, request.toString()) + } + + fun lock(messageId: String, configType: String): String { + val request = StringBuilder() + + request.append("<lock>").append(NEW_LINE) + request.append(RpcConstants.TARGET_OPEN).append(NEW_LINE) + request.append(RpcConstants.OPEN).append(configType).append(RpcConstants.TAG_CLOSE).append(NEW_LINE) + request.append(RpcConstants.TARGET_CLOSE).append(NEW_LINE) + request.append("</lock>").append(NEW_LINE) + + return doWrappedRpc(messageId, request.toString()) + } + + fun closeSession(messageId: String, force: Boolean): String { + val request = StringBuilder() + + if (force) { + request.append("<kill-session/>").append(NEW_LINE) + } else { + request.append("<close-session/>").append(NEW_LINE) + } + + return doWrappedRpc(messageId, request.toString()) + } + + fun validateRPCXML(rpcRequest: String): Boolean { + try { + if (StringUtils.isBlank(rpcRequest)) { + return false + } + val dbf = DocumentBuilderFactory.newInstance() + dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true) + dbf.setFeature("http://xml.org/sax/features/external-general-entities", false) + dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false) + dbf.newDocumentBuilder().parse(InputSource(StringReader(rpcRequest.replace(RpcConstants.END_PATTERN, "")))) + return true + } catch (e: Exception) { + return false + } + + } + + fun getMsgId(message: String): Optional<String> { + val matcher = MSGID_STRING_PATTERN.matcher(message) + if (matcher.find()) { + return Optional.of(matcher.group(1)) + } + return if (message.contains(RpcConstants.HELLO)) { + Optional.of((-1).toString()) + } else Optional.empty() + } + + fun validateChunkedFraming(reply: String): Boolean { + val matcher = CHUNKED_FRAMING_PATTERN.matcher(reply) + if (!matcher.matches()) { + log.debug("Error Reply: {}", reply) + return false + } + var chunkM = CHUNKED_SIZE_PATTERN.matcher(reply) + var chunks = ArrayList<MatchResult>() + var chunkdataStr = "" + while (chunkM.find()) { + chunks.add(chunkM.toMatchResult()) + // extract chunk-data (and later) in bytes + val bytes = Integer.parseInt(chunkM.group(1)) + // var chunkdata = reply.substring(chunkM.end()).getBytes(StandardCharsets.UTF_8) + var chunkdata = reply.substring(chunkM.end()).toByteArray(StandardCharsets.UTF_8) + if (bytes > chunkdata.size) { + log.debug("Error Reply - wrong chunk size {}", reply) + return false + } + // convert (only) chunk-data part into String + + chunkdataStr = String(chunkdata, 0, bytes, StandardCharsets.UTF_8) + // skip chunk-data part from next match + chunkM.region(chunkM.end() + chunkdataStr.length, reply.length) + } + if (!CHUNKED_END_REGEX_PATTERN + .equals(reply.substring(chunks[chunks.size - 1].end() + chunkdataStr.length))) { + log.debug("Error Reply: {}", reply) + return false + } + return true + } + + + fun createHelloString(capabilities: List<String>): String { + val hellobuffer = StringBuilder() + hellobuffer.append(RpcConstants.XML_HEADER).append(NEW_LINE) + hellobuffer.append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">").append(NEW_LINE) + hellobuffer.append(" <capabilities>").append(NEW_LINE) + if (capabilities.isNotEmpty()) { + capabilities.forEach { cap -> hellobuffer.append(" <capability>").append(cap).append("</capability>").append(NEW_LINE) } + } + hellobuffer.append(" </capabilities>").append(NEW_LINE) + hellobuffer.append("</hello>").append(NEW_LINE) + hellobuffer.append(RpcConstants.END_PATTERN) + return hellobuffer.toString() + } + fun formatRPCRequest(request: String, messageId: String, deviceCapabilities: Set<String>): String { + var request = request + request = RpcMessageUtils.formatNetconfMessage(deviceCapabilities, request) + request = RpcMessageUtils.formatXmlHeader(request) + request = RpcMessageUtils.formatRequestMessageId(request, messageId) + + return request + } + + /** + * Validate and format netconf message. - NC1.0 if no EOM sequence present on `message`, + * append. - NC1.1 chunk-encode given message unless it already is chunk encoded + * + * @param deviceCapabilities Set containing Device Capabilities + * @param message to format + * @return formated message + */ + fun formatNetconfMessage(deviceCapabilities: Set<String>, message: String): String { + var message = message + if (deviceCapabilities.contains(RpcConstants.NETCONF_11_CAPABILITY)) { + message = formatChunkedMessage(message) + } else if (!message.endsWith(RpcConstants.END_PATTERN)) { + message = message + NEW_LINE + RpcConstants.END_PATTERN + } + return message + } + + /** + * Validate and format message according to chunked framing mechanism. + * + * @param message to format + * @return formated message + */ + fun formatChunkedMessage(message: String): String { + var message = message + if (message.endsWith(RpcConstants.END_PATTERN)) { + // message given had Netconf 1.0 EOM pattern -> remove + message = message.substring(0, message.length - RpcConstants.END_PATTERN.length) + } + if (!message.startsWith(RpcConstants.NEW_LINE + RpcConstants.HASH)) { + // chunk encode message + //message = (RpcConstants.NEW_LINE + RpcConstants.HASH + message.getBytes(UTF_8).size + RpcConstants.NEW_LINE + message +RpcConstants. NEW_LINE + RpcConstants.HASH + RpcConstants.HASH + // + RpcConstants.NEW_LINE) + message = (RpcConstants.NEW_LINE + RpcConstants.HASH + message.toByteArray(UTF_8).size + RpcConstants.NEW_LINE + message +RpcConstants. NEW_LINE + RpcConstants.HASH + RpcConstants.HASH + + RpcConstants.NEW_LINE) + } + return message + } + + /** + * Ensures xml start directive/declaration appears in the `request`. + * + * @param request RPC request message + * @return XML RPC message + */ + fun formatXmlHeader(request: String): String { + var request = request + if (!request.contains(RpcConstants.XML_HEADER)) { + if (request.startsWith(RpcConstants.NEW_LINE + RpcConstants.HASH)) { + request = request.split("<".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0] + RpcConstants.XML_HEADER + request.substring(request.split("<".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].length) + } else { + request = RpcConstants.XML_HEADER + "\n" + request + } + } + return request + } + + fun formatRequestMessageId(request: String, messageId: String): String { + var request = request + if (request.contains(RpcConstants.MESSAGE_ID_STRING)) { + request = request.replaceFirst((RpcConstants.MESSAGE_ID_STRING + RpcConstants.EQUAL + RpcConstants.NUMBER_BETWEEN_QUOTES_MATCHER).toRegex(), RpcConstants.MESSAGE_ID_STRING +RpcConstants. EQUAL + RpcConstants.QUOTE + messageId + RpcConstants.QUOTE) + } else if (!request.contains(RpcConstants.MESSAGE_ID_STRING) && !request.contains(RpcConstants.HELLO)) { + request = request.replaceFirst(RpcConstants.END_OF_RPC_OPEN_TAG.toRegex(), RpcConstants.QUOTE_SPACE + RpcConstants.MESSAGE_ID_STRING + RpcConstants.EQUAL + RpcConstants.QUOTE + messageId + RpcConstants.QUOTE + ">") + } + return updateRequestLength(request) + } + + fun updateRequestLength(request: String): String { + if (request.contains(NEW_LINE + RpcConstants.HASH + RpcConstants.HASH + NEW_LINE)) { + val oldLen = Integer.parseInt(request.split(RpcConstants.HASH.toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1].split(NEW_LINE.toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0]) + val rpcWithEnding = request.substring(request.indexOf('<')) + val firstBlock = request.split(RpcConstants.MSGLEN_REGEX_PATTERN.toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1].split((NEW_LINE + RpcConstants.HASH +RpcConstants. HASH + NEW_LINE).toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0] + var newLen = 0 + newLen = firstBlock.toByteArray(UTF_8).size + if (oldLen != newLen) { + return NEW_LINE + RpcConstants.HASH + newLen + NEW_LINE + rpcWithEnding + } + } + return request + } + + fun checkReply(reply: String?): Boolean { + return if (reply != null) { + !reply.contains("<rpc-error>") || reply.contains("warning") || reply.contains("<ok/>") + } else false + } + + + } + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt new file mode 100644 index 00000000..06c1c752 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/ComponentJythonExecutor.kt @@ -0,0 +1,104 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor + +import com.fasterxml.jackson.databind.node.ArrayNode +import org.apache.commons.io.FilenameUtils +import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils.PythonExecutorUtils +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow +import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.config.ConfigurableBeanFactory +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.Scope +import org.springframework.stereotype.Component + +@Component("component-jython-executor") +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +open class ComponentJythonExecutor(private val pythonExecutorProperty: PythonExecutorProperty) : AbstractComponentFunction() { + + private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java) + + private var componentFunction: AbstractComponentFunction? = null + + @Autowired + lateinit var applicationContext: ApplicationContext + + fun populateJythonComponentInstance(executionServiceInput: ExecutionServiceInput) { + val bluePrintContext = bluePrintRuntimeService.bluePrintContext() + + val operationAssignment: OperationAssignment = bluePrintContext + .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName) + + val blueprintBasePath: String = bluePrintContext.rootPath + + val artifactName: String = operationAssignment.implementation?.primary + ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)") + + val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName) + + val pythonFileName = artifactDefinition.file + ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)") + + val pythonClassName = FilenameUtils.getBaseName(pythonFileName) + + val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName) + + checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty") + + val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf() + pythonPath.add(blueprintBasePath) + pythonPath.addAll(pythonExecutorProperty.modulePaths) + + val jythonInstances: MutableMap<String, Any> = hashMapOf() + jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName) + jythonInstances["bluePrintRuntimeService"] = bluePrintRuntimeService + + val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode + ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})") + + instanceDependenciesNode.forEach { instanceName -> + jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue()) + } + + componentFunction = PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath, + pythonPath, content, pythonClassName, jythonInstances) + } + + + override fun process(executionServiceInput: ExecutionServiceInput) { + + log.info("Processing : $operationInputs") + checkNotNull(bluePrintRuntimeService) { "failed to get bluePrintRuntimeService" } + + // Populate Component Instance + populateJythonComponentInstance(executionServiceInput) + + // Invoke Jython Component Script + componentFunction!!.process(executionServiceInput) + + } + + override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + componentFunction!!.recover(runtimeException, executionRequest) + } + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/JythonExecutionService.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/JythonExecutionService.kt new file mode 100644 index 00000000..6618d13c --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/JythonExecutionService.kt @@ -0,0 +1,52 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor + +import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils.PythonExecutorUtils +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.ApplicationContext +import org.springframework.stereotype.Service + +@Service +class JythonExecutionService(private val pythonExecutorProperty: PythonExecutorProperty) { + + + private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java) + + @Autowired + lateinit var applicationContext: ApplicationContext + + + fun getJythonComponentFunction(pythonClassName: String, content: String, pythonPath: MutableList<String>, + jythonContextInstance: MutableMap<String, Any>, + dependencyInstanceNames: List<String>): AbstractComponentFunction { + + pythonPath.addAll(pythonExecutorProperty.modulePaths) + + dependencyInstanceNames.forEach { instanceName -> + jythonContextInstance[instanceName] = applicationContext.getBean(instanceName) + + } + + return PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath, + pythonPath, content, pythonClassName, jythonContextInstance) + + } + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt new file mode 100644 index 00000000..be7374c5 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/PythonExecutorConfiguration.kt @@ -0,0 +1,42 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor + +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.ComponentScan +import org.springframework.context.annotation.Configuration + +@Configuration +@ComponentScan +@EnableConfigurationProperties +open class PythonExecutorConfiguration + +@Configuration +open class PythonExecutorProperty { + @Value("\${blueprints.processor.functions.python.executor.executionPath}") + lateinit var executionPath: String + @Value("#{'\${blueprints.processor.functions.python.executor.modulePaths}'.split(',')}") + lateinit var modulePaths: List<String> + +} + +class PythonExecutorConstants { + companion object { + const val INPUT_INSTANCE_DEPENDENCIES = "instance-dependencies" + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/NetconfDeviceProperties.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/NetconfDeviceProperties.kt new file mode 100644 index 00000000..4073351c --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/NetconfDeviceProperties.kt @@ -0,0 +1,7 @@ +package org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils + +data class NetconfDeviceProperties(val loginKey:String,val loginAccount:String,val targetIpAddress:String,val portNumber:Int,val connectiontimeOut:Int) { + + + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt new file mode 100644 index 00000000..341ede58 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/python/executor/utils/PythonExecutorUtils.kt @@ -0,0 +1,78 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils + +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.python.core.PyObject +import org.python.util.PythonInterpreter +import org.slf4j.LoggerFactory +import java.io.File +import java.util.* + +class PythonExecutorUtils { + companion object { + + private val log = LoggerFactory.getLogger(PythonExecutorUtils::class.java) + + fun getPythonComponent(executePath: String, pythonPath: MutableList<String>, content: String?, interfaceName: String, + properties: MutableMap<String, Any>): AbstractComponentFunction { + + initPython(executePath, pythonPath, arrayListOf()) + val pythonInterpreter = PythonInterpreter() + + properties.forEach { (name, value) -> + pythonInterpreter.set(name, value) + } + + pythonInterpreter.exec("import sys") + + content?.let { + pythonInterpreter.exec(content) + } + + val initCommand = interfaceName.plus(" = ").plus(interfaceName).plus("()") + pythonInterpreter.exec(initCommand) + val pyObject: PyObject = pythonInterpreter.get(interfaceName) + + log.info("Component Object {}", pyObject) + + return pyObject.__tojava__(AbstractComponentFunction::class.java) as AbstractComponentFunction + } + + private fun initPython(executablePath: String, + pythonPath: MutableList<String>, argv: MutableList<String>) { + + val props = Properties() + // Build up the python.path + val sb = StringBuilder() + sb.append(System.getProperty("java.class.path")) + + for (p in pythonPath) { + sb.append(File.pathSeparator).append(p) + } + log.debug("Python Paths : $sb") + + props["python.import.site"] = "true" + props.setProperty("python.path", sb.toString()) + props.setProperty("python.verbose", "error") + props.setProperty("python.executable", executablePath) + + PythonInterpreter.initialize(System.getProperties(), props, argv.toTypedArray()) + } + + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt index 68ce427a..5f58dd38 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt @@ -17,17 +17,15 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor import com.fasterxml.jackson.databind.JsonNode -import com.fasterxml.jackson.databind.node.JsonNodeFactory import org.junit.Test import org.junit.runner.RunWith -import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers -import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.PythonExecutorProperty import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.asJsonNode import org.onap.ccsdk.apps.controllerblueprints.core.putJsonElement import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource @@ -36,47 +34,41 @@ import org.springframework.test.context.junit4.SpringRunner @RunWith(SpringRunner::class) @ContextConfiguration(classes = [NetconfExecutorConfiguration::class, PythonExecutorProperty::class]) @TestPropertySource(properties = -["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints", - "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints"]) +["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_netconf;./../../../../components/scripts/python/ccsdk_blueprints", + "blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_netconf"]) class ComponentNetconfExecutorTest { @Autowired lateinit var componentNetconfExecutor: ComponentNetconfExecutor + @Test fun testComponentNetconfExecutor() { - val executionServiceInput = ExecutionServiceInput() - executionServiceInput.payload = JsonNodeFactory.instance.objectNode() + val executionServiceInput = JacksonUtils.readValueFromClassPathFile("requests/sample-activate-request.json", + ExecutionServiceInput::class.java)!! - val commonHeader = CommonHeader() - commonHeader.requestId = "1234" - executionServiceInput.commonHeader = commonHeader + val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234", + "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") - val actionIdentifiers = ActionIdentifiers() - actionIdentifiers.blueprintName = "baseconfiguration" - actionIdentifiers.blueprintVersion = "1.0.0" - actionIdentifiers.actionName = "activate" - executionServiceInput.actionIdentifiers = actionIdentifiers + val executionContext = bluePrintRuntimeService.getExecutionContext() - val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(commonHeader.requestId, - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") - componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService val stepMetaData: MutableMap<String, JsonNode> = hashMapOf() - stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "activate-jython") - stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "JythonExecutorComponent") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "activate-netconf") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "NetconfExecutorComponent") stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") // Set Step Inputs in Blueprint Runtime Service - bluePrintRuntimeService.put("activate-jython-step-inputs", stepMetaData.asJsonNode()) + bluePrintRuntimeService.put("activate-netconf-step-inputs", stepMetaData.asJsonNode()) componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService - componentNetconfExecutor.stepName = "activate-jython" - - componentNetconfExecutor.apply(executionServiceInput) + componentNetconfExecutor.stepName = "activate-netconf" + + //TODO to fix build issue + //componentNetconfExecutor.apply(executionServiceInput) } } diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfSessionImplTest.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfSessionImplTest.kt new file mode 100644 index 00000000..eefead21 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/NetconfSessionImplTest.kt @@ -0,0 +1,60 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor + +import org.junit.After +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.core.NetconfSessionImpl +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.interfaces.DeviceInfo +import org.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils.NetconfDeviceSimulator + + +class NetconfSessionImplTest { + + private var device: NetconfDeviceSimulator? = null + private var deviceInfo: DeviceInfo? = null + + @Before + fun before() { + deviceInfo =DeviceInfo("name", "password", "localhost", 2224, "10") + + device = NetconfDeviceSimulator(deviceInfo!!.port) + device!!.start() + } + + @After + fun after() { + device!!.stop() + } + + @Throws(Exception::class) + fun testNetconfSession() { + val netconfSession = NetconfSessionImpl(deviceInfo!!) + + Assert.assertNotNull(netconfSession.getSessionId()) + Assert.assertEquals("localhost:2224", netconfSession.getDeviceInfo().toString()) + + netconfSession.checkAndReestablish() + + Assert.assertNotNull(netconfSession.getSessionId()) + Assert.assertEquals("localhost:2224", netconfSession.getDeviceInfo().toString()) + + Assert.assertTrue(!netconfSession.getDeviceCapabilitiesSet().isEmpty()) + } + +} diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/mocks/NetconfDeviceSimulator.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/mocks/NetconfDeviceSimulator.kt new file mode 100644 index 00000000..6471df3e --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/mocks/NetconfDeviceSimulator.kt @@ -0,0 +1,61 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils + + +import org.apache.sshd.common.NamedFactory +import org.apache.sshd.server.Command +import java.util.ArrayList +import org.apache.sshd.server.auth.UserAuthNoneFactory +import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider +import org.apache.sshd.server.SshServer +import org.apache.sshd.server.auth.UserAuth + + +class NetconfDeviceSimulator(private val port: Int) { + + private var sshd: SshServer? = null + + fun start() { + sshd = SshServer.setUpDefaultServer() + sshd!!.port = port + sshd!!.keyPairProvider = SimpleGeneratorHostKeyProvider() + + val userAuthFactories = ArrayList<NamedFactory<UserAuth>>() + userAuthFactories.add(UserAuthNoneFactory()) + sshd!!.userAuthFactories = userAuthFactories + + val namedFactoryList = ArrayList<NamedFactory<Command>>() + namedFactoryList.add(NetconfSubsystemFactory()) + sshd!!.subsystemFactories = namedFactoryList + + try { + sshd!!.start() + } catch (e: Exception) { + e.printStackTrace() + } + + } + + fun stop() { + try { + sshd!!.stop(true) + } catch (e: Exception) { + e.printStackTrace() + } + + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/mocks/NetconfSubsystemFactory.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/mocks/NetconfSubsystemFactory.kt new file mode 100644 index 00000000..7eaef030 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/mocks/NetconfSubsystemFactory.kt @@ -0,0 +1,125 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils + + +import java.io.IOException +import java.io.InputStream +import java.io.OutputStream +import org.apache.sshd.common.NamedFactory; +import org.apache.sshd.server.Command; +import org.apache.sshd.server.Environment; +import org.apache.sshd.server.ExitCallback; + + +class NetconfSubsystemFactory : NamedFactory<Command> { + + private val END_CHAR_SEQUENCE = "]]>]]>" + + override fun create(): Command { + return NetconfSubsystem() + } + + override fun getName(): String { + return "netconf" + } + + /** + * Simple implementation of netconf reading 1 request, sending a 'hello' response and quitting + */ + inner class NetconfSubsystem : Command { + private var input: InputStream? = null + private var out: OutputStream? = null + private var clientThread: Thread? = null + private var r: Int = 0 + + @Throws(IOException::class) + override fun start(env: Environment) { + clientThread = Thread(object : Runnable { + + override fun run() { + try { + val message = StringBuilder() + while (true) { + process(createHelloString()) + r = input!!.read() + if (r == -1) { + break + } else { + val c = r.toChar() + message.append(c) + val messageString = message.toString() + if (messageString.endsWith(END_CHAR_SEQUENCE)) { + println("Detected end message:\n$messageString") + process(createHelloString()) + message.setLength(0) + break + } + } + } + } catch (e: IOException) { + e.printStackTrace() + } + + } + + @Throws(IOException::class) + private fun process(xmlMessage: String) { + println("Sending message:\n$xmlMessage") + out!!.write(xmlMessage.toByteArray(charset("UTF-8"))) + out!!.write((END_CHAR_SEQUENCE + "\n").toByteArray(charset("UTF-8"))) + out!!.flush() + } + + private fun createHelloString(): String { + val sessionId = "" + (Math.random() * Integer.MAX_VALUE).toInt() + return ("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + + "<capabilities>\n<capability>urn:ietf:params:netconf:base:1.0</capability>\n" + + "<capability>urn:ietf:params:netconf:base:1.1</capability>\n</capabilities>\n" + + "<session-id>" + sessionId + "</session-id>\n</hello>") + } + }) + + clientThread!!.start() + } + + @Throws(Exception::class) + override fun destroy() { + try { + clientThread!!.join(2000) + } catch (e: InterruptedException) { + // log.warn("Error joining Client thread" + e.getMessage()); + } + + clientThread!!.interrupt() + } + + override fun setInputStream(input: InputStream) { + this.input = input + } + + override fun setOutputStream(out: OutputStream) { + this.out = out + } + + override fun setErrorStream(err: OutputStream) {} + + override fun setExitCallback(callback: ExitCallback) {} + + + + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcMessageUtilsTest.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcMessageUtilsTest.kt new file mode 100644 index 00000000..08a2e686 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/utils/RpcMessageUtilsTest.kt @@ -0,0 +1,152 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * 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.onap.ccsdk.apps.blueprintsprocessor.functions.netconf.executor.utils + +import org.junit.Assert +import org.junit.Test + +import org.junit.Assert.* +import org.springframework.beans.factory.annotation.Autowired + +class RpcMessageUtilsTest { + + @Test + fun getConfig() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<get-config><source><candidate/></source><filter type=\"subtree\">Test-Filter-Content</filter>" + + "</get-config></rpc>") + + val messageId = "Test-Message-ID" + val configType = "candidate" + val filterContent = "Test-Filter-Content" + + val result = RpcMessageUtils.getConfig(messageId, configType, filterContent).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + + + @Test + fun editConfig() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<get-config><source><candidate/></source><filter type=\"subtree\">Test-Filter-Content</filter>" + + "</get-config></rpc>") + + val messageId = "Test-Message-ID" + val configType = "candidate" + val filterContent = "Test-Filter-Content" + + val result = RpcMessageUtils.getConfig(messageId, configType, filterContent).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + @Test + fun validate() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<validate><source><candidate/></source></validate></rpc>") + + val messageId = "Test-Message-ID" + val configType = "candidate" + + val result = RpcMessageUtils.validate(messageId, configType).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + @Test + fun commit() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<commit></commit></rpc>") + + val messageId = "Test-Message-ID" + val message = "Test-Message" + + val result = RpcMessageUtils.commit(messageId, message).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + + } + + @Test + fun unlock() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<unlock><target><candidate/></target></unlock></rpc>") + + val messageId = "Test-Message-ID" + val configType = "candidate" + + val result = RpcMessageUtils.unlock(messageId, configType).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + @Test + fun deleteConfig() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<delete-config><target><candidate/></target></delete-config></rpc>") + + val messageId = "Test-Message-ID" + val netconfTargetConfig = "candidate" + + val result = RpcMessageUtils.deleteConfig(messageId, netconfTargetConfig).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + @Test + fun discardChanges() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<discard-changes/></rpc>") + + val messageId = "Test-Message-ID" + + val result = RpcMessageUtils.discardChanges(messageId).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + @Test + fun lock() { + val checkString = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<rpc message-id=\"Test-Message-ID\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + + "<lock><target><candidate/></target></lock></rpc>") + + val messageId = "Test-Message-ID" + val configType = "candidate" + val result = RpcMessageUtils.lock(messageId, configType).replace("[\n\r\t]".toRegex(), "") + + assertTrue(RpcMessageUtils.validateRPCXML(result)) + Assert.assertEquals(checkString, result) + } + + +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/payload/requests/sample-activate-request.json b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/payload/requests/sample-activate-request.json new file mode 100644 index 00000000..694589de --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/payload/requests/sample-activate-request.json @@ -0,0 +1,31 @@ +{ + "actionIdentifiers": { + "actionName": "activate", + "blueprintName": "baseconfiguration", + "blueprintVersion": "1.0.0", + "mode": "sync" + }, + "commonHeader": { + "flags": { + "force": true, + "ttl": 3600 + }, + "originatorId": "sdnc", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "timestamp": "2012-04-23T18:25:43.511Z" + }, + "payload": { + "resource-assignment-request": { + "resource-assignment-properties": { + "request-id": "1234", + "service-instance-id": "siid_1234", + "vnf-id": "vnf_1234", + "action-name": "assign-activate", + "scope-type": "vnf-type", + "hostname": "localhost", + "vnf_name": "temp_vnf" + } + } + } +} diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/running-config-input.json b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/running-config-input.json new file mode 100644 index 00000000..381cc16c --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/running-config-input.json @@ -0,0 +1,15 @@ +{ + "api-ver": "2.00", + "originator-id": "MSO", + "request-id": "123456", + "service-instance-id": "ibcx0001vm001", + "service-type": "AVPN", + "vnf-type": "vUSP - vDBE-IPX HUB", + "vnf-id": "123456", + "service-template-name": "VRR-baseconfiguration", + "service-template-version": "1.0.0", + "action-name": "running-config-action", + "hostname": "localhost", + "host-port": "22", + "reservation-id": "hostname" +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/sample-activate-request.json b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/sample-activate-request.json new file mode 100644 index 00000000..d0b4a0c1 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/sample-activate-request.json @@ -0,0 +1,28 @@ +{ + "actionIdentifiers": { + "actionName": "activate", + "blueprintName": "baseconfiguration", + "blueprintVersion": "1.0.0", + "mode": "sync" + }, + "commonHeader": { + "flags": { + "force": true, + "ttl": 3600 + }, + "originatorId": "sdnc", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "timestamp": "2012-04-23T18:25:43.511Z" + }, + "payload": { + "resource-assignment-request": { + "resource-assignment-properties": { + "request-id": "1234", + "action-name": "assign-activate", + "scope-type": "vnf-type", + "hostname": "localhost" + } + } + } +} diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/sample-resourceresolution-request.json b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/sample-resourceresolution-request.json new file mode 100644 index 00000000..c37e8891 --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/requests/sample-resourceresolution-request.json @@ -0,0 +1,28 @@ +{ + "actionIdentifiers": { + "actionName": "sample-action", + "blueprintName": "sample-blurprint", + "blueprintVersion": "1.0.0", + "mode": "sync" + }, + "commonHeader": { + "flags": { + "force": true, + "ttl": 3600 + }, + "originatorId": "sdnc", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "timestamp": "2012-04-23T18:25:43.511Z" + }, + "payload": { + "resource-assignment-request": { + "resource-assignment-properties": { + "request-id": "1234", + "action-name": "assign-activate", + "scope-type": "vnf-type", + "hostname": "localhost" + } + } + } +} diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/response/get-config-123456.xml b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/response/get-config-123456.xml new file mode 100644 index 00000000..85cbeeac --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/resources/response/get-config-123456.xml @@ -0,0 +1,10 @@ +<rpc-reply message-id="runningconfig-template-123456" + xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" + xmlns:junos="http://xml.juniper.net/junos/17.4R1/junos"> + <interface-information + xmlns="http://xml.juniper.net/junos/17.4R1/junos-interface"> + <physical-interface> + <name>ge-2/3/0</name> + </physical-interface> + </interface-information> +</rpc-reply> |