Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / ResourceAssignmentProcessor.kt
index 3482d45..454a899 100644 (file)
@@ -27,19 +27,19 @@ import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
+import org.onap.ccsdk.cds.controllerblueprints.core.isNullOrMissing
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
-import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
 import org.slf4j.LoggerFactory
-import java.util.*
+import java.util.HashMap
 
 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
 
     private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
 
     lateinit var raRuntimeService: ResourceAssignmentRuntimeService
-    lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
+    var resourceDictionaries: MutableMap<String, ResourceDefinition> = hashMapOf()
 
     var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf()
     lateinit var scriptType: String
@@ -49,46 +49,66 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig
      */
     open fun <T> scriptPropertyInstanceType(name: String): T {
         return scriptPropertyInstances as? T
-                ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
+            ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
     }
 
-    open fun getFromInput(resourceAssignment: ResourceAssignment): JsonNode? {
-        var value: JsonNode? = null
+    open fun setFromInput(resourceAssignment: ResourceAssignment): Boolean {
         try {
-            value = raRuntimeService.getInputValue(resourceAssignment.name)
-            ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
+            val value = raRuntimeService.getInputValue(resourceAssignment.name)
+            if (!value.isNullOrMissing()) {
+                log.debug(
+                    "For Resource:(${resourceAssignment.name}) found value:({}) in input-data.",
+                    ResourceAssignmentUtils.getValueToLog(resourceAssignment.property?.metadata, value)
+                )
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
+                return true
+            }
         } catch (e: BluePrintProcessorException) {
             // NoOp - couldn't find value from input
         }
-        return value
+        return false
     }
 
-    open fun resourceDefinition(name: String): ResourceDefinition {
-        return resourceDictionaries[name]
-                ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
+    open fun setFromInputKeyDependencies(keys: MutableList<String>, resourceAssignment: ResourceAssignment): Boolean {
+        try {
+            for (dependencyKey in keys) {
+                var value = raRuntimeService.getInputValue(dependencyKey)
+                if (!value.isNullOrMissing()) {
+                    log.debug(
+                        "For Resource:(${resourceAssignment.name}) found value:({}) in input-data under: ($dependencyKey).",
+                        ResourceAssignmentUtils.getValueToLog(resourceAssignment.property?.metadata, value)
+                    )
+                    ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
+                    return true
+                }
+            }
+        } catch (e: BluePrintProcessorException) {
+            // NoOp - couldn't find value from input
+        }
+        return false
+    }
+
+    open fun resourceDefinition(name: String): ResourceDefinition? {
+        return if (resourceDictionaries.containsKey(name)) resourceDictionaries[name] else null
     }
 
-    //TODO("Convert return Map<String, JsonNode>")
-    open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
-        val resolvedInputKeyMapping = HashMap<String, Any>()
+    open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, JsonNode> {
+        val resolvedInputKeyMapping = HashMap<String, JsonNode>()
         if (MapUtils.isNotEmpty(inputKeyMapping)) {
             for ((key, value) in inputKeyMapping) {
                 val resultValue = raRuntimeService.getResolutionStore(value)
-                val expressionValue = JacksonUtils.getValue(resultValue)
-                log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
-                resolvedInputKeyMapping[key] = expressionValue
+                resolvedInputKeyMapping[key] = resultValue
             }
         }
         return resolvedInputKeyMapping
     }
 
-    //TODO("Convert keyMapping =  MutableMap<String, JsonNode>")
-    open suspend fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: MutableMap<String, Any>):
+    open suspend fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: MutableMap<String, JsonNode>):
             String {
         if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
             return valueToResolve
         }
-        //TODO("Optimize to JSON Node directly")
+        // TODO("Optimize to JSON Node directly without velocity").asJsonNode().toString()
         return BluePrintVelocityTemplateService.generateContent(valueToResolve, keyMapping.asJsonNode().toString())
     }
 
@@ -98,6 +118,7 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig
         } catch (runtimeException: RuntimeException) {
             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
             recoverNB(runtimeException, resourceAssignment)
+            return false
         }
         return true
     }
@@ -126,7 +147,7 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig
         try {
             process(resourceAssignment)
         } catch (runtimeException: RuntimeException) {
-            log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
+            log.error("failed in ResourceAssignmentProcessor : ${runtimeException.message}", runtimeException)
             recover(runtimeException, resourceAssignment)
         }
     }
@@ -175,4 +196,4 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig
     fun addError(error: String) {
         raRuntimeService.getBluePrintError().addError(error)
     }
-}
\ No newline at end of file
+}