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.ResourceResolverUtils
35 * @author Brinda Santh
37 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
38 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
40 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
44 If Property Assignment is Expression.
46 Recursively resolve the expression
49 fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
50 assignment: JsonNode): JsonNode {
51 val valueNode: JsonNode
52 log.trace("Assignment ({})", assignment)
53 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
55 if (expressionData.isExpression) {
56 valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
58 valueNode = expressionData.valueNode
63 fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
65 var valueNode: JsonNode = NullNode.getInstance()
67 if (expressionData.isExpression) {
68 val command = expressionData.command!!
71 BluePrintConstants.EXPRESSION_GET_INPUT -> {
72 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
74 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
75 valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
77 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
78 valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
80 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
81 valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
83 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
84 valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
86 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
90 throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))
98 get_property: [ <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 val valueNode: JsonNode
104 val attributeName = attributeExpression.attributeName
105 val subAttributeName: String? = attributeExpression.subAttributeName
107 var attributeNodeTemplateName = nodeTemplateName
108 when (attributeExpression.modelableEntityName) {
110 val environmentValue = System.getProperty(attributeName)
111 valueNode = JacksonUtils.jsonNode(environmentValue)
114 if (!attributeExpression.modelableEntityName.equals("SELF", true)) {
115 attributeNodeTemplateName = attributeExpression.modelableEntityName
117 /* Enable in ONAP Dublin Release
118 val nodeTemplateAttributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)
119 ?: throw BluePrintException(format("failed to get attribute definitions for node template " +
120 "({})'s property name ({}) ", nodeTemplateName, attributeName))
122 var attributeDefinition: AttributeDefinition = bluePrintContext.nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)!!
124 log.info("node template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, nodeTemplateAttributeExpression)
127 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
128 ?: throw BluePrintException(format("failed to get node template ({})'s attribute ({}) ", nodeTemplateName, attributeName))
132 // subPropertyName?.let {
133 // valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
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 val 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 // subPropertyName?.let {
164 // valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
170 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
172 fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
173 var outputNodeTemplateName = nodeTemplateName
174 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
175 outputNodeTemplateName = operationOutputExpression.modelableEntityName
177 return bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
178 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
179 operationOutputExpression.propertyName)
183 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
185 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
187 var artifactNodeTemplateName = nodeTemplateName
188 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
189 artifactNodeTemplateName = artifactExpression.modelableEntityName
191 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
192 .artifacts?.get(artifactExpression.artifactName)
193 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
194 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
196 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
199 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
200 val bluePrintBasePath: String = bluePrintContext.rootPath
202 if (artifactDefinition.repository != null) {
204 } else if (artifactDefinition.file != null) {
205 return ResourceResolverUtils.getFileContent(artifactDefinition.file!!, bluePrintBasePath)