Merge "Sonar fix: Application.java"
[ccsdk/apps.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / service / PropertyAssignmentService.kt
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.core.service\r
19 \r
20 \r
21 import com.att.eelf.configuration.EELFLogger\r
22 import com.att.eelf.configuration.EELFManager\r
23 import com.fasterxml.jackson.databind.JsonNode\r
24 import com.fasterxml.jackson.databind.node.NullNode\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.*\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.format\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.ResourceResolverUtils\r
31 \r
32 /**\r
33  *\r
34  *\r
35  * @author Brinda Santh\r
36  */\r
37 class PropertyAssignmentService(var bluePrintRuntimeService: BluePrintRuntimeService<MutableMap<String, JsonNode>>) {\r
38     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())\r
39 \r
40     private var bluePrintContext: BluePrintContext = bluePrintRuntimeService.bluePrintContext()\r
41 \r
42 /*\r
43 \r
44 If Property Assignment is Expression.\r
45     Get the Expression\r
46     Recursively resolve the expression\r
47  */\r
48 \r
49     fun resolveAssignmentExpression(nodeTemplateName: String, assignmentName: String,\r
50                                     assignment: Any): JsonNode {\r
51         val valueNode: JsonNode\r
52         log.trace("Assignment ({})", assignment)\r
53         val expressionData = BluePrintExpressionService.getExpressionData(assignment)\r
54 \r
55         if (expressionData.isExpression) {\r
56             valueNode = resolveExpression(nodeTemplateName, assignmentName, expressionData)\r
57         } else {\r
58             valueNode = expressionData.valueNode\r
59         }\r
60         return valueNode\r
61     }\r
62 \r
63     fun resolveExpression(nodeTemplateName: String, propName: String, expressionData: ExpressionData): JsonNode {\r
64 \r
65         var valueNode: JsonNode = NullNode.getInstance()\r
66 \r
67         if (expressionData.isExpression) {\r
68             val command = expressionData.command!!\r
69 \r
70             when (command) {\r
71                 BluePrintConstants.EXPRESSION_GET_INPUT -> {\r
72                     valueNode = bluePrintRuntimeService.getInputValue(expressionData.inputExpression?.propertyName!!)\r
73                 }\r
74                 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {\r
75                     valueNode = resolveAttributeExpression(nodeTemplateName, expressionData.attributeExpression!!)\r
76                 }\r
77                 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {\r
78                     valueNode = resolvePropertyExpression(nodeTemplateName, expressionData.propertyExpression!!)\r
79                 }\r
80                 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {\r
81                     valueNode = resolveOperationOutputExpression(nodeTemplateName, expressionData.operationOutputExpression!!)\r
82                 }\r
83                 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {\r
84                     valueNode = resolveArtifactExpression(nodeTemplateName, expressionData.artifactExpression!!)\r
85                 }\r
86                 BluePrintConstants.EXPRESSION_GET_NODE_OF_TYPE -> {\r
87 \r
88                 }\r
89                 else -> {\r
90                     throw BluePrintException(format("for property ({}), command ({}) is not supported ", propName, command))\r
91                 }\r
92             }\r
93         }\r
94         return valueNode\r
95     }\r
96 \r
97     /*\r
98     get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,\r
99     <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]\r
100  */\r
101     fun resolveAttributeExpression(nodeTemplateName: String, attributeExpression: AttributeExpression): JsonNode {\r
102         val valueNode: JsonNode\r
103 \r
104         val attributeName = attributeExpression.attributeName\r
105         val subAttributeName: String? = attributeExpression.subAttributeName\r
106 \r
107         var attributeNodeTemplateName = nodeTemplateName\r
108         when (attributeExpression.modelableEntityName) {\r
109             "ENV" -> {\r
110                 val environmentValue = System.getProperty(attributeName)\r
111                 valueNode = JacksonUtils.jsonNode(environmentValue)\r
112             }\r
113             else -> {\r
114                 if (!attributeExpression.modelableEntityName.equals("SELF", true)) {\r
115                     attributeNodeTemplateName = attributeExpression.modelableEntityName\r
116                 }\r
117                 /* Enable in ONAP Dublin Release\r
118                 val nodeTemplateAttributeExpression = bluePrintContext.nodeTemplateByName(attributeNodeTemplateName).attributes?.get(attributeName)\r
119                         ?: throw BluePrintException(format("failed to get attribute definitions for node template " +\r
120                                 "({})'s property name ({}) ", nodeTemplateName, attributeName))\r
121 \r
122                 var attributeDefinition: AttributeDefinition = bluePrintContext.nodeTemplateNodeType(attributeNodeTemplateName).attributes?.get(attributeName)!!\r
123 \r
124                 log.info("node template name ({}), property Name ({}) resolved value ({})", attributeNodeTemplateName, attributeName, nodeTemplateAttributeExpression)\r
125                 */\r
126 \r
127                 valueNode = bluePrintRuntimeService.getNodeTemplateAttributeValue(attributeNodeTemplateName, attributeName)\r
128                         ?: throw BluePrintException(format("failed to get node template ({})'s attribute ({}) ", nodeTemplateName, attributeName))\r
129             }\r
130 \r
131         }\r
132 //        subPropertyName?.let {\r
133 //            valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))\r
134 //        }\r
135         return valueNode\r
136     }\r
137 \r
138     /*\r
139         get_property: [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>,\r
140         <nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ]\r
141      */\r
142     fun resolvePropertyExpression(nodeTemplateName: String, propertyExpression: PropertyExpression): JsonNode {\r
143         val valueNode: JsonNode\r
144 \r
145         val propertyName = propertyExpression.propertyName\r
146         val subPropertyName: String? = propertyExpression.subPropertyName\r
147 \r
148         var propertyNodeTemplateName = nodeTemplateName\r
149         if (!propertyExpression.modelableEntityName.equals("SELF", true)) {\r
150             propertyNodeTemplateName = propertyExpression.modelableEntityName\r
151         }\r
152 \r
153         val nodeTemplatePropertyExpression = bluePrintContext.nodeTemplateByName(propertyNodeTemplateName).properties?.get(propertyName)\r
154                 ?: throw BluePrintException(format("failed to get property definitions for node template ({})'s property name ({}) ", nodeTemplateName, propertyName))\r
155 \r
156         var propertyDefinition: PropertyDefinition = bluePrintContext.nodeTemplateNodeType(propertyNodeTemplateName).properties?.get(propertyName)!!\r
157 \r
158         log.info("node template name ({}), property Name ({}) resolved value ({})", propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)\r
159 \r
160         // Check it it is a nested expression\r
161         valueNode = resolveAssignmentExpression(propertyNodeTemplateName, propertyName, nodeTemplatePropertyExpression)\r
162 \r
163 //        subPropertyName?.let {\r
164 //            valueNode = valueNode.at(JsonPointer.valueOf(subPropertyName))\r
165 //        }\r
166         return valueNode\r
167     }\r
168 \r
169     /*\r
170     get_operation_output: <modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>\r
171      */\r
172     fun resolveOperationOutputExpression(nodeTemplateName: String, operationOutputExpression: OperationOutputExpression): JsonNode {\r
173         var outputNodeTemplateName = nodeTemplateName\r
174         if (!operationOutputExpression.modelableEntityName.equals("SELF", true)) {\r
175             outputNodeTemplateName = operationOutputExpression.modelableEntityName\r
176         }\r
177         return bluePrintRuntimeService.getNodeTemplateOperationOutputValue(outputNodeTemplateName,\r
178                 operationOutputExpression.interfaceName, operationOutputExpression.operationName,\r
179                 operationOutputExpression.propertyName)\r
180     }\r
181 \r
182     /*\r
183     get_artifact: [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ]\r
184      */\r
185     fun resolveArtifactExpression(nodeTemplateName: String, artifactExpression: ArtifactExpression): JsonNode {\r
186 \r
187         var artifactNodeTemplateName = nodeTemplateName\r
188         if (!artifactExpression.modelableEntityName.equals("SELF", true)) {\r
189             artifactNodeTemplateName = artifactExpression.modelableEntityName\r
190         }\r
191         val artifactDefinition: ArtifactDefinition = bluePrintContext.nodeTemplateByName(artifactNodeTemplateName)\r
192                 .artifacts?.get(artifactExpression.artifactName)\r
193                 ?: throw BluePrintException(format("failed to get artifact definitions for node template ({})'s " +\r
194                         "artifact name ({}) ", nodeTemplateName, artifactExpression.artifactName))\r
195 \r
196         return JacksonUtils.jsonNodeFromObject(artifactContent(artifactDefinition))\r
197     }\r
198 \r
199     fun artifactContent(artifactDefinition: ArtifactDefinition): String {\r
200         val bluePrintBasePath: String = bluePrintRuntimeService.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)?.asText()\r
201                 ?: throw BluePrintException("failed to get property (${BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH}) from getStore")\r
202 \r
203         if (artifactDefinition.repository != null) {\r
204             TODO()\r
205         } else if (artifactDefinition.file != null) {\r
206             return ResourceResolverUtils.getFileContent(artifactDefinition.file!!, bluePrintBasePath)\r
207         }\r
208         return ""\r
209     }\r
210 }\r
211 \r