2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.controllerblueprints.core.service
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.databind.node.ArrayNode
23 import com.fasterxml.jackson.databind.node.ObjectNode
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
32 * @author Brinda Santh
34 object BluePrintExpressionService {
35 val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
38 fun getExpressionData(propertyAssignmentNode: JsonNode): ExpressionData {
39 log.trace("Assignment Data/Expression : {}", propertyAssignmentNode)
40 val expressionData = ExpressionData(valueNode = propertyAssignmentNode)
41 if (propertyAssignmentNode is ObjectNode) {
43 val commands: Set<String> = propertyAssignmentNode.fieldNames().asSequence().toList().intersect(BluePrintTypes.validCommands())
44 if (commands.isNotEmpty()) {
45 expressionData.isExpression = true
46 expressionData.command = commands.first()
47 expressionData.expressionNode = propertyAssignmentNode
49 when (expressionData.command) {
50 BluePrintConstants.EXPRESSION_GET_INPUT -> {
51 expressionData.inputExpression = populateInputExpression(propertyAssignmentNode)
53 BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
54 expressionData.attributeExpression = populateAttributeExpression(propertyAssignmentNode)
56 BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
57 expressionData.propertyExpression = populatePropertyExpression(propertyAssignmentNode)
59 BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
60 expressionData.operationOutputExpression = populateOperationOutputExpression(propertyAssignmentNode)
62 BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
63 expressionData.artifactExpression = populateArtifactExpression(propertyAssignmentNode)
72 fun populateInputExpression(jsonNode: JsonNode): InputExpression {
73 return InputExpression(propertyName = jsonNode.first().textValue())
77 fun populatePropertyExpression(jsonNode: JsonNode): PropertyExpression {
78 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
79 check(arrayNode.size() >= 2) {
80 throw BluePrintException(String.format("missing property expression, " +
81 "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <property_name>, " +
82 "<nested_property_name_or_index_1>, ..., <nested_property_name_or_index_n> ] , but present {}", jsonNode))
84 var reqOrCapEntityName: String? = null
86 var subProperty: String? = null
88 arrayNode.size() == 2 -> propertyName = arrayNode[1].textValue()
89 arrayNode.size() == 3 -> {
90 reqOrCapEntityName = arrayNode[1].textValue()
91 propertyName = arrayNode[2].textValue()
93 arrayNode.size() > 3 -> {
94 reqOrCapEntityName = arrayNode[1].textValue()
95 propertyName = arrayNode[2].textValue()
96 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
98 }.map { it.textValue() }
99 subProperty = propertyPaths.joinToString("/")
103 return PropertyExpression(modelableEntityName = arrayNode[0].asText(),
104 reqOrCapEntityName = reqOrCapEntityName,
105 propertyName = propertyName,
106 subPropertyName = subProperty
111 fun populateAttributeExpression(jsonNode: JsonNode): AttributeExpression {
112 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
113 check(arrayNode.size() >= 2) {
114 throw BluePrintException(String.format("missing attribute expression, " +
115 "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <attribute_name>," +
116 " <nested_attribute_name_or_index_1>, ..., <nested_attribute_name_or_index_n> ] , but present {}", jsonNode))
119 var reqOrCapEntityName: String? = null
120 var attributeName = ""
121 var subAttributeName: String? = null
123 arrayNode.size() == 2 -> attributeName = arrayNode[1].textValue()
124 arrayNode.size() == 3 -> {
125 reqOrCapEntityName = arrayNode[1].textValue()
126 attributeName = arrayNode[2].textValue()
128 arrayNode.size() > 3 -> {
129 reqOrCapEntityName = arrayNode[1].textValue()
130 attributeName = arrayNode[2].textValue()
131 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
133 }.map { it.textValue() }
134 subAttributeName = propertyPaths.joinToString("/")
137 return AttributeExpression(modelableEntityName = arrayNode[0].asText(),
138 reqOrCapEntityName = reqOrCapEntityName,
139 attributeName = attributeName,
140 subAttributeName = subAttributeName
145 fun populateOperationOutputExpression(jsonNode: JsonNode): OperationOutputExpression {
146 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
148 check(arrayNode.size() >= 4) {
149 throw BluePrintException(String.format("missing operation output expression, " +
150 "it should be (<modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>) , but present {}", jsonNode))
152 return OperationOutputExpression(modelableEntityName = arrayNode[0].asText(),
153 interfaceName = arrayNode[1].asText(),
154 operationName = arrayNode[2].asText(),
155 propertyName = arrayNode[3].asText()
160 fun populateArtifactExpression(jsonNode: JsonNode): ArtifactExpression {
161 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
163 check(arrayNode.size() >= 2) {
164 throw BluePrintException(String.format("missing artifact expression, " +
165 "it should be [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ] , but present {}", jsonNode))
167 return ArtifactExpression(modelableEntityName = arrayNode[0].asText(),
168 artifactName = arrayNode[1].asText(),
169 location = arrayNode[2]?.asText() ?: "LOCAL_FILE",
170 remove = arrayNode[3]?.asBoolean() ?: false