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 org.apache.commons.collections.MapUtils
21 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode
24 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintTemplateService
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
28 import org.slf4j.LoggerFactory
31 abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, ResourceAssignment> {
33 private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java)
35 lateinit var raRuntimeService: ResourceAssignmentRuntimeService
36 lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition>
38 var scriptPropertyInstances: Map<String, Any> = hashMapOf()
41 * This will be called from the scripts to serve instance from runtime to scripts.
43 open fun <T> scriptPropertyInstanceType(name: String): T {
44 return scriptPropertyInstances as? T
45 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")
48 open fun resourceDefinition(name: String): ResourceDefinition {
49 return resourceDictionaries[name]
50 ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)")
53 open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> {
54 val resolvedInputKeyMapping = HashMap<String, Any>()
55 if (MapUtils.isNotEmpty(inputKeyMapping)) {
56 for ((key, value) in inputKeyMapping) {
57 val resultValue = raRuntimeService.getResolutionStore(value)
58 val expressionValue = JacksonUtils.getValue(resultValue)
59 log.trace("Reference dictionary key ({}), value ({})", key, expressionValue)
60 resolvedInputKeyMapping[key] = expressionValue
63 return resolvedInputKeyMapping
66 open fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: Map<String, Any>): String {
67 if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) {
70 return BluePrintTemplateService.generateContent(valueToResolve, additionalContext = keyMapping)
73 override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment {
74 log.info("prepareRequest for ${resourceAssignment.name}, dictionary(${resourceAssignment.dictionaryName})," +
75 "source(${resourceAssignment.dictionarySource})")
76 return resourceAssignment
79 override fun prepareResponse(): ResourceAssignment {
80 log.info("Preparing Response...")
81 return ResourceAssignment()
84 override fun apply(resourceAssignment: ResourceAssignment): ResourceAssignment {
86 prepareRequest(resourceAssignment)
87 process(resourceAssignment)
88 } catch (runtimeException: RuntimeException) {
89 recover(runtimeException, resourceAssignment)
91 return prepareResponse()