Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / features / model / utils / ExpressionUtils.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.features.model.utils;\r
19 \r
20 import java.io.IOException;\r
21 import java.util.HashMap;\r
22 import java.util.Iterator;\r
23 import java.util.Map;\r
24 import org.apache.commons.lang3.StringUtils;\r
25 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
26 import org.onap.ccsdk.features.model.data.PropertyDefinition;\r
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
28 import com.att.eelf.configuration.EELFLogger;\r
29 import com.att.eelf.configuration.EELFManager;\r
30 import com.fasterxml.jackson.databind.JsonNode;\r
31 import com.fasterxml.jackson.databind.ObjectMapper;\r
32 import com.fasterxml.jackson.databind.node.ObjectNode;\r
33 \r
34 public class ExpressionUtils {\r
35     private static EELFLogger logger = EELFManager.getInstance().getLogger(ExpressionUtils.class);\r
36 \r
37     private final SvcLogicContext context;\r
38     private final Map<String, String> inParams;\r
39 \r
40     public ExpressionUtils(final SvcLogicContext context, final Map<String, String> inParams) {\r
41         this.context = context;\r
42         this.inParams = inParams;\r
43     }\r
44 \r
45     public void populatePropertAssignmentsWithPrefix(String prefix, Map<String, PropertyDefinition> typeProperties,\r
46             Map<String, Object> templateProperties) throws IOException {\r
47         if (typeProperties != null && templateProperties != null) {\r
48             ObjectMapper mapper = new ObjectMapper();\r
49             String templatejsonContent = mapper.writeValueAsString(templateProperties);\r
50             logger.debug("Expression inputs ({}).", templatejsonContent);\r
51 \r
52             JsonNode rootArray = mapper.readTree(templatejsonContent);\r
53             processJsonExpression(rootArray);\r
54             if (rootArray != null) {\r
55                 Map<String, String> prefixParams = new HashMap<>();\r
56                 TransformationUtils.convertJson2RootProperties(prefixParams, rootArray);\r
57                 logger.info("Resolved inputs ({}).", rootArray);\r
58                 prefixParams.forEach((key, value) -> {\r
59                     this.inParams.put(prefix + "." + key, value);\r
60                 });\r
61             }\r
62         }\r
63     }\r
64 \r
65     public void populatePropertAssignments(Map<String, PropertyDefinition> typeProperties,\r
66             Map<String, Object> templateProperties) throws IOException {\r
67         if (typeProperties != null && templateProperties != null) {\r
68             ObjectMapper mapper = new ObjectMapper();\r
69             String templatejsonContent = mapper.writeValueAsString(templateProperties);\r
70             logger.info("Expression inputs ({}).", templatejsonContent);\r
71 \r
72             JsonNode rootArray = mapper.readTree(templatejsonContent);\r
73             processJsonExpression(rootArray);\r
74             TransformationUtils.convertJson2RootProperties(this.inParams, rootArray);\r
75             logger.info("Resolved inputs ({}).", rootArray);\r
76 \r
77         }\r
78     }\r
79 \r
80     public void populateOutPropertAssignments(Map<String, PropertyDefinition> typeProperties,\r
81             Map<String, Object> templateProperties) throws IOException {\r
82         if (typeProperties != null && templateProperties != null) {\r
83             ObjectMapper mapper = new ObjectMapper();\r
84             String templatejsonContent = mapper.writeValueAsString(templateProperties);\r
85             logger.info("Expression outputs ({}).", templatejsonContent);\r
86 \r
87             JsonNode rootArray = mapper.readTree(templatejsonContent);\r
88             processJsonExpression(rootArray);\r
89             logger.info("Resolved outputs ({}).", rootArray);\r
90         }\r
91     }\r
92 \r
93     public void processJsonExpression(JsonNode rootArray) throws IOException {\r
94         if (rootArray != null) {\r
95             Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
96             while (fields.hasNext()) {\r
97                 Map.Entry<String, JsonNode> entry = fields.next();\r
98                 processJsonNode(rootArray, entry.getKey(), entry.getValue());\r
99             }\r
100         }\r
101     }\r
102 \r
103     private void processJsonNode(JsonNode parentNode, String nodeName, JsonNode node) throws IOException {\r
104         if (node == null) {\r
105             // Do Nothing\r
106         } else if (node.isArray()) {\r
107             String[] a = new String[node.size()];\r
108 \r
109             for (int i = 0; i < a.length; i++) {\r
110                 processJsonNode(null, null, node.get(i));\r
111             }\r
112         } else if (node.isObject()) {\r
113 \r
114             Iterator<Map.Entry<String, JsonNode>> fields = node.fields();\r
115             while (fields.hasNext()) {\r
116                 Map.Entry<String, JsonNode> entry = fields.next();\r
117                 processJsonNode(node, entry.getKey(), entry.getValue());\r
118             }\r
119         } else if (node.isTextual()) {\r
120             boolean expression = checkExpressionContent(node.asText());\r
121             if (expression) {\r
122                 processExpressionContent(parentNode, nodeName, node);\r
123             }\r
124         }\r
125     }\r
126 \r
127     private boolean checkExpressionContent(String content) {\r
128         return (StringUtils.isNotBlank(content) && (content.contains(ConfigModelConstant.EXPRESSION_GET_INPUT)\r
129                 || content.contains(ConfigModelConstant.EXPRESSION_GET_ATTRIBUTE)\r
130                 || content.contains(ConfigModelConstant.EXPRESSION_SET_VALUE)));\r
131     }\r
132 \r
133     @SuppressWarnings("squid:S3776")\r
134     private void processExpressionContent(JsonNode parentNode, String nodeName, JsonNode node) throws IOException {\r
135 \r
136         if (node != null && StringUtils.isNotBlank(node.asText())) {\r
137             String content = node.asText();\r
138             ObjectMapper mapper = new ObjectMapper();\r
139             Map<String, String> expressionMap = mapper.readValue(content, Map.class);\r
140             boolean isExpressionNode = false;\r
141 \r
142             if (expressionMap != null) {\r
143                 String expressionValue = null;\r
144                 String expressionKey = null;\r
145 \r
146                 if (expressionMap.containsKey(ConfigModelConstant.EXPRESSION_GET_INPUT)) {\r
147                     isExpressionNode = true;\r
148                     expressionKey = expressionMap.get(ConfigModelConstant.EXPRESSION_GET_INPUT);\r
149                     expressionValue = resolveGetInputExpression(expressionKey, context);\r
150                     if (expressionValue == null) {\r
151                         expressionValue = resolveGetInputExpression("inputs." + expressionKey + ".default", context);\r
152                     }\r
153                 } else if (expressionMap.containsKey(ConfigModelConstant.EXPRESSION_GET_ATTRIBUTE)) {\r
154                     isExpressionNode = true;\r
155                     expressionValue =\r
156                             context.getAttribute(expressionMap.get(ConfigModelConstant.EXPRESSION_GET_ATTRIBUTE));\r
157                 } else if (expressionMap.containsKey(ConfigModelConstant.EXPRESSION_SET_VALUE)) {\r
158                     isExpressionNode = true;\r
159                     expressionKey = expressionMap.get(ConfigModelConstant.EXPRESSION_SET_VALUE);\r
160                     expressionValue = context.getAttribute(expressionKey);\r
161 \r
162                     if (StringUtils.isNotBlank(expressionKey)) {\r
163                         context.setAttribute(expressionKey, expressionValue);\r
164                     }\r
165                 }\r
166 \r
167                 if (isExpressionNode && expressionValue == null) {\r
168                     ((ObjectNode) parentNode).put(nodeName, "");\r
169                 }\r
170                 if (StringUtils.isNotBlank(expressionValue)) {\r
171                     if (expressionValue.trim().startsWith("[") || expressionValue.trim().startsWith("{")) {\r
172                         JsonNode valueNode = mapper.readTree(expressionValue);\r
173                         ((ObjectNode) parentNode).put(nodeName, valueNode);\r
174                     } else {\r
175                         ((ObjectNode) parentNode).put(nodeName, expressionValue);\r
176                     }\r
177                 }\r
178                 logger.debug("expression ({}), expression key ({}), expression Value ({})", expressionMap,\r
179                         expressionKey, expressionValue);\r
180             }\r
181         }\r
182     }\r
183 \r
184     private String resolveGetInputExpression(String key, final SvcLogicContext context) {\r
185         if (StringUtils.isNotBlank(key) && context != null) {\r
186             return context.getAttribute(key);\r
187         }\r
188         return null;\r
189     }\r
190 \r
191 }\r