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