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
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactExpression
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.AttributeExpression
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.ExpressionData
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationOutputExpression
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyExpression
31 import org.onap.ccsdk.cds.controllerblueprints.core.format
32 import org.onap.ccsdk.cds.controllerblueprints.core.isComplexType
33 import org.onap.ccsdk.cds.controllerblueprints.core.jsonPathParse
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils
36 import org.slf4j.LoggerFactory
41 * @author Brinda Santh
43 open class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
45 private val log = LoggerFactory.getLogger(this::class.toString())
47 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
51 If Property Assignment is Expression.
53 Recursively resolve the expression
56 fun resolveAssignmentExpression(
57 definitionType: String,
58 definitionName: String,
59 assignmentName: String,
62 log.trace("Assignment ({})", assignment)
63 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
65 return if (expressionData.isExpression) {
66 resolveExpression(definitionType, definitionName, assignmentName, expressionData)
68 expressionData.valueNode
72 fun resolveExpression(
73 definitionType: String,
74 definitionName: String,
76 expressionData: ExpressionData
79 var valueNode: JsonNode = NullNode.getInstance()
81 if (expressionData.isExpression) {
82 val command = expressionData.command!!
85 BluePrintConstants.EXPRESSION_GET_INPUT -> {
86 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
88 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
90 resolveAttributeExpression(definitionType, definitionName, expressionData.attributeExpression!!)
92 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
94 resolvePropertyExpression(definitionType, definitionName, expressionData.propertyExpression!!)
96 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
98 resolveOperationOutputExpression(definitionName, expressionData.operationOutputExpression!!)
100 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
101 valueNode = resolveArtifactExpression(definitionName, expressionData.artifactExpression!!)
103 BluePrintConstants.EXPRESSION_DSL_REFERENCE -> {
105 bluePrintRuntimeService.resolveDSLExpression(expressionData.dslExpression!!.propertyName)
107 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
110 throw BluePrintException(
111 "for $definitionType($definitionName) property ($propName), " +
112 "command ($command) is not supported "
121 get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
122 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
124 fun resolveAttributeExpression(
125 definitionType: String,
126 definitionName: String,
127 attributeExpression: AttributeExpression
129 var valueNode: JsonNode
131 val attributeName = attributeExpression.attributeName
132 val subAttributeName: String? = attributeExpression.subAttributeName
134 var attributeDefinitionName = definitionName
136 * Attributes are dynamic runtime properties information. There are multiple types of Attributes,
137 * ENV : Environment Variables
138 * APP : Application properties ( ie Spring resolved properties )
139 * BPP : Blueprint Properties, Specific to Blue Print execution.
140 * SELF : Current Node Template properties.
142 when (attributeExpression.modelableEntityName) {
143 BluePrintConstants.PROPERTY_ENV -> {
144 val environmentValue = System.getenv(attributeName)
145 valueNode = environmentValue.asJsonPrimitive()
147 BluePrintConstants.PROPERTY_APP -> {
148 val environmentValue = System.getProperty(attributeName)
149 valueNode = environmentValue.asJsonPrimitive()
151 BluePrintConstants.PROPERTY_BPP -> {
152 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(
153 BluePrintConstants.PROPERTY_BPP,
155 ) ?: throw BluePrintException("failed to get env attribute name ($attributeName) ")
158 if (!attributeExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
159 attributeDefinitionName = attributeExpression.modelableEntityName
162 /** This block is to Validate, if Attribute definition is present */
163 when (definitionType) {
164 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TEMPLATE,
165 BluePrintConstants.MODEL_DEFINITION_TYPE_WORKFLOW,
166 BluePrintConstants.MODEL_DEFINITION_TYPE_DSL ->
167 bluePrintContext.nodeTemplateNodeType(attributeDefinitionName).attributes
168 BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TEMPLATE ->
169 bluePrintContext.relationshipTemplateRelationshipType(attributeDefinitionName).attributes
170 else -> throw BluePrintException("failed to understand template type($definitionType), it is not supported")
171 }?.get(attributeName)
172 ?: throw BluePrintException(
173 "failed to get attribute definitions for " +
174 "$definitionType ($attributeDefinitionName)'s attribute name ($attributeName) "
177 valueNode = when (definitionType) {
178 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TEMPLATE,
179 BluePrintConstants.MODEL_DEFINITION_TYPE_WORKFLOW,
180 BluePrintConstants.MODEL_DEFINITION_TYPE_DSL ->
181 bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeDefinitionName, attributeName)
182 BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TEMPLATE ->
183 bluePrintRuntimeService.getRelationshipTemplateAttributeValue(
184 attributeDefinitionName,
187 else -> throw BluePrintException("failed to understand template type($definitionType), it is not supported")
189 ?: throw BluePrintException("failed to get node template ($attributeDefinitionName)'s attribute name ($attributeName) ")
192 if (subAttributeName != null) {
193 if (valueNode.isComplexType())
194 valueNode = valueNode.jsonPathParse(subAttributeName)
200 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
201 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
203 fun resolvePropertyExpression(
204 definitionType: String,
205 definitionName: String,
206 propertyExpression: PropertyExpression
208 var valueNode: JsonNode
210 val propertyName = propertyExpression.propertyName
211 val subPropertyName: String? = propertyExpression.subPropertyName
213 var propertyDefinitionName = definitionName
215 if (!propertyExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
216 propertyDefinitionName = propertyExpression.modelableEntityName
219 val nodeTemplatePropertyExpression = when (definitionType) {
220 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TEMPLATE,
221 BluePrintConstants.MODEL_DEFINITION_TYPE_WORKFLOW,
222 BluePrintConstants.MODEL_DEFINITION_TYPE_DSL ->
223 bluePrintContext.nodeTemplateByName(propertyDefinitionName).properties
224 BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TEMPLATE ->
225 bluePrintContext.relationshipTemplateByName(propertyDefinitionName).properties
226 else -> throw BluePrintException("failed to understand template type($definitionType), it is not supported")
228 ?: throw BluePrintException("failed to get property assignment for node template ($definitionName)'s property name ($propertyName).")
230 /** This block is to Validate, if Property definition is present */
231 when (definitionType) {
232 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TEMPLATE,
233 BluePrintConstants.MODEL_DEFINITION_TYPE_WORKFLOW,
234 BluePrintConstants.MODEL_DEFINITION_TYPE_DSL ->
235 bluePrintContext.nodeTemplateNodeType(propertyDefinitionName).properties
236 BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TEMPLATE ->
237 bluePrintContext.relationshipTemplateRelationshipType(propertyDefinitionName).properties
238 else -> throw BluePrintException("failed to understand template type($definitionType), it is not supported")
240 ?: throw BluePrintException("failed to get property definition for node template ($definitionName)'s property name ($propertyName).")
243 "$definitionType($propertyDefinitionName), property($propertyName) resolved value ($nodeTemplatePropertyExpression)"
246 // Check it it is a nested expression
247 valueNode = resolveAssignmentExpression(
249 propertyDefinitionName,
251 nodeTemplatePropertyExpression
254 if (subPropertyName != null) {
255 if (valueNode.isComplexType())
256 valueNode = valueNode.jsonPathParse(subPropertyName)
262 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
264 fun resolveOperationOutputExpression(
265 nodeTemplateName: String,
266 operationOutputExpression: OperationOutputExpression
268 var outputNodeTemplateName = nodeTemplateName
269 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
270 outputNodeTemplateName = operationOutputExpression.modelableEntityName
273 var valueNode = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
274 outputNodeTemplateName,
275 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
276 operationOutputExpression.propertyName
279 val subPropertyName: String? = operationOutputExpression.subPropertyName
280 if (subPropertyName != null) {
281 if (valueNode.isComplexType())
282 valueNode = valueNode.jsonPathParse(subPropertyName)
288 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
290 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
292 var artifactNodeTemplateName = nodeTemplateName
293 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
294 artifactNodeTemplateName = artifactExpression.modelableEntityName
296 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
297 .artifacts?.get(artifactExpression.artifactName)
298 ?: throw BluePrintException(
300 "failed to get artifact definitions for node template ({})'s " +
301 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName
305 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
308 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
309 val bluePrintBasePath: String = bluePrintContext.rootPath
311 if (artifactDefinition.repository != null) {
313 } else if (artifactDefinition.file != null) {
314 return ResourceResolverUtils.getFileContent(artifactDefinition.file, bluePrintBasePath)