2 * Copyright © 2018 IBM.
3 * Modifications Copyright © 2017-2018 AT&T Intellectual Property.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor
20 import com.fasterxml.jackson.databind.JsonNode
21 import org.apache.commons.collections.MapUtils
22 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
23 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintTemplateService
27 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
30 import org.slf4j.LoggerFactory
33 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, Boolean> {
35 private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
37 lateinit var raRuntimeService: ResourceAssignmentRuntimeService
38 lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
40 var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf()
43 * This will be called from the scripts to serve instance from runtime to scripts.
45 open fun <T> scriptPropertyInstanceType(name: String): T {
46 return scriptPropertyInstances as? T
47 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
50 open fun getFromInput(resourceAssignment: ResourceAssignment): JsonNode? {
51 var value: JsonNode? = null
53 value = raRuntimeService.getInputValue(resourceAssignment.name)
54 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
55 } catch (e: BluePrintProcessorException) {
56 // NoOp - couldn't find value from input
61 open fun resourceDefinition(name: String): ResourceDefinition {
62 return resourceDictionaries[name]
63 ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
66 open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
67 val resolvedInputKeyMapping = HashMap<String, Any>()
68 if (MapUtils.isNotEmpty(inputKeyMapping)) {
69 for ((key, value) in inputKeyMapping) {
70 val resultValue = raRuntimeService.getResolutionStore(value)
71 val expressionValue = JacksonUtils.getValue(resultValue)
72 log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
73 resolvedInputKeyMapping[key] = expressionValue
76 return resolvedInputKeyMapping
79 open fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: Map<String, Any>): String {
80 if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
83 return BluePrintTemplateService.generateContent(valueToResolve, additionalContext = keyMapping)
86 override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
87 log.info("prepareRequest for ${resourceAssignment.name}, dictionary(${resourceAssignment.dictionaryName})," +
88 "source(${resourceAssignment.dictionarySource})")
89 return resourceAssignment
92 override fun prepareResponse(): Boolean {
93 log.info("Preparing Response...")
97 override fun apply(resourceAssignment: ResourceAssignment): Boolean {
99 prepareRequest(resourceAssignment)
100 process(resourceAssignment)
101 } catch (runtimeException: RuntimeException) {
102 recover(runtimeException, resourceAssignment)
104 return prepareResponse()
107 fun addError(type: String, name: String, error: String) {
108 raRuntimeService.getBluePrintError().addError(type, name, error)
111 fun addError(error: String) {
112 raRuntimeService.getBluePrintError().addError(error)