Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintExpressionService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
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.cds.controllerblueprints.core.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.ArrayNode
22 import com.fasterxml.jackson.databind.node.ObjectNode
23 import com.fasterxml.jackson.databind.node.TextNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactExpression
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.AttributeExpression
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.DSLExpression
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.ExpressionData
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.InputExpression
32 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationOutputExpression
33 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyExpression
34 import org.slf4j.LoggerFactory
35
36 /**
37  *
38  *
39  * @author Brinda Santh
40  */
41 object BluePrintExpressionService {
42
43     val log = LoggerFactory.getLogger(this::class.toString())
44
45     @JvmStatic
46     fun checkContainsExpression(propertyAssignmentNode: JsonNode): Boolean {
47         val json = propertyAssignmentNode.toString()
48         // FIXME("Check if any Optimisation needed")
49         return (
50             json.contains(BluePrintConstants.EXPRESSION_GET_INPUT) ||
51                 json.contains(BluePrintConstants.EXPRESSION_GET_ATTRIBUTE) ||
52                 json.contains(BluePrintConstants.EXPRESSION_GET_PROPERTY)
53             )
54     }
55
56     @JvmStatic
57     fun getExpressionData(propertyAssignmentNode: JsonNode): ExpressionData {
58         log.trace("Assignment Data/Expression : {}", propertyAssignmentNode)
59         val expressionData = ExpressionData(valueNode = propertyAssignmentNode)
60         if (propertyAssignmentNode is ObjectNode) {
61             val commands: Set<String> = propertyAssignmentNode.fieldNames().asSequence().toList().intersect(BluePrintTypes.validCommands())
62             if (commands.isNotEmpty()) {
63                 expressionData.isExpression = true
64                 expressionData.command = commands.first()
65                 expressionData.expressionNode = propertyAssignmentNode
66
67                 when (expressionData.command) {
68                     BluePrintConstants.EXPRESSION_GET_INPUT -> {
69                         expressionData.inputExpression = populateInputExpression(propertyAssignmentNode)
70                     }
71                     BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
72                         expressionData.attributeExpression = populateAttributeExpression(propertyAssignmentNode)
73                     }
74                     BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
75                         expressionData.propertyExpression = populatePropertyExpression(propertyAssignmentNode)
76                     }
77                     BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
78                         expressionData.operationOutputExpression = populateOperationOutputExpression(propertyAssignmentNode)
79                     }
80                     BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
81                         expressionData.artifactExpression = populateArtifactExpression(propertyAssignmentNode)
82                     }
83                 }
84             }
85         } else if (propertyAssignmentNode is TextNode &&
86             propertyAssignmentNode.textValue().startsWith(BluePrintConstants.EXPRESSION_DSL_REFERENCE)
87         ) {
88             expressionData.isExpression = true
89             expressionData.command = BluePrintConstants.EXPRESSION_DSL_REFERENCE
90             expressionData.dslExpression = populateDSLExpression(propertyAssignmentNode)
91         }
92         return expressionData
93     }
94
95     @JvmStatic
96     fun populateDSLExpression(jsonNode: TextNode): DSLExpression {
97         return DSLExpression(
98             propertyName = jsonNode.textValue()
99                 .removePrefix(BluePrintConstants.EXPRESSION_DSL_REFERENCE)
100         )
101     }
102
103     @JvmStatic
104     fun populateInputExpression(jsonNode: JsonNode): InputExpression {
105         return InputExpression(propertyName = jsonNode.first().textValue())
106     }
107
108     @JvmStatic
109     fun populatePropertyExpression(jsonNode: JsonNode): PropertyExpression {
110         val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
111         check(arrayNode.size() >= 2) {
112             throw BluePrintException(
113                 String.format(
114                     "missing property expression, " +
115                         "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>, " +
116                         "<nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ] , but present {}",
117                     jsonNode
118                 )
119             )
120         }
121         var reqOrCapEntityName: String? = null
122         var propertyName = ""
123         var subProperty: String? = null
124         when {
125             arrayNode.size() == 2 -> propertyName = arrayNode[1].textValue()
126             arrayNode.size() == 3 -> {
127                 reqOrCapEntityName = arrayNode[1].textValue()
128                 propertyName = arrayNode[2].textValue()
129             }
130             arrayNode.size() > 3 -> {
131                 reqOrCapEntityName = arrayNode[1].textValue()
132                 propertyName = arrayNode[2].textValue()
133                 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
134                     index >= 3
135                 }.map { it.textValue() }
136                 subProperty = propertyPaths.joinToString(".")
137             }
138         }
139
140         return PropertyExpression(
141             modelableEntityName = arrayNode[0].asText(),
142             reqOrCapEntityName = reqOrCapEntityName,
143             propertyName = propertyName,
144             subPropertyName = subProperty
145         )
146     }
147
148     @JvmStatic
149     fun populateAttributeExpression(jsonNode: JsonNode): AttributeExpression {
150         val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
151         check(arrayNode.size() >= 2) {
152             throw BluePrintException(
153                 String.format(
154                     "missing attribute expression, " +
155                         "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <attribute_name>," +
156                         " <nested_attribute_name_or_index_1>, ..., <nested_attribute_name_or_index_n> ] , but present {}",
157                     jsonNode
158                 )
159             )
160         }
161
162         var reqOrCapEntityName: String? = null
163         var attributeName = ""
164         var subAttributeName: String? = null
165         when {
166             arrayNode.size() == 2 -> attributeName = arrayNode[1].textValue()
167             arrayNode.size() == 3 -> {
168                 reqOrCapEntityName = arrayNode[1].textValue()
169                 attributeName = arrayNode[2].textValue()
170             }
171             arrayNode.size() > 3 -> {
172                 reqOrCapEntityName = arrayNode[1].textValue()
173                 attributeName = arrayNode[2].textValue()
174                 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
175                     index >= 3
176                 }.map { it.textValue() }
177                 subAttributeName = propertyPaths.joinToString(".")
178             }
179         }
180         return AttributeExpression(
181             modelableEntityName = arrayNode[0].asText(),
182             reqOrCapEntityName = reqOrCapEntityName,
183             attributeName = attributeName,
184             subAttributeName = subAttributeName
185         )
186     }
187
188     @JvmStatic
189     fun populateOperationOutputExpression(jsonNode: JsonNode): OperationOutputExpression {
190         val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
191
192         check(arrayNode.size() >= 4) {
193             throw BluePrintException(
194                 String.format(
195                     "missing operation output expression, " +
196                         "it should be (<modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>) , but present {}",
197                     jsonNode
198                 )
199             )
200         }
201
202         var subPropertyName: String? = null
203         if (arrayNode.size() == 5)
204             subPropertyName = arrayNode[4].asText()
205
206         return OperationOutputExpression(
207             modelableEntityName = arrayNode[0].asText(),
208             interfaceName = arrayNode[1].asText(),
209             operationName = arrayNode[2].asText(),
210             propertyName = arrayNode[3].asText(),
211             subPropertyName = subPropertyName
212         )
213     }
214
215     @JvmStatic
216     fun populateArtifactExpression(jsonNode: JsonNode): ArtifactExpression {
217         val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
218
219         check(arrayNode.size() >= 2) {
220             throw BluePrintException(
221                 String.format(
222                     "missing artifact expression, " +
223                         "it should be [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ] , but present {}",
224                     jsonNode
225                 )
226             )
227         }
228         return ArtifactExpression(
229             modelableEntityName = arrayNode[0].asText(),
230             artifactName = arrayNode[1].asText(),
231             location = arrayNode[2]?.asText() ?: "LOCAL_FILE",
232             remove = arrayNode[3]?.asBoolean() ?: false
233         )
234     }
235 }