2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
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
39 * @author Brinda Santh
41 object BluePrintExpressionService {
43 val log = LoggerFactory.getLogger(this::class.toString())
46 fun checkContainsExpression(propertyAssignmentNode: JsonNode): Boolean {
47 val json = propertyAssignmentNode.toString()
48 // FIXME("Check if any Optimisation needed")
50 json.contains(BluePrintConstants.EXPRESSION_GET_INPUT) ||
51 json.contains(BluePrintConstants.EXPRESSION_GET_ATTRIBUTE) ||
52 json.contains(BluePrintConstants.EXPRESSION_GET_PROPERTY)
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
67 when (expressionData.command) {
68 BluePrintConstants.EXPRESSION_GET_INPUT -> {
69 expressionData.inputExpression = populateInputExpression(propertyAssignmentNode)
71 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
72 expressionData.attributeExpression = populateAttributeExpression(propertyAssignmentNode)
74 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
75 expressionData.propertyExpression = populatePropertyExpression(propertyAssignmentNode)
77 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
78 expressionData.operationOutputExpression = populateOperationOutputExpression(propertyAssignmentNode)
80 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
81 expressionData.artifactExpression = populateArtifactExpression(propertyAssignmentNode)
85 } else if (propertyAssignmentNode is TextNode &&
86 propertyAssignmentNode.textValue().startsWith(BluePrintConstants.EXPRESSION_DSL_REFERENCE)
88 expressionData.isExpression = true
89 expressionData.command = BluePrintConstants.EXPRESSION_DSL_REFERENCE
90 expressionData.dslExpression = populateDSLExpression(propertyAssignmentNode)
96 fun populateDSLExpression(jsonNode: TextNode): DSLExpression {
98 propertyName = jsonNode.textValue()
99 .removePrefix(BluePrintConstants.EXPRESSION_DSL_REFERENCE)
104 fun populateInputExpression(jsonNode: JsonNode): InputExpression {
105 return InputExpression(propertyName = jsonNode.first().textValue())
109 fun populatePropertyExpression(jsonNode: JsonNode): PropertyExpression {
110 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
111 check(arrayNode.size() >= 2) {
112 throw BluePrintException(
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 {}",
121 var reqOrCapEntityName: String? = null
122 var propertyName = ""
123 var subProperty: String? = null
125 arrayNode.size() == 2 -> propertyName = arrayNode[1].textValue()
126 arrayNode.size() == 3 -> {
127 reqOrCapEntityName = arrayNode[1].textValue()
128 propertyName = arrayNode[2].textValue()
130 arrayNode.size() > 3 -> {
131 reqOrCapEntityName = arrayNode[1].textValue()
132 propertyName = arrayNode[2].textValue()
133 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
135 }.map { it.textValue() }
136 subProperty = propertyPaths.joinToString(".")
140 return PropertyExpression(
141 modelableEntityName = arrayNode[0].asText(),
142 reqOrCapEntityName = reqOrCapEntityName,
143 propertyName = propertyName,
144 subPropertyName = subProperty
149 fun populateAttributeExpression(jsonNode: JsonNode): AttributeExpression {
150 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
151 check(arrayNode.size() >= 2) {
152 throw BluePrintException(
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 {}",
162 var reqOrCapEntityName: String? = null
163 var attributeName = ""
164 var subAttributeName: String? = null
166 arrayNode.size() == 2 -> attributeName = arrayNode[1].textValue()
167 arrayNode.size() == 3 -> {
168 reqOrCapEntityName = arrayNode[1].textValue()
169 attributeName = arrayNode[2].textValue()
171 arrayNode.size() > 3 -> {
172 reqOrCapEntityName = arrayNode[1].textValue()
173 attributeName = arrayNode[2].textValue()
174 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
176 }.map { it.textValue() }
177 subAttributeName = propertyPaths.joinToString(".")
180 return AttributeExpression(
181 modelableEntityName = arrayNode[0].asText(),
182 reqOrCapEntityName = reqOrCapEntityName,
183 attributeName = attributeName,
184 subAttributeName = subAttributeName
189 fun populateOperationOutputExpression(jsonNode: JsonNode): OperationOutputExpression {
190 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
192 check(arrayNode.size() >= 4) {
193 throw BluePrintException(
195 "missing operation output expression, " +
196 "it should be (<modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>) , but present {}",
202 var subPropertyName: String? = null
203 if (arrayNode.size() == 5)
204 subPropertyName = arrayNode[4].asText()
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
216 fun populateArtifactExpression(jsonNode: JsonNode): ArtifactExpression {
217 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
219 check(arrayNode.size() >= 2) {
220 throw BluePrintException(
222 "missing artifact expression, " +
223 "it should be [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ] , but present {}",
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