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