caa02dbce501130b6d1a54b2fb8bf76a91885142
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.core.service
18
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.*
28
29 /**
30  *
31  *
32  * @author Brinda Santh
33  */
34 object BluePrintExpressionService {
35     val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
36
37     @JvmStatic
38     fun getExpressionData(propertyAssignmentNode: JsonNode): ExpressionData {
39         log.trace("Assignment Data/Expression : {}", propertyAssignmentNode)
40         val expressionData = ExpressionData(valueNode = propertyAssignmentNode)
41         if (propertyAssignmentNode is ObjectNode) {
42
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
48
49                 when (expressionData.command) {
50                     BluePrintConstants.EXPRESSION_GET_INPUT -> {
51                         expressionData.inputExpression = populateInputExpression(propertyAssignmentNode)
52                     }
53                     BluePrintConstants.EXPRESSION_GET_ATTRIBUTE -> {
54                         expressionData.attributeExpression = populateAttributeExpression(propertyAssignmentNode)
55                     }
56                     BluePrintConstants.EXPRESSION_GET_PROPERTY -> {
57                         expressionData.propertyExpression = populatePropertyExpression(propertyAssignmentNode)
58                     }
59                     BluePrintConstants.EXPRESSION_GET_OPERATION_OUTPUT -> {
60                         expressionData.operationOutputExpression = populateOperationOutputExpression(propertyAssignmentNode)
61                     }
62                     BluePrintConstants.EXPRESSION_GET_ARTIFACT -> {
63                         expressionData.artifactExpression = populateArtifactExpression(propertyAssignmentNode)
64                     }
65                 }
66             }
67         }
68         return expressionData
69     }
70
71     @JvmStatic
72     fun populateInputExpression(jsonNode: JsonNode): InputExpression {
73         return InputExpression(propertyName = jsonNode.first().textValue())
74     }
75
76     @JvmStatic
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))
83         }
84         var reqOrCapEntityName: String? = null
85         var propertyName = ""
86         var subProperty: String? = null
87         when {
88             arrayNode.size() == 2 -> propertyName = arrayNode[1].textValue()
89             arrayNode.size() == 3 -> {
90                 reqOrCapEntityName = arrayNode[1].textValue()
91                 propertyName = arrayNode[2].textValue()
92             }
93             arrayNode.size() > 3 -> {
94                 reqOrCapEntityName = arrayNode[1].textValue()
95                 propertyName = arrayNode[2].textValue()
96                 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
97                     index >= 3
98                 }.map { it.textValue() }
99                 subProperty = propertyPaths.joinToString("/")
100             }
101         }
102
103         return PropertyExpression(modelableEntityName = arrayNode[0].asText(),
104                 reqOrCapEntityName = reqOrCapEntityName,
105                 propertyName = propertyName,
106                 subPropertyName = subProperty
107         )
108     }
109
110     @JvmStatic
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))
117         }
118
119         var reqOrCapEntityName: String? = null
120         var attributeName = ""
121         var subAttributeName: String? = null
122         when {
123             arrayNode.size() == 2 -> attributeName = arrayNode[1].textValue()
124             arrayNode.size() == 3 -> {
125                 reqOrCapEntityName = arrayNode[1].textValue()
126                 attributeName = arrayNode[2].textValue()
127             }
128             arrayNode.size() > 3 -> {
129                 reqOrCapEntityName = arrayNode[1].textValue()
130                 attributeName = arrayNode[2].textValue()
131                 val propertyPaths: List<String> = arrayNode.filterIndexed { index, _ ->
132                     index >= 3
133                 }.map { it.textValue() }
134                 subAttributeName = propertyPaths.joinToString("/")
135             }
136         }
137         return AttributeExpression(modelableEntityName = arrayNode[0].asText(),
138                 reqOrCapEntityName = reqOrCapEntityName,
139                 attributeName = attributeName,
140                 subAttributeName = subAttributeName
141         )
142     }
143
144     @JvmStatic
145     fun populateOperationOutputExpression(jsonNode: JsonNode): OperationOutputExpression {
146         val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
147
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))
151         }
152         return OperationOutputExpression(modelableEntityName = arrayNode[0].asText(),
153                 interfaceName = arrayNode[1].asText(),
154                 operationName = arrayNode[2].asText(),
155                 propertyName = arrayNode[3].asText()
156         )
157     }
158
159     @JvmStatic
160     fun populateArtifactExpression(jsonNode: JsonNode): ArtifactExpression {
161         val arrayNode: ArrayNode = jsonNode.first() as ArrayNode
162
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))
166         }
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
171         )
172     }
173 }