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