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.JsonParserUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils
33 * @author Brinda Santh
35 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
36 private val log= LoggerFactory.getLogger(this::class.toString())
38 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
42 If Property Assignment is Expression.
44 Recursively resolve the expression
47 fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
48 assignment: JsonNode): JsonNode {
49 val valueNode: JsonNode
50 log.trace("Assignment ({})", assignment)
51 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
53 if (expressionData.isExpression) {
54 valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
56 valueNode = expressionData.valueNode
61 fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
63 var valueNode: JsonNode = NullNode.getInstance()
65 if (expressionData.isExpression) {
66 val command = expressionData.command!!
69 BluePrintConstants.EXPRESSION_GET_INPUT -> {
70 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
72 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
73 valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
75 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
76 valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
78 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
79 valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
81 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
82 valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
84 BluePrintConstants.EXPRESSION_DSL_REFERENCE -> {
85 valueNode = bluePrintRuntimeService.resolveDSLExpression(expressionData.dslExpression!!.propertyName)
87 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
91 throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))
99 get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
100 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
102 fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
103 var valueNode: JsonNode
105 val attributeName = attributeExpression.attributeName
106 val subAttributeName: String? = attributeExpression.subAttributeName
108 var attributeNodeTemplateName = nodeTemplateName
110 * Attributes are dynamic runtime properties information. There are multiple types of Attributes,
111 * ENV : Environment Variables
112 * APP : Application properties ( ie Spring resolved properties )
113 * BPP : Blueprint Properties, Specific to Blue Print execution.
114 * SELF : Current Node Template properties.
116 when (attributeExpression.modelableEntityName) {
117 BluePrintConstants.PROPERTY_ENV -> {
118 val environmentValue = System.getProperty(attributeName)
119 valueNode = environmentValue.asJsonPrimitive()
121 BluePrintConstants.PROPERTY_APP -> {
122 TODO("Get property from application properties")
124 BluePrintConstants.PROPERTY_BPP -> {
125 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(BluePrintConstants.PROPERTY_BPP, attributeName)
126 ?: throw BluePrintException("failed to get env attribute name ($attributeName) ")
129 if (!attributeExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
130 attributeNodeTemplateName = attributeExpression.modelableEntityName
133 var attributeDefinition: AttributeDefinition = bluePrintContext
134 .nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)
135 ?: throw BluePrintException("failed to get attribute definitions for node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
137 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
138 ?: throw BluePrintException("failed to get node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
142 if (subAttributeName != null) {
143 if (valueNode.isComplexType())
144 valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subAttributeName)
150 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
151 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
153 fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
154 var valueNode: JsonNode
156 val propertyName = propertyExpression.propertyName
157 val subPropertyName: String? = propertyExpression.subPropertyName
159 var propertyNodeTemplateName = nodeTemplateName
161 if (!propertyExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
162 propertyNodeTemplateName = propertyExpression.modelableEntityName
165 val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
166 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
168 var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
170 log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
172 // Check it it is a nested expression
173 valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
175 if (subPropertyName != null) {
176 if (valueNode.isComplexType())
177 valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName)
183 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
185 fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
186 var outputNodeTemplateName = nodeTemplateName
187 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
188 outputNodeTemplateName = operationOutputExpression.modelableEntityName
191 var valueNode = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
192 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
193 operationOutputExpression.propertyName)
195 val subPropertyName: String? = operationOutputExpression.subPropertyName
196 if (subPropertyName != null) {
197 if (valueNode.isComplexType())
198 valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName)
204 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
206 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
208 var artifactNodeTemplateName = nodeTemplateName
209 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
210 artifactNodeTemplateName = artifactExpression.modelableEntityName
212 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
213 .artifacts?.get(artifactExpression.artifactName)
214 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
215 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
217 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
220 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
221 val bluePrintBasePath: String = bluePrintContext.rootPath
223 if (artifactDefinition.repository != null) {
225 } else if (artifactDefinition.file != null) {
226 return ResourceResolverUtils.getFileContent(artifactDefinition.file, bluePrintBasePath)