Resolve Attribute and properties fails with subPropertyName
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / PropertyAssignmentService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
19
20
21 import org.slf4j.LoggerFactory
22 import com.fasterxml.jackson.databind.JsonNode
23 import com.fasterxml.jackson.databind.node.NullNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils
29
30 /**
31  *
32  *
33  * @author Brinda Santh
34  */
35 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
36     private val log= LoggerFactory.getLogger(this::class.toString())
37
38     private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
39
40 /*
41
42 If Property Assignment is Expression.
43     Get the Expression
44     Recursively resolve the expression
45  */
46
47     fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
48                                     assignment: JsonNode): JsonNode {
49         val valueNode: JsonNode
50         log.trace("Assignment ({})", assignment)
51         val expressionData = BluePrintExpressionService.getExpressionData(assignment)
52
53         if (expressionData.isExpression) {
54             valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
55         } else {
56             valueNode = expressionData.valueNode
57         }
58         return valueNode
59     }
60
61     fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
62
63         var valueNode: JsonNode = NullNode.getInstance()
64
65         if (expressionData.isExpression) {
66             val command = expressionData.command!!
67
68             when (command) {
69                 BluePrintConstants.EXPRESSION_GET_INPUT -> {
70                     valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
71                 }
72                 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
73                     valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
74                 }
75                 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
76                     valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
77                 }
78                 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
79                     valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
80                 }
81                 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
82                     valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
83                 }
84                 BluePrintConstants.EXPRESSION_DSL_REFERENCE -> {
85                     valueNode = bluePrintRuntimeService.resolveDSLExpression(expressionData.dslExpression!!.propertyName)
86                 }
87                 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {
88
89                 }
90                 else -> {
91                     throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))
92                 }
93             }
94         }
95         return valueNode
96     }
97
98     /*
99     get_attribute: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
100     <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
101  */
102     fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {
103         var valueNode: JsonNode
104
105         val attributeName = attributeExpression.attributeName
106         val subAttributeName: String? = attributeExpression.subAttributeName
107
108         var attributeNodeTemplateName = nodeTemplateName
109         /**
110          * Attributes are dynamic runtime properties information. There are multiple types of Attributes,
111          * ENV : Environment Variables
112          * APP : Application properties ( ie Spring resolved properties )
113          * BPP : Blueprint Properties, Specific to Blue Print execution.
114          * SELF : Current Node Template properties.
115          */
116         when (attributeExpression.modelableEntityName) {
117             BluePrintConstants.PROPERTY_ENV -> {
118                 val environmentValue = System.getProperty(attributeName)
119                 valueNode = environmentValue.asJsonPrimitive()
120             }
121             BluePrintConstants.PROPERTY_APP -> {
122                 TODO("Get property from application properties")
123             }
124             BluePrintConstants.PROPERTY_BPP -> {
125                 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(BluePrintConstants.PROPERTY_BPP, attributeName)
126                         ?: throw BluePrintException("failed to get env attribute name ($attributeName) ")
127             }
128             else -> {
129                 if (!attributeExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
130                     attributeNodeTemplateName = attributeExpression.modelableEntityName
131                 }
132
133                 var attributeDefinition: AttributeDefinition = bluePrintContext
134                         .nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)
135                         ?: throw BluePrintException("failed to get attribute definitions for node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
136
137                 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
138                         ?: throw BluePrintException("failed to get node template ($attributeNodeTemplateName)'s attribute name ($attributeName) ")
139             }
140
141         }
142         if (subAttributeName != null) {
143             if (valueNode.isComplexType())
144                 valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subAttributeName)
145         }
146         return valueNode
147     }
148
149     /*
150         get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
151         <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
152      */
153     fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
154         var valueNode: JsonNode
155
156         val propertyName = propertyExpression.propertyName
157         val subPropertyName: String? = propertyExpression.subPropertyName
158
159         var propertyNodeTemplateName = nodeTemplateName
160
161         if (!propertyExpression.modelableEntityName.equals(BluePrintConstants.PROPERTY_SELF, true)) {
162             propertyNodeTemplateName = propertyExpression.modelableEntityName
163         }
164
165         val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
166                 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
167
168         var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
169
170         log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
171
172         // Check it it is a nested expression
173         valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
174
175         if (subPropertyName != null) {
176             if (valueNode.isComplexType())
177                 valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName)
178         }
179         return valueNode
180     }
181
182     /*
183     get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
184      */
185     fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
186         var outputNodeTemplateName = nodeTemplateName
187         if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
188             outputNodeTemplateName = operationOutputExpression.modelableEntityName
189         }
190
191         var valueNode = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
192                 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
193                 operationOutputExpression.propertyName)
194
195         val subPropertyName: String? = operationOutputExpression.subPropertyName
196         if (subPropertyName != null) {
197             if (valueNode.isComplexType())
198                 valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName)
199         }
200         return valueNode
201     }
202
203     /*
204     get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
205      */
206     fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
207
208         var artifactNodeTemplateName = nodeTemplateName
209         if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
210             artifactNodeTemplateName = artifactExpression.modelableEntityName
211         }
212         val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
213                 .artifacts?.get(artifactExpression.artifactName)
214                 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
215                         "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
216
217         return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
218     }
219
220     fun artifactContent(artifactDefinition: ArtifactDefinition): String {
221         val bluePrintBasePath: String = bluePrintContext.rootPath
222
223         if (artifactDefinition.repository != null) {
224             TODO()
225         } else if (artifactDefinition.file != null) {
226             return ResourceResolverUtils.getFileContent(artifactDefinition.file, bluePrintBasePath)
227         }
228         return ""
229     }
230 }
231