81b7acb563d3de70b315c56cf29a5a91e6668863
[ccsdk/apps.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / apps / 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.apps.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.apps.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
28 import org.onap.ccsdk.apps.controllerblueprints.core.format
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.ResourceResolverUtils
31
32 /**
33  *
34  *
35  * @author Brinda Santh
36  */
37 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {
38     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
39
40     private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()
41
42 /*
43
44 If Property Assignment is Expression.
45     Get the Expression
46     Recursively resolve the expression
47  */
48
49     fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,
50                                     assignment: JsonNode): JsonNode {
51         val valueNode: JsonNode
52         log.trace("Assignment ({})", assignment)
53         val expressionData = BluePrintExpressionService.getExpressionData(assignment)
54
55         if (expressionData.isExpression) {
56             valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)
57         } else {
58             valueNode = expressionData.valueNode
59         }
60         return valueNode
61     }
62
63     fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {
64
65         var valueNode: JsonNode = NullNode.getInstance()
66
67         if (expressionData.isExpression) {
68             val command = expressionData.command!!
69
70             when (command) {
71                 BluePrintConstants.EXPRESSION_GET_INPUT -> {
72                     valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)
73                 }
74                 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
75                     valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)
76                 }
77                 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
78                     valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)
79                 }
80                 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
81                     valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)
82                 }
83                 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
84                     valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)
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_property: [ <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         val valueNode: JsonNode
103
104         val attributeName = attributeExpression.attributeName
105         val subAttributeName: String? = attributeExpression.subAttributeName
106
107         var attributeNodeTemplateName = nodeTemplateName
108         when (attributeExpression.modelableEntityName) {
109             "ENV" -> {
110                 val environmentValue = System.getProperty(attributeName)
111                 valueNode = JacksonUtils.jsonNode(environmentValue)
112             }
113             else -> {
114                 if (!attributeExpression.modelableEntityName.equals("SELF", true)) {
115                     attributeNodeTemplateName = attributeExpression.modelableEntityName
116                 }
117                 /* Enable in ONAP Dublin Release
118                 val nodeTemplateAttributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)
119                         ?: throw BluePrintException(format("failed to get attribute definitions for node template " +
120                                 "({})'s property name ({}) ", nodeTemplateName, attributeName))
121
122                 var attributeDefinition: AttributeDefinition = bluePrintContext.nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)!!
123
124                 log.info("node template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, nodeTemplateAttributeExpression)
125                 */
126
127                 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)
128                         ?: throw BluePrintException(format("failed to get node template ({})'s attribute ({}) ", nodeTemplateName, attributeName))
129             }
130
131         }
132 //        subPropertyName?.let {
133 //            valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
134 //        }
135         return valueNode
136     }
137
138     /*
139         get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,
140         <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]
141      */
142     fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {
143         val valueNode: JsonNode
144
145         val propertyName = propertyExpression.propertyName
146         val subPropertyName: String? = propertyExpression.subPropertyName
147
148         var propertyNodeTemplateName = nodeTemplateName
149         if (!propertyExpression.modelableEntityName.equals("SELF", true)) {
150             propertyNodeTemplateName = propertyExpression.modelableEntityName
151         }
152
153         val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)
154                 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))
155
156         var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!
157
158         log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
159
160         // Check it it is a nested expression
161         valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)
162
163 //        subPropertyName?.let {
164 //            valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))
165 //        }
166         return valueNode
167     }
168
169     /*
170     get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>
171      */
172     fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {
173         var outputNodeTemplateName = nodeTemplateName
174         if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {
175             outputNodeTemplateName = operationOutputExpression.modelableEntityName
176         }
177         return bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,
178                 operationOutputExpression.interfaceName, operationOutputExpression.operationName,
179                 operationOutputExpression.propertyName)
180     }
181
182     /*
183     get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]
184      */
185     fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {
186
187         var artifactNodeTemplateName = nodeTemplateName
188         if (!artifactExpression.modelableEntityName.equals("SELF", true)) {
189             artifactNodeTemplateName = artifactExpression.modelableEntityName
190         }
191         val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)
192                 .artifacts?.get(artifactExpression.artifactName)
193                 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +
194                         "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))
195
196         return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))
197     }
198
199     fun artifactContent(artifactDefinition: ArtifactDefinition): String {
200         val bluePrintBasePath: String = bluePrintContext.rootPath
201
202         if (artifactDefinition.repository != null) {
203             TODO()
204         } else if (artifactDefinition.file != null) {
205             return ResourceResolverUtils.getFileContent(artifactDefinition.file!!, bluePrintBasePath)
206         }
207         return ""
208     }
209 }
210