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.service.BluePrintVelocityTemplateService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
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
37 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
39 private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
41 lateinit var raRuntimeService: ResourceAssignmentRuntimeService
42 lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
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 getFromInput(resourceAssignment: ResourceAssignment): JsonNode? {
56 var value: JsonNode? = null
58 value = raRuntimeService.getInputValue(resourceAssignment.name)
59 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
60 } catch (e: BluePrintProcessorException) {
61 // NoOp - couldn't find value from input
66 open fun resourceDefinition(name: String): ResourceDefinition {
67 return resourceDictionaries[name]
68 ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
71 //TODO("Convert return Map<String, JsonNode>")
72 open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
73 val resolvedInputKeyMapping = HashMap<String, Any>()
74 if (MapUtils.isNotEmpty(inputKeyMapping)) {
75 for ((key, value) in inputKeyMapping) {
76 val resultValue = raRuntimeService.getResolutionStore(value)
77 val expressionValue = JacksonUtils.getValue(resultValue)
78 log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
79 resolvedInputKeyMapping[key] = expressionValue
82 return resolvedInputKeyMapping
85 //TODO("Convert keyMapping = MutableMap<String, JsonNode>")
86 open suspend fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: MutableMap<String, Any>):
88 if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
91 //TODO("Optimize to JSON Node directly")
92 return BluePrintVelocityTemplateService.generateContent(valueToResolve, keyMapping.asJsonNode().toString())
95 final override suspend fun applyNB(resourceAssignment: ResourceAssignment): Boolean {
97 processNB(resourceAssignment)
98 } catch (runtimeException: RuntimeException) {
99 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
100 recoverNB(runtimeException, resourceAssignment)
105 suspend fun executeScript(resourceAssignment: ResourceAssignment) {
106 return when (scriptType) {
107 BluePrintConstants.SCRIPT_JYTHON -> {
108 executeScriptBlocking(resourceAssignment)
111 executeScriptNB(resourceAssignment)
116 private suspend fun executeScriptNB(resourceAssignment: ResourceAssignment) {
118 processNB(resourceAssignment)
119 } catch (runtimeException: RuntimeException) {
120 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
121 recoverNB(runtimeException, resourceAssignment)
125 private fun executeScriptBlocking(resourceAssignment: ResourceAssignment) {
127 process(resourceAssignment)
128 } catch (runtimeException: RuntimeException) {
129 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
130 recover(runtimeException, resourceAssignment)
135 * If Jython Script, Override Blocking methods(process() and recover())
136 * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default
138 * methods will have default implementation,
140 * Always applyNB() method will be invoked, apply() won't be called from parent
143 final override fun apply(resourceAssignment: ResourceAssignment): Boolean {
144 throw BluePrintException("Not Implemented, use applyNB method")
147 final override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
148 throw BluePrintException("Not Implemented required")
151 final override fun prepareResponse(): Boolean {
152 throw BluePrintException("Not Implemented required")
155 final override suspend fun prepareRequestNB(resourceAssignment: ResourceAssignment): ResourceAssignment {
156 throw BluePrintException("Not Implemented required")
159 final override suspend fun prepareResponseNB(): Boolean {
160 throw BluePrintException("Not Implemented required")
163 override fun process(resourceAssignment: ResourceAssignment) {
164 throw BluePrintException("Not Implemented, child class will implement this")
167 override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
168 throw BluePrintException("Not Implemented, child class will implement this")
171 fun addError(type: String, name: String, error: String) {
172 raRuntimeService.getBluePrintError().addError(type, name, error)
175 fun addError(error: String) {
176 raRuntimeService.getBluePrintError().addError(error)