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