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.apps.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.apps.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
28 import org.onap.ccsdk.apps.controllerblueprints.core.format
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JsonParserUtils
31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.ResourceResolverUtils
36 * @author Brinda Santh
38 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
39 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
41 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
45 If Property Assignment is Expression.
47 Recursively resolve the expression
50 fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
51 assignment: JsonNode): JsonNode {
52 val valueNode: JsonNode
53 log.trace("Assignment ({})", assignment)
54 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
56 if (expressionData.isExpression) {
57 valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
59 valueNode = expressionData.valueNode
64 fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
66 var valueNode: JsonNode = NullNode.getInstance()
68 if (expressionData.isExpression) {
69 val command = expressionData.command!!
72 BluePrintConstants.EXPRESSION_GET_INPUT -> {
73 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
75 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
76 valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
78 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
79 valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
81 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
82 valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
84 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
85 valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
87 BluePrintConstants.EXPRESSION_DSL_REFERENCE -> {
88 valueNode = bluePrintRuntimeService.resolveDSLExpression(expressionData.dslExpression!!.propertyName)
90 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
94 throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))
102 get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
103 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
105 fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
106 var valueNode: JsonNode
108 val attributeName = attributeExpression.attributeName
109 val subAttributeName: String? = attributeExpression.subAttributeName
111 var attributeNodeTemplateName = nodeTemplateName
112 when (attributeExpression.modelableEntityName) {
114 val environmentValue = System.getProperty(attributeName)
115 valueNode = JacksonUtils.jsonNode(environmentValue)
118 if (!attributeExpression.modelableEntityName.equals("SELF", true)) {
119 attributeNodeTemplateName = attributeExpression.modelableEntityName
122 var attributeDefinition: AttributeDefinition = bluePrintContext
123 .nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)
124 ?: throw BluePrintException("failed to get attribute definitions for node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
126 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
127 ?: throw BluePrintException("failed to get node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
131 if (subAttributeName != null) {
132 if (valueNode.isObject || valueNode.isArray)
133 valueNode = JsonParserUtils.parse(valueNode, subAttributeName)
139 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
140 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
142 fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
143 var valueNode: JsonNode
145 val propertyName = propertyExpression.propertyName
146 val subPropertyName: String? = propertyExpression.subPropertyName
148 var propertyNodeTemplateName = nodeTemplateName
149 if (!propertyExpression.modelableEntityName.equals("SELF", true)) {
150 propertyNodeTemplateName = propertyExpression.modelableEntityName
153 val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
154 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
156 var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
158 log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
160 // Check it it is a nested expression
161 valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
163 if (subPropertyName != null) {
164 if (valueNode.isObject || valueNode.isArray)
165 valueNode = JsonParserUtils.parse(valueNode, subPropertyName)
171 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
173 fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
174 var outputNodeTemplateName = nodeTemplateName
175 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
176 outputNodeTemplateName = operationOutputExpression.modelableEntityName
178 return bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
179 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
180 operationOutputExpression.propertyName)
184 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
186 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
188 var artifactNodeTemplateName = nodeTemplateName
189 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
190 artifactNodeTemplateName = artifactExpression.modelableEntityName
192 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
193 .artifacts?.get(artifactExpression.artifactName)
194 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
195 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
197 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
200 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
201 val bluePrintBasePath: String = bluePrintContext.rootPath
203 if (artifactDefinition.repository != null) {
205 } else if (artifactDefinition.file != null) {
206 return ResourceResolverUtils.getFileContent(artifactDefinition.file!!, bluePrintBasePath)