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