2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.core.service
\r
20 import com.fasterxml.jackson.databind.JsonNode
\r
21 import com.fasterxml.jackson.databind.node.NullNode
\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.ResourceResolverUtils
\r
26 import org.slf4j.Logger
\r
27 import org.slf4j.LoggerFactory
\r
31 * @author Brinda Santh
\r
33 class PropertyAssignmentService(var context: MutableMap<String, Any>,
\r
34 var bluePrintRuntimeService: BluePrintRuntimeService) {
\r
35 private val logger: Logger = LoggerFactory.getLogger(this::class.toString())
\r
37 private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext
\r
41 If Property Assignment is Expression.
\r
43 Recurssely resolve the expression
\r
46 fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
\r
47 assignment: Any): JsonNode {
\r
48 var valueNode: JsonNode = NullNode.getInstance()
\r
49 logger.trace("Assignment ({})", assignment)
\r
50 val expressionData = BluePrintExpressionService.getExpressionData(assignment)
\r
52 if (expressionData.isExpression) {
\r
53 valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
\r
55 valueNode = expressionData.valueNode
\r
60 fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
\r
62 var valueNode: JsonNode = NullNode.getInstance()
\r
64 if(expressionData.isExpression) {
\r
65 val command = expressionData.command!!
\r
68 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_INPUT ->{
\r
69 valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
\r
71 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_ATTRIBUTE ->{
\r
72 valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
\r
74 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_PROPERTY ->{
\r
75 valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
\r
77 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT ->{
\r
78 valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
\r
80 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_ARTIFACT ->{
\r
81 valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
\r
83 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE ->{
\r
87 throw BluePrintException(String.format("for property ({}), command ({}) is not supported ", propName, command))
\r
95 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
\r
96 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
\r
98 fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
\r
99 var valueNode: JsonNode = NullNode.getInstance()
\r
101 val attributeName = attributeExpression.attributeName
\r
102 val subAttributeName: String? = attributeExpression.subAttributeName
\r
104 var attributeNodeTemplateName = nodeTemplateName
\r
105 if (!attributeExpression.modelableEntityName.equals("SELF", true)) {
\r
106 attributeNodeTemplateName = attributeExpression.modelableEntityName
\r
109 val attributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)
\r
110 ?: throw BluePrintException(String.format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, attributeName))
\r
112 var propertyDefinition: AttributeDefinition = bluePrintContext.nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)!!
\r
114 logger.info("template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, attributeExpression)
\r
116 // Check it it is a nested expression
\r
117 valueNode = resolveAssignmentExpression(attributeNodeTemplateName, attributeName, attributeExpression)
\r
119 // subPropertyName?.let {
\r
120 // valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
\r
126 get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
\r
127 <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
\r
129 fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
\r
130 var valueNode: JsonNode = NullNode.getInstance()
\r
132 val propertyName = propertyExpression.propertyName
\r
133 val subPropertyName: String? = propertyExpression.subPropertyName
\r
135 var propertyNodeTemplateName = nodeTemplateName
\r
136 if (!propertyExpression.modelableEntityName.equals("SELF", true)) {
\r
137 propertyNodeTemplateName = propertyExpression.modelableEntityName
\r
140 val propertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
\r
141 ?: throw BluePrintException(String.format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
\r
143 var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
\r
145 logger.info("template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, propertyExpression)
\r
147 // Check it it is a nested expression
\r
148 valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, propertyExpression)
\r
150 // subPropertyName?.let {
\r
151 // valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
\r
157 get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
\r
159 fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
\r
160 var outputNodeTemplateName = nodeTemplateName
\r
161 if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
\r
162 outputNodeTemplateName = operationOutputExpression.modelableEntityName
\r
164 return bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
\r
165 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
\r
166 operationOutputExpression.propertyName)
\r
170 get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
\r
172 fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
\r
174 var artifactNodeTemplateName = nodeTemplateName
\r
175 if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
\r
176 artifactNodeTemplateName = artifactExpression.modelableEntityName
\r
178 val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
\r
179 .artifacts?.get(artifactExpression.artifactName)
\r
180 ?: throw BluePrintException(String.format("failed to get artifact definitions for node template ({})'s " +
\r
181 "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
\r
183 return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
\r
186 fun artifactContent(artifactDefinition: ArtifactDefinition): String {
\r
187 val bluePrintBasePath: String = context[org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH] as? String
\r
188 ?: throw BluePrintException(String.format("failed to get property (%s) from context", org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH))
\r
190 if (artifactDefinition.repository != null) {
\r
192 } else if (artifactDefinition.file != null) {
\r
193 return ResourceResolverUtils.getFileContent(artifactDefinition.file!!, bluePrintBasePath)
\r