fa15e2778773af0c07a11bd3cb07ae500a8c73af
[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 com.att.eelf.configuration.EELFLogger;\r
26 import com.att.eelf.configuration.EELFManager;\r
27 import com.google.gson.JsonElement;\r
28 import com.google.gson.JsonObject;\r
29 import com.google.gson.JsonParser;\r
30 \r
31 import java.util.Date;\r
32 import java.util.Map;\r
33 \r
34 import org.apache.camel.CamelContext;\r
35 import org.apache.camel.Exchange;\r
36 import org.apache.camel.builder.ExchangeBuilder;\r
37 import org.onap.clamp.clds.exception.cds.CdsParametersException;\r
38 import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;\r
39 import org.onap.clamp.clds.util.JsonUtils;\r
40 import org.onap.clamp.clds.util.LoggingUtils;\r
41 import org.springframework.beans.factory.annotation.Autowired;\r
42 import org.springframework.stereotype.Component;\r
43 \r
44 /**\r
45  * This class implements the communication with CDS for the service inventory.\r
46  */\r
47 @Component\r
48 public class CdsServices {\r
49 \r
50     @Autowired\r
51     CamelContext camelContext;\r
52 \r
53     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);\r
54 \r
55     /**\r
56      * Constructor.\r
57      */\r
58     @Autowired\r
59     public CdsServices() {\r
60     }\r
61 \r
62 \r
63     /**\r
64      * Query CDS to get blueprint's workflow list.\r
65      *\r
66      * @param blueprintName    CDS blueprint name\r
67      * @param blueprintVersion CDS blueprint version\r
68      * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list\r
69      */\r
70     public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {\r
71         LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");\r
72 \r
73         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
74                 .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)\r
75                 .build();\r
76 \r
77         Exchange exchangeResponse = camelContext.createProducerTemplate()\r
78                 .send("direct:get-blueprint-workflow-list", myCamelExchange);\r
79 \r
80         if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
81             String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
82             logger.info("getBlueprintWorkflowList, response from CDS:" + cdsResponse);\r
83             LoggingUtils.setResponseContext("0", "Get Blueprint workflow list", this.getClass().getName());\r
84             Date startTime = new Date();\r
85             LoggingUtils.setTimeContext(startTime, new Date());\r
86             return JsonUtils.GSON_JPA_MODEL.fromJson(cdsResponse, CdsBpWorkFlowListResponse.class);\r
87         } else {\r
88             logger.error("CDS getBlueprintWorkflowList FAILED");\r
89             return null;\r
90         }\r
91 \r
92     }\r
93 \r
94     /**\r
95      * Query CDS to get input properties of workflow.\r
96      *\r
97      * @param blueprintName    CDS blueprint name\r
98      * @param blueprintVersion CDS blueprint name\r
99      * @param workflow         CDS blueprint's workflow\r
100      * @return input properties in json format\r
101      */\r
102     public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,\r
103                                                  String workflow) {\r
104         LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");\r
105 \r
106         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
107                 .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))\r
108                 .build();\r
109 \r
110         Exchange exchangeResponse = camelContext.createProducerTemplate()\r
111                 .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);\r
112 \r
113         if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
114             String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
115             logger.info("getWorkflowInputProperties, response from CDS:" + cdsResponse);\r
116             LoggingUtils.setResponseContext("0", "Get Blueprint workflow input properties", this.getClass().getName());\r
117             Date startTime = new Date();\r
118             LoggingUtils.setTimeContext(startTime, new Date());\r
119             return parseCdsResponse(cdsResponse);\r
120         } else {\r
121             logger.error("CDS getWorkflowInputProperties FAILED");\r
122             return null;\r
123         }\r
124     }\r
125 \r
126     protected JsonObject parseCdsResponse(String response) {\r
127         JsonObject root = JsonParser.parseString(response).getAsJsonObject();\r
128         JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");\r
129         JsonObject dataTypes = root.getAsJsonObject("dataTypes");\r
130 \r
131         JsonObject workFlowProperties = new JsonObject();\r
132         workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes));\r
133         return workFlowProperties;\r
134     }\r
135 \r
136     private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes) {\r
137         JsonObject inputObject = new JsonObject();\r
138         for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {\r
139             String key = entry.getKey();\r
140             JsonObject inputProperty = inputs.getAsJsonObject(key);\r
141             String type = inputProperty.get("type").getAsString();\r
142             if (isComplexType(type, dataTypes)) {\r
143                 inputObject.add(key, handleComplexType(type, dataTypes));\r
144             } else if (type.equalsIgnoreCase("list")) {\r
145                 inputObject.add(key, handleListType(key, inputProperty,\r
146                                                     dataTypes));\r
147             } else {\r
148                 inputObject.add(key, entry.getValue());\r
149             }\r
150         }\r
151         return inputObject;\r
152     }\r
153 \r
154     private JsonObject handleListType(String propertyName,\r
155                                       JsonObject inputProperty,\r
156                                       JsonObject dataTypes) {\r
157         if (inputProperty.get("entry_schema") != null) {\r
158             String type = inputProperty.get("entry_schema").getAsJsonObject().get(\r
159                             "type").getAsString();\r
160             if (dataTypes.get(type) != null) {\r
161                 JsonObject jsonObject = new JsonObject();\r
162                 jsonObject.addProperty("type", "list");\r
163                 jsonObject.add("properties", handleComplexType(type, dataTypes));\r
164                 return jsonObject;\r
165             } else {\r
166                 return inputProperty;\r
167             }\r
168         }\r
169         throw new CdsParametersException("Entry schema is null for " + propertyName);\r
170     }\r
171 \r
172     private JsonObject handleComplexType(String key, JsonObject dataTypes) {\r
173         JsonObject properties = dataTypes.get(key).getAsJsonObject().get("properties").getAsJsonObject();\r
174         return getInputProperties(properties, dataTypes);\r
175     }\r
176 \r
177     private boolean isComplexType(String type, JsonObject dataTypes) {\r
178         return dataTypes.get(type) != null;\r
179     }\r
180 \r
181     /**\r
182      * Creates payload to query CDS to get workflow input properties.\r
183      *\r
184      * @param blueprintName CDS blueprint name\r
185      * @param version       CDS blueprint version\r
186      * @param workflow      CDS blueprint workflow\r
187      * @return returns payload in json format\r
188      */\r
189     public String getCdsPayloadForWorkFlow(String blueprintName, String version, String workflow) {\r
190         JsonObject jsonObject = new JsonObject();\r
191         jsonObject.addProperty("blueprintName", blueprintName);\r
192         jsonObject.addProperty("version", version);\r
193         jsonObject.addProperty("returnContent", "json");\r
194         jsonObject.addProperty("workflowName", workflow);\r
195         jsonObject.addProperty("specType", "TOSCA");\r
196         return jsonObject.toString();\r
197     }\r
198 }\r