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