2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.core.service
\r
19 import com.fasterxml.jackson.databind.JsonNode
\r
20 import com.fasterxml.jackson.databind.node.ArrayNode
\r
21 import com.fasterxml.jackson.databind.node.ObjectNode
\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
\r
26 import org.slf4j.Logger
\r
27 import org.slf4j.LoggerFactory
\r
31 * @author Brinda Santh
\r
33 object BluePrintExpressionService {
\r
34 val logger: Logger = LoggerFactory.getLogger(this::class.toString())
\r
37 fun getExpressionData(propertyAssignment: Any): ExpressionData {
\r
38 val propertyAssignmentNode: JsonNode = JacksonUtils.jsonNodeFromObject(propertyAssignment)
\r
39 return getExpressionData(propertyAssignmentNode)
\r
43 fun getExpressionData(propertyAssignmentNode: JsonNode): ExpressionData {
\r
44 logger.trace("Assignment Data/Expression : {}", propertyAssignmentNode)
\r
45 val expressionData = ExpressionData(valueNode = propertyAssignmentNode)
\r
46 if (propertyAssignmentNode is ObjectNode) {
\r
48 val commands: Set<String> = propertyAssignmentNode.fieldNames().asSequence().toList().intersect(BluePrintTypes.validCommands())
\r
49 if (commands.isNotEmpty()) {
\r
50 expressionData.isExpression = true
\r
51 expressionData.command = commands.first()
\r
52 expressionData.expressionNode = propertyAssignmentNode
\r
54 when (expressionData.command) {
\r
55 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_INPUT -> {
\r
56 expressionData.inputExpression = populateInputExpression(propertyAssignmentNode)
\r
58 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
\r
59 expressionData.attributeExpression = populateAttributeExpression(propertyAssignmentNode)
\r
61 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
\r
62 expressionData.propertyExpression = populatePropertyExpression(propertyAssignmentNode)
\r
64 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
\r
65 expressionData.operationOutputExpression = populateOperationOutputExpression(propertyAssignmentNode)
\r
67 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
\r
68 expressionData.artifactExpression = populateArtifactExpression(propertyAssignmentNode)
\r
73 return expressionData
\r
77 fun populateInputExpression(jsonNode: JsonNode): InputExpression {
\r
78 return InputExpression(propertyName = jsonNode.first().textValue())
\r
82 fun populatePropertyExpression(jsonNode: JsonNode): PropertyExpression {
\r
83 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
84 check(arrayNode.size() >= 2) {
\r
85 throw BluePrintException(String.format("missing property expression, " +
\r
86 "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>, " +
\r
87 "<nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ] , but present {}", jsonNode))
\r
89 var reqOrCapEntityName: String? = null
\r
90 var propertyName = ""
\r
91 var subProperty: String? = null
\r
92 if (arrayNode.size() == 2) {
\r
93 propertyName = arrayNode[1].textValue()
\r
94 } else if (arrayNode.size() == 3) {
\r
95 reqOrCapEntityName = arrayNode[1].textValue()
\r
96 propertyName = arrayNode[2].textValue()
\r
97 } else if (arrayNode.size() > 3) {
\r
98 reqOrCapEntityName = arrayNode[1].textValue()
\r
99 propertyName = arrayNode[2].textValue()
\r
100 val propertyPaths: List<String> = arrayNode.filterIndexed { index, obj ->
\r
102 }.map { it.textValue() }
\r
103 subProperty = propertyPaths.joinToString("/")
\r
106 return PropertyExpression(modelableEntityName = arrayNode[0].asText(),
\r
107 reqOrCapEntityName = reqOrCapEntityName,
\r
108 propertyName = propertyName,
\r
109 subPropertyName = subProperty
\r
114 fun populateAttributeExpression(jsonNode: JsonNode): AttributeExpression {
\r
115 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
116 check(arrayNode.size() >= 3) {
\r
117 throw BluePrintException(String.format("missing attribute expression, " +
\r
118 "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <attribute_name>," +
\r
119 " <nested_attribute_name_or_index_1>, ..., <nested_attribute_name_or_index_n> ] , but present {}", jsonNode))
\r
122 var reqOrCapEntityName: String? = null
\r
123 var propertyName: String = ""
\r
124 var subProperty: String? = null
\r
125 if (arrayNode.size() == 2) {
\r
126 propertyName = arrayNode[1].textValue()
\r
127 } else if (arrayNode.size() == 3) {
\r
128 reqOrCapEntityName = arrayNode[1].textValue()
\r
129 propertyName = arrayNode[2].textValue()
\r
130 } else if (arrayNode.size() > 3) {
\r
131 reqOrCapEntityName = arrayNode[1].textValue()
\r
132 propertyName = arrayNode[2].textValue()
\r
133 val propertyPaths: List<String> = arrayNode.filterIndexed { index, obj ->
\r
135 }.map { it.textValue() }
\r
136 subProperty = propertyPaths.joinToString("/")
\r
138 return AttributeExpression(modelableEntityName = arrayNode[0].asText(),
\r
139 reqOrCapEntityName = reqOrCapEntityName,
\r
140 attributeName = propertyName,
\r
141 subAttributeName = subProperty
\r
146 fun populateOperationOutputExpression(jsonNode: JsonNode): OperationOutputExpression {
\r
147 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
149 check(arrayNode.size() >= 4) {
\r
150 throw BluePrintException(String.format("missing operation output expression, " +
\r
151 "it should be (<modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>) , but present {}", jsonNode))
\r
153 return OperationOutputExpression(modelableEntityName = arrayNode[0].asText(),
\r
154 interfaceName = arrayNode[1].asText(),
\r
155 operationName = arrayNode[2].asText(),
\r
156 propertyName = arrayNode[3].asText()
\r
161 fun populateArtifactExpression(jsonNode: JsonNode): ArtifactExpression {
\r
162 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
164 check(arrayNode.size() >= 2) {
\r
165 throw BluePrintException(String.format("missing artifact expression, " +
\r
166 "it should be [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ] , but present {}", jsonNode))
\r
168 return ArtifactExpression(modelableEntityName = arrayNode[0].asText(),
\r
169 artifactName = arrayNode[1].asText(),
\r
170 location = arrayNode[2]?.asText() ?: "LOCAL_FILE",
\r
171 remove = arrayNode[3]?.asBoolean() ?: false
\r