2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
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.cds.controllerblueprints.core.service
21 import org.slf4j.LoggerFactory
22 import com.fasterxml.jackson.databind.JsonNode
23 import com.fasterxml.jackson.databind.node.NullNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils
32 * @author Brinda Santh
34 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
35 private val log= LoggerFactory.getLogger(this::class.toString())
37 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
41 If Property Assignment is Expression.
43 Recursively resolve the expression
46 fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
47 assignment: JsonNode): JsonNode {
48 val valueNode: JsonNode
49 log.trace("Assignment ({})", assignment)
50 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
52 if (expressionData.isExpression) {
53 valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
55 valueNode = expressionData.valueNode
60 fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
62 var valueNode: JsonNode = NullNode.getInstance()
64 if (expressionData.isExpression) {
65 val command = expressionData.command!!
68 BluePrintConstants.EXPRESSION_GET_INPUT -> {
69 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
71 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
72 valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
74 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
75 valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
77 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
78 valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
80 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
81 valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
83 BluePrintConstants.EXPRESSION_DSL_REFERENCE -> {
84 valueNode = bluePrintRuntimeService.resolveDSLExpression(expressionData.dslExpression!!.propertyName)
86 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
90 throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))
98 get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
99 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
101 fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
102 var valueNode: JsonNode
104 val attributeName = attributeExpression.attributeName
105 val subAttributeName: String? = attributeExpression.subAttributeName
107 var attributeNodeTemplateName = nodeTemplateName
109 * Attributes are dynamic runtime properties information. There are multiple types of Attributes,
110 * ENV : Environment Variables
111 * APP : Application properties ( ie Spring resolved properties )
112 * BPP : Blueprint Properties, Specific to Blue Print execution.
113 * SELF : Current Node Template properties.
115 when (attributeExpression.modelableEntityName) {
116 BluePrintConstants.PROPERTY_ENV -> {
117 val environmentValue = System.getProperty(attributeName)
118 valueNode = environmentValue.asJsonPrimitive()
120 BluePrintConstants.PROPERTY_APP -> {
121 TODO("Get property from application properties")
123 BluePrintConstants.PROPERTY_BPP -> {
124 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(BluePrintConstants.PROPERTY_BPP, attributeName)
125 ?: throw BluePrintException("failed to get env attribute name ($attributeName) ")
128 if (!attributeExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
129 attributeNodeTemplateName = attributeExpression.modelableEntityName
132 var attributeDefinition: AttributeDefinition = bluePrintContext
133 .nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)
134 ?: throw BluePrintException("failed to get attribute definitions for node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
136 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
137 ?: throw BluePrintException("failed to get node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
141 if (subAttributeName != null) {
142 if (valueNode.isComplexType())
143 valueNode = valueNode.jsonPathParse(subAttributeName)
149 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
150 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
152 fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
153 var valueNode: JsonNode
155 val propertyName = propertyExpression.propertyName
156 val subPropertyName: String? = propertyExpression.subPropertyName
158 var propertyNodeTemplateName = nodeTemplateName
160 if (!propertyExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
161 propertyNodeTemplateName = propertyExpression.modelableEntityName
164 val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
165 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
167 var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
169 log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
171 // Check it it is a nested expression
172 valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
174 if (subPropertyName != null) {
175 if (valueNode.isComplexType())
176 valueNode = valueNode.jsonPathParse(subPropertyName)
182 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
184 fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
185 var outputNodeTemplateName = nodeTemplateName
186 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
187 outputNodeTemplateName = operationOutputExpression.modelableEntityName
190 var valueNode = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
191 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
192 operationOutputExpression.propertyName)
194 val subPropertyName: String? = operationOutputExpression.subPropertyName
195 if (subPropertyName != null) {
196 if (valueNode.isComplexType())
197 valueNode = valueNode.jsonPathParse(subPropertyName)
203 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
205 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
207 var artifactNodeTemplateName = nodeTemplateName
208 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
209 artifactNodeTemplateName = artifactExpression.modelableEntityName
211 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
212 .artifacts?.get(artifactExpression.artifactName)
213 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
214 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
216 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
219 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
220 val bluePrintBasePath: String = bluePrintContext.rootPath
222 if (artifactDefinition.repository != null) {
224 } else if (artifactDefinition.file != null) {
225 return ResourceResolverUtils.getFileContent(artifactDefinition.file, bluePrintBasePath)