From a0140dea9c608d745767574ac621ca0060a2bddc Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Thu, 18 Jul 2019 16:59:19 -0400 Subject: Refactor Netconf script component parent. Change-Id: Ibbec8cd5785372a89e14a86d4e6ff7f9fed4aad2 Issue-ID: CCSDK-1499 Signed-off-by: Brinda Santh Signed-off-by: Steve Siani --- .../cli/executor/CliExecutorExtensions.kt | 23 ++++ .../cli/executor/ScriptComponentExtensions.kt | 22 ---- .../netconf/executor/ComponentNetconfExecutor.kt | 8 +- .../netconf/executor/NetconfExecutorExtensions.kt | 58 +++++++++ .../netconf/executor/ScriptComponentExtensions.kt | 58 --------- .../executor/ComponentNetconfExecutorTest.kt | 17 ++- .../resolution/ResourceResolutionExtensions.kt | 57 +++++++++ .../resolution/ResourceResolutionService.kt | 141 +++++++++++---------- .../resolution/ScriptComponentExtensions.kt | 57 --------- .../executor/RestconfExecutorExtensions.kt | 37 ++++++ .../restconf/executor/ScriptComponentExtensions.kt | 36 ------ .../controllerblueprints/core/CustomFunctions.kt | 17 ++- .../core/service/PropertyAssignmentService.kt | 7 +- .../core/utils/JsonParserUtils.kt | 7 +- .../core/utils/JsonParserUtilsTest.kt | 26 +++- 15 files changed, 315 insertions(+), 256 deletions(-) create mode 100644 ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/CliExecutorExtensions.kt delete mode 100644 ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ScriptComponentExtensions.kt create mode 100644 ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/NetconfExecutorExtensions.kt delete mode 100644 ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ScriptComponentExtensions.kt create mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionExtensions.kt delete mode 100644 ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ScriptComponentExtensions.kt create mode 100644 ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/RestconfExecutorExtensions.kt delete mode 100644 ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ScriptComponentExtensions.kt (limited to 'ms') diff --git a/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/CliExecutorExtensions.kt b/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/CliExecutorExtensions.kt new file mode 100644 index 000000000..bc9b7103c --- /dev/null +++ b/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/CliExecutorExtensions.kt @@ -0,0 +1,23 @@ +/* + * Copyright © 2019 IBM. + * Modifications Copyright © 2018-2019 IBM, Bell Canada + * + * 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.cds.blueprintsprocessor.functions.cli.executor + +/** + * Register the CLI module exposed dependency + */ + diff --git a/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ScriptComponentExtensions.kt b/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ScriptComponentExtensions.kt deleted file mode 100644 index 81f1fd821..000000000 --- a/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ScriptComponentExtensions.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright © 2019 IBM. - * - * 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.cds.blueprintsprocessor.functions.cli.executor - -/** - * Register the CLI module exposed dependency - */ - diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt index 663daf54e..cdee3903b 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutor.kt @@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractScriptComponentFunction import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService import org.onap.ccsdk.cds.controllerblueprints.core.getAsString import org.springframework.beans.factory.config.ConfigurableBeanFactory @@ -39,7 +40,7 @@ open class ComponentNetconfExecutor(private var componentFunctionScriptingServic const val INSTANCE_DEPENDENCIES = "instance-dependencies" } - lateinit var scriptComponent: NetconfComponentFunction + lateinit var scriptComponent: AbstractScriptComponentFunction override suspend fun processNB(executionRequest: ExecutionServiceInput) { @@ -54,8 +55,9 @@ open class ComponentNetconfExecutor(private var componentFunctionScriptingServic scriptDependencies.add(instanceName.textValue()) } - scriptComponent = componentFunctionScriptingService.scriptInstance(this, scriptType, - scriptClassReference, scriptDependencies) + scriptComponent = componentFunctionScriptingService + .scriptInstance(this, scriptType, + scriptClassReference, scriptDependencies) checkNotNull(scriptComponent) { "failed to get netconf script component" } diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/NetconfExecutorExtensions.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/NetconfExecutorExtensions.kt new file mode 100644 index 000000000..510621b2e --- /dev/null +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/NetconfExecutorExtensions.kt @@ -0,0 +1,58 @@ +/* + * Copyright © 2019 IBM. + * Modifications Copyright © 2018 - 2019 IBM, Bell Canada. + * + * 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.cds.blueprintsprocessor.functions.netconf.executor + +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionService +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils + +/** + * Register the Netconf module exposed dependency + */ +fun BluePrintDependencyService.netconfClientService(): ResourceResolutionService = + instance(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION) + + +fun AbstractComponentFunction.netconfDevice(requirementName: String): NetconfDevice { + val deviceInfo = netconfDeviceInfo(requirementName) + return NetconfDevice(deviceInfo) +} + +fun AbstractComponentFunction.netconfDeviceInfo(requirementName: String): DeviceInfo { + + val blueprintContext = bluePrintRuntimeService.bluePrintContext() + + val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, requirementName) + + val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement + .node!!, requirement.capability!!) + + return netconfDeviceInfo(capabilityProperties) +} + +private fun AbstractComponentFunction.netconfDeviceInfo(capabilityProperty: MutableMap): DeviceInfo { + return JacksonUtils.getInstanceFromMap(capabilityProperty, DeviceInfo::class.java) +} + +/** + * Blocking Methods called from Jython Scripts + */ \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ScriptComponentExtensions.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ScriptComponentExtensions.kt deleted file mode 100644 index 510621b2e..000000000 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ScriptComponentExtensions.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright © 2019 IBM. - * Modifications Copyright © 2018 - 2019 IBM, Bell Canada. - * - * 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.cds.blueprintsprocessor.functions.netconf.executor - -import com.fasterxml.jackson.databind.JsonNode -import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.DeviceInfo -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionService -import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils - -/** - * Register the Netconf module exposed dependency - */ -fun BluePrintDependencyService.netconfClientService(): ResourceResolutionService = - instance(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION) - - -fun AbstractComponentFunction.netconfDevice(requirementName: String): NetconfDevice { - val deviceInfo = netconfDeviceInfo(requirementName) - return NetconfDevice(deviceInfo) -} - -fun AbstractComponentFunction.netconfDeviceInfo(requirementName: String): DeviceInfo { - - val blueprintContext = bluePrintRuntimeService.bluePrintContext() - - val requirement = blueprintContext.nodeTemplateRequirement(nodeTemplateName, requirementName) - - val capabilityProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(requirement - .node!!, requirement.capability!!) - - return netconfDeviceInfo(capabilityProperties) -} - -private fun AbstractComponentFunction.netconfDeviceInfo(capabilityProperty: MutableMap): DeviceInfo { - return JacksonUtils.getInstanceFromMap(capabilityProperty, DeviceInfo::class.java) -} - -/** - * Blocking Methods called from Jython Scripts - */ \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt index 575117b15..8a08268d8 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/netconf/executor/ComponentNetconfExecutorTest.kt @@ -26,7 +26,7 @@ import org.junit.runner.RunWith import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils @@ -61,8 +61,21 @@ class ComponentNetconfExecutorTest { val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234", "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") - val executionContext = bluePrintRuntimeService.getExecutionContext() + val assignmentParams = "{\n" + + " \"ipAddress\": \"127.0.0.1\",\n" + + " \"hostName\": \"vnf-host\"\n" + + " }" + + val json = """{ + "hostname" : "127.0.0.1" + } + """.trimIndent() + bluePrintRuntimeService.assignInputs(json.asJsonType()) + bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params", + JacksonUtils.jsonNode(assignmentParams)) + + val executionContext = bluePrintRuntimeService.getExecutionContext() componentNetconfExecutor.bluePrintRuntimeService = bluePrintRuntimeService diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionExtensions.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionExtensions.kt new file mode 100644 index 000000000..ab01b15b7 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionExtensions.kt @@ -0,0 +1,57 @@ +/* + * Copyright © 2019 IBM. + * + * 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.cds.blueprintsprocessor.functions.resource.resolution + +import kotlinx.coroutines.runBlocking +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService + + +/** + * Register the Resolution module exposed dependency + */ +fun BluePrintDependencyService.resourceResolutionService(): ResourceResolutionService = + instance(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION) + + +suspend fun AbstractComponentFunction.storedContentFromResolvedArtifactNB(resolutionKey: String, + artifactName: String): String { + return BluePrintDependencyService.resourceResolutionService() + .resolveFromDatabase(bluePrintRuntimeService, artifactName, resolutionKey) +} + +/** + * Return resolved and mashed artifact content for artifact prefix [artifactPrefix] + */ +suspend fun AbstractComponentFunction.contentFromResolvedArtifactNB(artifactPrefix: String): String { + return BluePrintDependencyService.resourceResolutionService() + .resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactPrefix, mapOf()) +} + + +/** + * Blocking Methods called from Jython Scripts + */ + +fun AbstractComponentFunction.storedContentFromResolvedArtifact(resolutionKey: String, artifactName: String) + : String = runBlocking { + storedContentFromResolvedArtifactNB(resolutionKey, artifactName) +} + +fun AbstractComponentFunction.contentFromResolvedArtifact(artifactPrefix: String): String = runBlocking { + contentFromResolvedArtifactNB(artifactPrefix) +} diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt index 938affc82..314065763 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt @@ -62,30 +62,29 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica private var templateResolutionDBService: TemplateResolutionService, private var blueprintTemplateService: BluePrintTemplateService, private var resourceResolutionDBService: ResourceResolutionDBService) : - ResourceResolutionService { + ResourceResolutionService { private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java) override fun registeredResourceSources(): List { return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java) - .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } - .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } + .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } + .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } } override suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String, resolutionKey: String): String { return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName( - bluePrintRuntimeService, - artifactTemplate, - resolutionKey) + bluePrintRuntimeService, + artifactTemplate, + resolutionKey) } override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String, artifactNames: List, properties: Map): MutableMap { - val resourceAssignmentRuntimeService = ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString()) @@ -95,7 +94,6 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica artifactName, properties) resolvedParams[artifactName] = resolvedContent.asJsonType() - } return resolvedParams } @@ -113,12 +111,12 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)") val resourceAssignmentContent = - bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping) + bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping) val resourceAssignments: MutableList = - JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java) - as? MutableList - ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions") + JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java) + as? MutableList + ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions") if (isToStore(properties)) { val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix) @@ -129,20 +127,20 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica // Get the Resource Dictionary Name val resourceDefinitions: MutableMap = ResourceAssignmentUtils - .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath) + .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath) // Resolve resources resolveResourceAssignments(bluePrintRuntimeService, - resourceDefinitions, - resourceAssignments, - artifactPrefix, - properties) + resourceDefinitions, + resourceAssignments, + artifactPrefix, + properties) val resolvedParamJsonContent = - ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList()) + ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList()) resolvedContent = blueprintTemplateService.generateContent(bluePrintRuntimeService, nodeTemplateName, - artifactTemplate, resolvedParamJsonContent) + artifactTemplate, resolvedParamJsonContent) if (isToStore(properties)) { templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix) @@ -164,49 +162,56 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica properties: Map) { val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments) - val resourceAssignmentRuntimeService = blueprintRuntimeService as ResourceAssignmentRuntimeService + + // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService + val resourceAssignmentRuntimeService = if (!(blueprintRuntimeService is ResourceAssignmentRuntimeService)) { + ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix) + } else { + blueprintRuntimeService + } + coroutineScope { bulkSequenced.forEach { batchResourceAssignments -> // Execute Non Dependent Assignments in parallel ( ie asynchronously ) val deferred = batchResourceAssignments - .filter { it.name != "*" && it.name != "start" } - .filter { it.status != BluePrintConstants.STATUS_SUCCESS } - .map { resourceAssignment -> - async { - val dictionaryName = resourceAssignment.dictionaryName - val dictionarySource = resourceAssignment.dictionarySource - - val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions) - - val resourceAssignmentProcessor = - applicationContext.getBean(processorName) as? ResourceAssignmentProcessor - ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " + - "for resource assignment(${resourceAssignment.name})") - try { - // Set BluePrint Runtime Service - resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService - // Set Resource Dictionaries - resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions - // Invoke Apply Method - resourceAssignmentProcessor.applyNB(resourceAssignment) - - if (isToStore(properties)) { - resourceResolutionDBService.write(properties, - blueprintRuntimeService, - artifactPrefix, - resourceAssignment) - log.info("Resource resolution saved into database successfully : ($resourceAssignment)") + .filter { it.name != "*" && it.name != "start" } + .filter { it.status != BluePrintConstants.STATUS_SUCCESS } + .map { resourceAssignment -> + async { + val dictionaryName = resourceAssignment.dictionaryName + val dictionarySource = resourceAssignment.dictionarySource + + val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions) + + val resourceAssignmentProcessor = + applicationContext.getBean(processorName) as? ResourceAssignmentProcessor + ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " + + "for resource assignment(${resourceAssignment.name})") + try { + // Set BluePrint Runtime Service + resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService + // Set Resource Dictionaries + resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions + // Invoke Apply Method + resourceAssignmentProcessor.applyNB(resourceAssignment) + + if (isToStore(properties)) { + resourceResolutionDBService.write(properties, + blueprintRuntimeService, + artifactPrefix, + resourceAssignment) + log.info("Resource resolution saved into database successfully : ($resourceAssignment)") + } + + // Set errors from RA + blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError()) + } catch (e: RuntimeException) { + log.error("Fail in processing ${resourceAssignment.name}", e) + throw BluePrintProcessorException(e) } - - // Set errors from RA - blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError()) - } catch (e: RuntimeException) { - log.error("Fail in processing ${resourceAssignment.name}", e) - throw BluePrintProcessorException(e) } } - } log.debug("Resolving (${deferred.size})resources parallel.") deferred.awaitAll() } @@ -229,10 +234,10 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica } else -> { val resourceDefinition = resourceDefinitions[dictionaryName] - ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName") + ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName") val resourceSource = resourceDefinition.sources[dictionarySource] - ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)") + ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)") ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type) } @@ -262,25 +267,25 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica if (resolutionKey.isNotEmpty()) { val existingResourceAssignments = - resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence( - bluePrintRuntimeService, - resolutionKey, - occurrence, - artifactPrefix) + resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence( + bluePrintRuntimeService, + resolutionKey, + occurrence, + artifactPrefix) if (existingResourceAssignments.isNotEmpty()) { log.info("Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.", - resolutionKey) + resolutionKey) } return existingResourceAssignments } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) { val existingResourceAssignments = - resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence( - bluePrintRuntimeService, - resourceId, - resourceType, + resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence( + bluePrintRuntimeService, + resourceId, + resourceType, - occurrence, - artifactPrefix) + occurrence, + artifactPrefix) if (existingResourceAssignments.isNotEmpty()) { log.info("Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " + "all resources not already resolved.") diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ScriptComponentExtensions.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ScriptComponentExtensions.kt deleted file mode 100644 index ab01b15b7..000000000 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ScriptComponentExtensions.kt +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright © 2019 IBM. - * - * 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.cds.blueprintsprocessor.functions.resource.resolution - -import kotlinx.coroutines.runBlocking -import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService - - -/** - * Register the Resolution module exposed dependency - */ -fun BluePrintDependencyService.resourceResolutionService(): ResourceResolutionService = - instance(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION) - - -suspend fun AbstractComponentFunction.storedContentFromResolvedArtifactNB(resolutionKey: String, - artifactName: String): String { - return BluePrintDependencyService.resourceResolutionService() - .resolveFromDatabase(bluePrintRuntimeService, artifactName, resolutionKey) -} - -/** - * Return resolved and mashed artifact content for artifact prefix [artifactPrefix] - */ -suspend fun AbstractComponentFunction.contentFromResolvedArtifactNB(artifactPrefix: String): String { - return BluePrintDependencyService.resourceResolutionService() - .resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactPrefix, mapOf()) -} - - -/** - * Blocking Methods called from Jython Scripts - */ - -fun AbstractComponentFunction.storedContentFromResolvedArtifact(resolutionKey: String, artifactName: String) - : String = runBlocking { - storedContentFromResolvedArtifactNB(resolutionKey, artifactName) -} - -fun AbstractComponentFunction.contentFromResolvedArtifact(artifactPrefix: String): String = runBlocking { - contentFromResolvedArtifactNB(artifactPrefix) -} diff --git a/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/RestconfExecutorExtensions.kt b/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/RestconfExecutorExtensions.kt new file mode 100644 index 000000000..6e0c3b612 --- /dev/null +++ b/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/RestconfExecutorExtensions.kt @@ -0,0 +1,37 @@ +/* + * Copyright © 2019 IBM. + * Modifications Copyright © 2018-2019 IBM, Bell Canada + * + * 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.cds.blueprintsprocessor.functions.restconf.executor + +/** + * Register the Restconf module exposed dependency + */ + + +/** + * Generic Mount function + */ + + +/** + * Generic Configure function + */ + + +/** + * Generic Unmount function + */ \ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ScriptComponentExtensions.kt b/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ScriptComponentExtensions.kt deleted file mode 100644 index 9fc685e19..000000000 --- a/ms/blueprintsprocessor/functions/restconf-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ScriptComponentExtensions.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright © 2019 IBM. - * - * 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.cds.blueprintsprocessor.functions.restconf.executor - -/** - * Register the Restconf module exposed dependency - */ - - -/** - * Generic Mount function - */ - - -/** - * Generic Configure function - */ - - -/** - * Generic Unmount function - */ \ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 34a2d9cd3..2717c3be9 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.node.* import org.apache.commons.lang3.ObjectUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.slf4j.LoggerFactory +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils import org.slf4j.helpers.MessageFormatter import kotlin.reflect.KClass @@ -41,8 +42,8 @@ fun T.bpClone(): T { } fun String.isJson(): Boolean { - return ((this.startsWith("{") && this.endsWith("}")) - || (this.startsWith("[") && this.endsWith("]"))) + return ((this.trim().startsWith("{") && this.trim().endsWith("}")) + || (this.trim().startsWith("[") && this.trim().endsWith("]"))) } fun Any.asJsonString(intend: Boolean? = false): String { @@ -263,4 +264,16 @@ inline fun T.isComplexType(): Boolean { return this is ObjectNode || this is ArrayNode } +// Json Parsing Extensions +fun JsonNode.jsonPathParse(expression: String): JsonNode { + check(this.isComplexType()) { "$this is not complex or array node to apply expression" } + return JsonParserUtils.parse(this, expression) +} + +// Json Path Extensions +fun JsonNode.jsonPaths(expression: String): List { + check(this.isComplexType()) { "$this is not complex or array node to apply expression" } + return JsonParserUtils.paths(this, expression) +} + diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt index 30bd75f7d..e5788a936 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt @@ -24,7 +24,6 @@ import com.fasterxml.jackson.databind.node.NullNode import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.data.* import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils /** @@ -141,7 +140,7 @@ If Property Assignment is Expression. } if (subAttributeName != null) { if (valueNode.isComplexType()) - valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subAttributeName) + valueNode = valueNode.jsonPathParse(subAttributeName) } return valueNode } @@ -174,7 +173,7 @@ If Property Assignment is Expression. if (subPropertyName != null) { if (valueNode.isComplexType()) - valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName) + valueNode = valueNode.jsonPathParse(subPropertyName) } return valueNode } @@ -195,7 +194,7 @@ If Property Assignment is Expression. val subPropertyName: String? = operationOutputExpression.subPropertyName if (subPropertyName != null) { if (valueNode.isComplexType()) - valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName) + valueNode = valueNode.jsonPathParse(subPropertyName) } return valueNode } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt index e5eef5a41..19686b5df 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt @@ -24,6 +24,7 @@ import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString class JsonParserUtils { companion object { @@ -39,7 +40,7 @@ class JsonParserUtils { } fun paths(jsonNode: JsonNode, expression: String): List { - return paths(jsonNode, expression) + return paths(jsonNode.asJsonString(), expression) } fun parse(jsonContent: String, expression: String): JsonNode { @@ -47,7 +48,7 @@ class JsonParserUtils { } fun parse(jsonNode: JsonNode, expression: String): JsonNode { - return parse(jsonNode.toString(), expression) + return parse(jsonNode.asJsonString(), expression) } fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode { @@ -56,7 +57,7 @@ class JsonParserUtils { fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode { - return parseNSet(jsonNode, expression, valueNode) + return parseNSet(jsonNode.asJsonString(), expression, valueNode) } } } \ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt index 9728a2255..810dae738 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt @@ -18,6 +18,9 @@ package org.onap.ccsdk.cds.controllerblueprints.core.utils import org.junit.Test import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType +import org.onap.ccsdk.cds.controllerblueprints.core.jsonPathParse +import org.onap.ccsdk.cds.controllerblueprints.core.jsonPaths import kotlin.test.assertEquals class JsonParserUtilsTest { @@ -26,8 +29,29 @@ class JsonParserUtilsTest { fun `test parse Node`() { val dataNode = JacksonUtils.jsonNodeFromClassPathFile("data/default-context.json") - val parsedNode = JsonParserUtils.parse(dataNode, "$.request-id") + val parsedNode = dataNode.jsonPathParse("$.request-id") assertEquals(parsedNode, "12345".asJsonPrimitive(), "failed to parse json request-id") } + + @Test + fun testPaths() { + val json: String = """ + { + "data" : { + "prop1" : "1234" + }, + "data2" : { + "prop1" : "12345" + }, + "data3" : [{ + "prop1" : "12345" + } + ] + } + """.trimIndent() + val jsonNode = json.jsonAsJsonType() + val parsedPath = jsonNode.jsonPaths("$..prop1") + println(parsedPath) + } } \ No newline at end of file -- cgit 1.2.3-korg