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