2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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 com.att.eelf.configuration.EELFLogger
22 import com.att.eelf.configuration.EELFManager
23 import com.fasterxml.jackson.databind.JsonNode
24 import com.fasterxml.jackson.databind.node.NullNode
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
29 import org.onap.ccsdk.cds.controllerblueprints.core.format
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils
37 * @author Brinda Santh
39 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
40 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
42 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
46 If Property Assignment is Expression.
48 Recursively resolve the expression
51 fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
52 assignment: JsonNode): JsonNode {
53 val valueNode: JsonNode
54 log.trace("Assignment ({})", assignment)
55 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
57 if (expressionData.isExpression) {
58 valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
60 valueNode = expressionData.valueNode
65 fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
67 var valueNode: JsonNode = NullNode.getInstance()
69 if (expressionData.isExpression) {
70 val command = expressionData.command!!
73 BluePrintConstants.EXPRESSION_GET_INPUT -> {
74 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
76 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
77 valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
79 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
80 valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
82 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
83 valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
85 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
86 valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
88 BluePrintConstants.EXPRESSION_DSL_REFERENCE -> {
89 valueNode = bluePrintRuntimeService.resolveDSLExpression(expressionData.dslExpression!!.propertyName)
91 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
95 throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))
103 get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
104 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
106 fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
107 var valueNode: JsonNode
109 val attributeName = attributeExpression.attributeName
110 val subAttributeName: String? = attributeExpression.subAttributeName
112 var attributeNodeTemplateName = nodeTemplateName
114 * Attributes are dynamic runtime properties information. There are multiple types of Attributes,
115 * ENV : Environment Variables
116 * APP : Application properties ( ie Spring resolved properties )
117 * BPP : Blueprint Properties, Specific to Blue Print execution.
118 * SELF : Current Node Template properties.
120 when (attributeExpression.modelableEntityName) {
121 BluePrintConstants.PROPERTY_ENV -> {
122 val environmentValue = System.getProperty(attributeName)
123 valueNode = environmentValue.asJsonPrimitive()
125 BluePrintConstants.PROPERTY_APP -> {
126 TODO("Get property from application properties")
128 BluePrintConstants.PROPERTY_BPP -> {
129 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(BluePrintConstants.PROPERTY_BPP, attributeName)
130 ?: throw BluePrintException("failed to get env attribute name ($attributeName) ")
133 if (!attributeExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
134 attributeNodeTemplateName = attributeExpression.modelableEntityName
137 var attributeDefinition: AttributeDefinition = bluePrintContext
138 .nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)
139 ?: throw BluePrintException("failed to get attribute definitions for node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
141 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
142 ?: throw BluePrintException("failed to get node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
146 if (subAttributeName != null) {
147 if (valueNode.isObject || valueNode.isArray)
148 valueNode = JsonParserUtils.parse(valueNode, subAttributeName)
154 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
155 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
157 fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
158 var valueNode: JsonNode
160 val propertyName = propertyExpression.propertyName
161 val subPropertyName: String? = propertyExpression.subPropertyName
163 var propertyNodeTemplateName = nodeTemplateName
165 if (!propertyExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
166 propertyNodeTemplateName = propertyExpression.modelableEntityName
169 val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
170 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
172 var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
174 log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
176 // Check it it is a nested expression
177 valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
179 if (subPropertyName != null) {
180 if (valueNode.isObject || valueNode.isArray)
181 valueNode = JsonParserUtils.parse(valueNode, subPropertyName)
187 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
189 fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
190 var outputNodeTemplateName = nodeTemplateName
191 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
192 outputNodeTemplateName = operationOutputExpression.modelableEntityName
194 return bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
195 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
196 operationOutputExpression.propertyName)
200 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
202 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
204 var artifactNodeTemplateName = nodeTemplateName
205 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
206 artifactNodeTemplateName = artifactExpression.modelableEntityName
208 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
209 .artifacts?.get(artifactExpression.artifactName)
210 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
211 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
213 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
216 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
217 val bluePrintBasePath: String = bluePrintContext.rootPath
219 if (artifactDefinition.repository != null) {
221 } else if (artifactDefinition.file != null) {
222 return ResourceResolverUtils.getFileContent(artifactDefinition.file, bluePrintBasePath)