Merge "Add service template assignments DSL"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / processor / ResourceAssignmentProcessor.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *
4  *  Modifications Copyright © 2017-2019 AT&T, Bell Canada
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor
20
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.resource.dict.ResourceAssignment
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
33 import org.slf4j.LoggerFactory
34 import java.util.*
35
36 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
37
38     private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
39
40     lateinit var raRuntimeService: ResourceAssignmentRuntimeService
41     lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
42
43     var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf()
44     lateinit var scriptType: String
45
46     /**
47      * This will be called from the scripts to serve instance from runtime to scripts.
48      */
49     open fun <T> scriptPropertyInstanceType(name: String): T {
50         return scriptPropertyInstances as? T
51                 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
52     }
53
54     open fun getFromInput(resourceAssignment: ResourceAssignment): JsonNode? {
55         var value: JsonNode? = null
56         try {
57             value = raRuntimeService.getInputValue(resourceAssignment.name)
58             ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
59         } catch (e: BluePrintProcessorException) {
60             // NoOp - couldn't find value from input
61         }
62         return value
63     }
64
65     open fun resourceDefinition(name: String): ResourceDefinition {
66         return resourceDictionaries[name]
67                 ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
68     }
69
70     open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, JsonNode> {
71         val resolvedInputKeyMapping = HashMap<String, JsonNode>()
72         if (MapUtils.isNotEmpty(inputKeyMapping)) {
73             for ((key, value) in inputKeyMapping) {
74                 val resultValue = raRuntimeService.getResolutionStore(value)
75                 resolvedInputKeyMapping[key] = resultValue
76             }
77         }
78         return resolvedInputKeyMapping
79     }
80
81     open suspend fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: MutableMap<String, JsonNode>):
82             String {
83         if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
84             return valueToResolve
85         }
86         //TODO("Optimize to JSON Node directly without velocity").asJsonNode().toString()
87         return BluePrintVelocityTemplateService.generateContent(valueToResolve, keyMapping.asJsonNode().toString())
88     }
89
90     final override suspend fun applyNB(resourceAssignment: ResourceAssignment): Boolean {
91         try {
92             processNB(resourceAssignment)
93         } catch (runtimeException: RuntimeException) {
94             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
95             recoverNB(runtimeException, resourceAssignment)
96         }
97         return true
98     }
99
100     suspend fun executeScript(resourceAssignment: ResourceAssignment) {
101         return when (scriptType) {
102             BluePrintConstants.SCRIPT_JYTHON -> {
103                 executeScriptBlocking(resourceAssignment)
104             }
105             else -> {
106                 executeScriptNB(resourceAssignment)
107             }
108         }
109     }
110
111     private suspend fun executeScriptNB(resourceAssignment: ResourceAssignment) {
112         try {
113             processNB(resourceAssignment)
114         } catch (runtimeException: RuntimeException) {
115             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
116             recoverNB(runtimeException, resourceAssignment)
117         }
118     }
119
120     private fun executeScriptBlocking(resourceAssignment: ResourceAssignment) {
121         try {
122             process(resourceAssignment)
123         } catch (runtimeException: RuntimeException) {
124             log.error("failed in ResourceAssignmentProcessor : ${runtimeException.message}", runtimeException)
125             recover(runtimeException, resourceAssignment)
126         }
127     }
128
129     /**
130      * If Jython Script, Override Blocking methods(process() and recover())
131      * If Kotlin or Internal Scripts, Override non blocking methods ( processNB() and recoverNB()), so default
132      * blocking
133      * methods will have default implementation,
134      *
135      * Always applyNB() method will be invoked, apply() won't be called from parent
136      */
137
138     final override fun apply(resourceAssignment: ResourceAssignment): Boolean {
139         throw BluePrintException("Not Implemented, use applyNB method")
140     }
141
142     final override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
143         throw BluePrintException("Not Implemented required")
144     }
145
146     final override fun prepareResponse(): Boolean {
147         throw BluePrintException("Not Implemented required")
148     }
149
150     final override suspend fun prepareRequestNB(resourceAssignment: ResourceAssignment): ResourceAssignment {
151         throw BluePrintException("Not Implemented required")
152     }
153
154     final override suspend fun prepareResponseNB(): Boolean {
155         throw BluePrintException("Not Implemented required")
156     }
157
158     override fun process(resourceAssignment: ResourceAssignment) {
159         throw BluePrintException("Not Implemented, child class will implement this")
160     }
161
162     override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
163         throw BluePrintException("Not Implemented, child class will implement this")
164     }
165
166     fun addError(type: String, name: String, error: String) {
167         raRuntimeService.getBluePrintError().addError(type, name, error)
168     }
169
170     fun addError(error: String) {
171         raRuntimeService.getBluePrintError().addError(error)
172     }
173 }