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