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