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 com.att.eelf.configuration.EELFLogger
\r
27 import com.att.eelf.configuration.EELFManager
\r
31 * @author Brinda Santh
\r
33 object BluePrintExpressionService {
\r
34 val log: EELFLogger = EELFManager.getInstance().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 log.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
93 arrayNode.size() == 2 -> propertyName = arrayNode[1].textValue()
\r
94 arrayNode.size() == 3 -> {
\r
95 reqOrCapEntityName = arrayNode[1].textValue()
\r
96 propertyName = arrayNode[2].textValue()
\r
98 arrayNode.size() > 3 -> {
\r
99 reqOrCapEntityName = arrayNode[1].textValue()
\r
100 propertyName = arrayNode[2].textValue()
\r
101 val propertyPaths: List<String> = arrayNode.filterIndexed { index, obj ->
\r
103 }.map { it.textValue() }
\r
104 subProperty = propertyPaths.joinToString("/")
\r
108 return PropertyExpression(modelableEntityName = arrayNode[0].asText(),
\r
109 reqOrCapEntityName = reqOrCapEntityName,
\r
110 propertyName = propertyName,
\r
111 subPropertyName = subProperty
\r
116 fun populateAttributeExpression(jsonNode: JsonNode): AttributeExpression {
\r
117 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
118 check(arrayNode.size() >= 3) {
\r
119 throw BluePrintException(String.format("missing attribute expression, " +
\r
120 "it should be [ <modelable_entity_name>, <optional_req_or_cap_name>, <attribute_name>," +
\r
121 " <nested_attribute_name_or_index_1>, ..., <nested_attribute_name_or_index_n> ] , but present {}", jsonNode))
\r
124 var reqOrCapEntityName: String? = null
\r
125 var propertyName = ""
\r
126 var subProperty: String? = null
\r
127 if (arrayNode.size() == 2) {
\r
128 propertyName = arrayNode[1].textValue()
\r
129 } else if (arrayNode.size() == 3) {
\r
130 reqOrCapEntityName = arrayNode[1].textValue()
\r
131 propertyName = arrayNode[2].textValue()
\r
132 } else if (arrayNode.size() > 3) {
\r
133 reqOrCapEntityName = arrayNode[1].textValue()
\r
134 propertyName = arrayNode[2].textValue()
\r
135 val propertyPaths: List<String> = arrayNode.filterIndexed { index, obj ->
\r
137 }.map { it.textValue() }
\r
138 subProperty = propertyPaths.joinToString("/")
\r
140 return AttributeExpression(modelableEntityName = arrayNode[0].asText(),
\r
141 reqOrCapEntityName = reqOrCapEntityName,
\r
142 attributeName = propertyName,
\r
143 subAttributeName = subProperty
\r
148 fun populateOperationOutputExpression(jsonNode: JsonNode): OperationOutputExpression {
\r
149 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
151 check(arrayNode.size() >= 4) {
\r
152 throw BluePrintException(String.format("missing operation output expression, " +
\r
153 "it should be (<modelable_entity_name>, <interface_name>, <operation_name>, <output_variable_name>) , but present {}", jsonNode))
\r
155 return OperationOutputExpression(modelableEntityName = arrayNode[0].asText(),
\r
156 interfaceName = arrayNode[1].asText(),
\r
157 operationName = arrayNode[2].asText(),
\r
158 propertyName = arrayNode[3].asText()
\r
163 fun populateArtifactExpression(jsonNode: JsonNode): ArtifactExpression {
\r
164 val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
\r
166 check(arrayNode.size() >= 2) {
\r
167 throw BluePrintException(String.format("missing artifact expression, " +
\r
168 "it should be [ <modelable_entity_name>, <artifact_name>, <location>, <remove> ] , but present {}", jsonNode))
\r
170 return ArtifactExpression(modelableEntityName = arrayNode[0].asText(),
\r
171 artifactName = arrayNode[1].asText(),
\r
172 location = arrayNode[2]?.asText() ?: "LOCAL_FILE",
\r
173 remove = arrayNode[3]?.asBoolean() ?: false
\r