2 * Copyright © 2018 IBM.
4 * Modifications Copyright © 2017-2019 AT&T, Bell Canada
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
21 import com.fasterxml.jackson.databind.JsonNode
22 import org.apache.commons.collections.MapUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
30 import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
34 import org.slf4j.LoggerFactory
35 import java.util.HashMap
37 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
39 private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
41 lateinit var raRuntimeService: ResourceAssignmentRuntimeService
42 var resourceDictionaries: MutableMap<String, ResourceDefinition> = hashMapOf()
44 var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf()
45 lateinit var scriptType: String
48 * This will be called from the scripts to serve instance from runtime to scripts.
50 open fun <T> scriptPropertyInstanceType(name: String): T {
51 return scriptPropertyInstances as? T
52 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
55 open fun setFromInput(resourceAssignment: ResourceAssignment): Boolean {
57 val value = raRuntimeService.getInputValue(resourceAssignment.name)
58 if (!value.isNullOrMissing()) {
60 "For Resource:(${resourceAssignment.name}) found value:({}) in input-data.",
61 ResourceAssignmentUtils.getValueToLog(resourceAssignment.property?.metadata, value)
63 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
66 } catch (e: BluePrintProcessorException) {
67 // NoOp - couldn't find value from input
72 open fun setFromInputKeyDependencies(keys: MutableList<String>, resourceAssignment: ResourceAssignment): Boolean {
74 for (dependencyKey in keys) {
75 var value = raRuntimeService.getInputValue(dependencyKey)
76 if (!value.isNullOrMissing()) {
78 "For Resource:(${resourceAssignment.name}) found value:({}) in input-data under: ($dependencyKey).",
79 ResourceAssignmentUtils.getValueToLog(resourceAssignment.property?.metadata, value)
81 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
85 } catch (e: BluePrintProcessorException) {
86 // NoOp - couldn't find value from input
91 open fun resourceDefinition(name: String): ResourceDefinition? {
92 return if (resourceDictionaries.containsKey(name)) resourceDictionaries[name] else null
95 open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, JsonNode> {
96 val resolvedInputKeyMapping = HashMap<String, JsonNode>()
97 if (MapUtils.isNotEmpty(inputKeyMapping)) {
98 for ((key, value) in inputKeyMapping) {
99 val resultValue = raRuntimeService.getResolutionStore(value)
100 resolvedInputKeyMapping[key] = resultValue
103 return resolvedInputKeyMapping
106 open suspend fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: MutableMap<String, JsonNode>):
108 if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
109 return valueToResolve
111 // TODO("Optimize to JSON Node directly without velocity").asJsonNode().toString()
112 return BluePrintVelocityTemplateService.generateContent(valueToResolve, keyMapping.asJsonNode().toString())
115 final override suspend fun applyNB(resourceAssignment: ResourceAssignment): Boolean {
117 processNB(resourceAssignment)
118 } catch (runtimeException: RuntimeException) {
119 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
120 recoverNB(runtimeException, resourceAssignment)
126 suspend fun executeScript(resourceAssignment: ResourceAssignment) {
127 return when (scriptType) {
128 BluePrintConstants.SCRIPT_JYTHON -> {
129 executeScriptBlocking(resourceAssignment)
132 executeScriptNB(resourceAssignment)
137 private suspend fun executeScriptNB(resourceAssignment: ResourceAssignment) {
139 processNB(resourceAssignment)
140 } catch (runtimeException: RuntimeException) {
141 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
142 recoverNB(runtimeException, resourceAssignment)
146 private fun executeScriptBlocking(resourceAssignment: ResourceAssignment) {
148 process(resourceAssignment)
149 } catch (runtimeException: RuntimeException) {
150 log.error("failed in ResourceAssignmentProcessor : ${runtimeException.message}", runtimeException)
151 recover(runtimeException, resourceAssignment)
156 * If Jython Script, Override Blocking methods(process() and recover())
157 * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default
159 * methods will have default implementation,
161 * Always applyNB() method will be invoked, apply() won't be called from parent
164 final override fun apply(resourceAssignment: ResourceAssignment): Boolean {
165 throw BluePrintException("Not Implemented, use applyNB method")
168 final override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
169 throw BluePrintException("Not Implemented required")
172 final override fun prepareResponse(): Boolean {
173 throw BluePrintException("Not Implemented required")
176 final override suspend fun prepareRequestNB(resourceAssignment: ResourceAssignment): ResourceAssignment {
177 throw BluePrintException("Not Implemented required")
180 final override suspend fun prepareResponseNB(): Boolean {
181 throw BluePrintException("Not Implemented required")
184 override fun process(resourceAssignment: ResourceAssignment) {
185 throw BluePrintException("Not Implemented, child class will implement this")
188 override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
189 throw BluePrintException("Not Implemented, child class will implement this")
192 fun addError(type: String, name: String, error: String) {
193 raRuntimeService.getBluePrintError().addError(type, name, error)
196 fun addError(error: String) {
197 raRuntimeService.getBluePrintError().addError(error)