CLAMP should not display all CDS workflow properties
[clamp.git] / src / main / java / org / onap / clamp / clds / client / CdsServices.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP CLAMP\r
4  * ================================================================================\r
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  * ================================================================================\r
20  *\r
21  */\r
22 \r
23 package org.onap.clamp.clds.client;\r
24 \r
25 import static java.lang.Boolean.parseBoolean;\r
26 \r
27 import com.att.eelf.configuration.EELFLogger;\r
28 import com.att.eelf.configuration.EELFManager;\r
29 import com.google.gson.JsonElement;\r
30 import com.google.gson.JsonObject;\r
31 import com.google.gson.JsonParser;\r
32 \r
33 import java.util.Date;\r
34 import java.util.Map;\r
35 \r
36 import org.apache.camel.CamelContext;\r
37 import org.apache.camel.Exchange;\r
38 import org.apache.camel.builder.ExchangeBuilder;\r
39 import org.onap.clamp.clds.exception.cds.CdsParametersException;\r
40 import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;\r
41 import org.onap.clamp.clds.util.JsonUtils;\r
42 import org.onap.clamp.clds.util.LoggingUtils;\r
43 import org.springframework.beans.factory.annotation.Autowired;\r
44 import org.springframework.stereotype.Component;\r
45 \r
46 /**\r
47  * This class implements the communication with CDS for the service inventory.\r
48  */\r
49 @Component\r
50 public class CdsServices {\r
51 \r
52     @Autowired\r
53     CamelContext camelContext;\r
54 \r
55     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);\r
56 \r
57     private static final String TYPE = "type";\r
58     private static final String PROPERTIES = "properties";\r
59     private static final String LIST = "list";\r
60 \r
61     /**\r
62      * Constructor.\r
63      */\r
64     @Autowired\r
65     public CdsServices() {\r
66     }\r
67 \r
68 \r
69     /**\r
70      * Query CDS to get blueprint's workflow list.\r
71      *\r
72      * @param blueprintName    CDS blueprint name\r
73      * @param blueprintVersion CDS blueprint version\r
74      * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list\r
75      */\r
76     public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {\r
77         LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");\r
78 \r
79         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
80                 .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)\r
81                 .build();\r
82 \r
83         Exchange exchangeResponse = camelContext.createProducerTemplate()\r
84                 .send("direct:get-blueprint-workflow-list", myCamelExchange);\r
85 \r
86         if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
87             String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
88             logger.info("getBlueprintWorkflowList, response from CDS:" + cdsResponse);\r
89             LoggingUtils.setResponseContext("0", "Get Blueprint workflow list", this.getClass().getName());\r
90             Date startTime = new Date();\r
91             LoggingUtils.setTimeContext(startTime, new Date());\r
92             return JsonUtils.GSON_JPA_MODEL.fromJson(cdsResponse, CdsBpWorkFlowListResponse.class);\r
93         } else {\r
94             logger.error("CDS getBlueprintWorkflowList FAILED");\r
95             return null;\r
96         }\r
97 \r
98     }\r
99 \r
100     /**\r
101      * Query CDS to get input properties of workflow.\r
102      *\r
103      * @param blueprintName    CDS blueprint name\r
104      * @param blueprintVersion CDS blueprint name\r
105      * @param workflow         CDS blueprint's workflow\r
106      * @return input properties in json format\r
107      */\r
108     public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,\r
109                                                  String workflow) {\r
110         LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");\r
111 \r
112         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
113                 .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))\r
114                 .build();\r
115 \r
116         Exchange exchangeResponse = camelContext.createProducerTemplate()\r
117                 .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);\r
118 \r
119         if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
120             String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
121             logger.info("getWorkflowInputProperties, response from CDS:" + cdsResponse);\r
122             LoggingUtils.setResponseContext("0", "Get Blueprint workflow input properties", this.getClass().getName());\r
123             Date startTime = new Date();\r
124             LoggingUtils.setTimeContext(startTime, new Date());\r
125             return parseCdsResponse(cdsResponse);\r
126         } else {\r
127             logger.error("CDS getWorkflowInputProperties FAILED");\r
128             return null;\r
129         }\r
130     }\r
131 \r
132     protected JsonObject parseCdsResponse(String response) {\r
133         JsonObject root = JsonParser.parseString(response).getAsJsonObject();\r
134         JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");\r
135         JsonObject dataTypes = root.getAsJsonObject("dataTypes");\r
136 \r
137         JsonObject workFlowProperties = new JsonObject();\r
138         workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes, new JsonObject()));\r
139         return workFlowProperties;\r
140     }\r
141 \r
142     private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes,\r
143                                           JsonObject inputObject) {\r
144         if (inputs == null) {\r
145             return inputObject;\r
146         }\r
147 \r
148         for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {\r
149             String key = entry.getKey();\r
150             JsonObject inputProperty = inputs.getAsJsonObject(key);\r
151             String type = inputProperty.get(TYPE).getAsString();\r
152             if (isComplexType(type, dataTypes)) {\r
153                 inputObject.add(key, handleComplexType(type, dataTypes));\r
154             } else if (LIST.equalsIgnoreCase(type)) {\r
155                 handleListType(key, inputProperty, dataTypes, inputObject);\r
156             } else if (isInputParam(inputProperty)) {\r
157                 inputObject.add(key, entry.getValue());\r
158             }\r
159         }\r
160         return inputObject;\r
161     }\r
162 \r
163     private void handleListType(String propertyName,\r
164                                       JsonObject inputProperty,\r
165                                       JsonObject dataTypes,\r
166                                       JsonObject inputObject) {\r
167         if (inputProperty.get("entry_schema") == null) {\r
168             throw new CdsParametersException("Entry schema is null for " + propertyName);\r
169         }\r
170 \r
171         String type = inputProperty.get("entry_schema").getAsJsonObject().get(\r
172                 TYPE).getAsString();\r
173         if (dataTypes.get(type) != null) {\r
174             JsonObject jsonObject = new JsonObject();\r
175             jsonObject.addProperty(TYPE, LIST);\r
176             jsonObject.add(PROPERTIES, getPropertiesObject(type, dataTypes));\r
177             inputObject.add(propertyName, jsonObject);\r
178         } else if (isInputParam(inputProperty)) {\r
179             inputObject.add(propertyName, inputProperty);\r
180         }\r
181     }\r
182 \r
183     private JsonObject handleComplexType(String key, JsonObject dataTypes) {\r
184         JsonObject jsonObject = new JsonObject();\r
185         jsonObject.addProperty(TYPE, "object");\r
186         jsonObject.add(PROPERTIES, getPropertiesObject(key, dataTypes));\r
187         return jsonObject;\r
188     }\r
189 \r
190     private JsonObject getPropertiesObject(String key, JsonObject dataTypes) {\r
191         JsonObject properties = dataTypes.get(key).getAsJsonObject().get(PROPERTIES).getAsJsonObject();\r
192         JsonObject object = new JsonObject();\r
193         getInputProperties(properties, dataTypes, object);\r
194         return object;\r
195     }\r
196 \r
197     private boolean isComplexType(String type, JsonObject dataTypes) {\r
198         if (dataTypes == null) {\r
199             return false;\r
200         }\r
201         return dataTypes.get(type) != null;\r
202     }\r
203 \r
204     private boolean isInputParam(JsonObject inputProperty) {\r
205         JsonElement inputParam = inputProperty.get("input-param");\r
206         if (inputParam == null) {\r
207             return false;\r
208         }\r
209         return parseBoolean(inputParam.getAsString());\r
210     }\r
211 \r
212     /**\r
213      * Creates payload to query CDS to get workflow input properties.\r
214      *\r
215      * @param blueprintName CDS blueprint name\r
216      * @param version       CDS blueprint version\r
217      * @param workflow      CDS blueprint workflow\r
218      * @return returns payload in json format\r
219      */\r
220     public String getCdsPayloadForWorkFlow(String blueprintName, String version, String workflow) {\r
221         JsonObject jsonObject = new JsonObject();\r
222         jsonObject.addProperty("blueprintName", blueprintName);\r
223         jsonObject.addProperty("version", version);\r
224         jsonObject.addProperty("returnContent", "json");\r
225         jsonObject.addProperty("workflowName", workflow);\r
226         jsonObject.addProperty("specType", "TOSCA");\r
227         return jsonObject.toString();\r
228     }\r
229 }\r