Merge "Payload attributes are not displayed in operational policy"
[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         }\r
88         return null;\r
89     }\r
90 \r
91     /**\r
92      * Query CDS to get input properties of workflow.\r
93      *\r
94      * @param blueprintName    CDS blueprint name\r
95      * @param blueprintVersion CDS blueprint name\r
96      * @param workflow         CDS blueprint's workflow\r
97      * @return input properties in json format\r
98      */\r
99     public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,\r
100                                                  String workflow) {\r
101         LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");\r
102 \r
103         Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
104                 .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))\r
105                 .build();\r
106 \r
107         Exchange exchangeResponse = camelContext.createProducerTemplate()\r
108                 .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);\r
109 \r
110         if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
111             String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
112             logger.info("getWorkflowInputProperties, response from CDS:" + cdsResponse);\r
113             LoggingUtils.setResponseContext("0", "Get Blueprint workflow input properties", this.getClass().getName());\r
114             Date startTime = new Date();\r
115             LoggingUtils.setTimeContext(startTime, new Date());\r
116             return parseCdsResponse(cdsResponse);\r
117         }\r
118         return null;\r
119     }\r
120 \r
121     protected JsonObject parseCdsResponse(String response) {\r
122         JsonObject root = JsonParser.parseString(response).getAsJsonObject();\r
123         JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");\r
124         JsonObject dataTypes = root.getAsJsonObject("dataTypes");\r
125 \r
126         JsonObject workFlowProperties = new JsonObject();\r
127         workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes));\r
128         return workFlowProperties;\r
129     }\r
130 \r
131     private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes) {\r
132         JsonObject inputObject = new JsonObject();\r
133         for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {\r
134             String key = entry.getKey();\r
135             JsonObject inputProperty = inputs.getAsJsonObject(key);\r
136             String type = inputProperty.get("type").getAsString();\r
137             if (isComplexType(type, dataTypes)) {\r
138                 inputObject.add(key, handleComplexType(type, dataTypes));\r
139             } else if (type.equalsIgnoreCase("list")) {\r
140                 inputObject.add(key, handleListType(key, inputProperty,\r
141                                                     dataTypes));\r
142             } else {\r
143                 inputObject.add(key, entry.getValue());\r
144             }\r
145         }\r
146         return inputObject;\r
147     }\r
148 \r
149     private JsonObject handleListType(String propertyName,\r
150                                       JsonObject inputProperty,\r
151                                       JsonObject dataTypes) {\r
152         if (inputProperty.get("entry_schema") != null) {\r
153             String type = inputProperty.get("entry_schema").getAsJsonObject().get(\r
154                             "type").getAsString();\r
155             if (dataTypes.get(type) != null) {\r
156                 JsonObject jsonObject = new JsonObject();\r
157                 jsonObject.addProperty("type", "list");\r
158                 jsonObject.add("properties", handleComplexType(type, dataTypes));\r
159                 return jsonObject;\r
160             } else {\r
161                 return inputProperty;\r
162             }\r
163         }\r
164         throw new CdsParametersException("Entry schema is null for " + propertyName);\r
165     }\r
166 \r
167     private JsonObject handleComplexType(String key, JsonObject dataTypes) {\r
168         JsonObject properties = dataTypes.get(key).getAsJsonObject().get("properties").getAsJsonObject();\r
169         return getInputProperties(properties, dataTypes);\r
170     }\r
171 \r
172     private boolean isComplexType(String type, JsonObject dataTypes) {\r
173         return dataTypes.get(type) != null;\r
174     }\r
175 \r
176     /**\r
177      * Creates payload to query CDS to get workflow input properties.\r
178      *\r
179      * @param blueprintName CDS blueprint name\r
180      * @param version       CDS blueprint version\r
181      * @param workflow      CDS blueprint workflow\r
182      * @return returns payload in json format\r
183      */\r
184     public String getCdsPayloadForWorkFlow(String blueprintName, String version, String workflow) {\r
185         JsonObject jsonObject = new JsonObject();\r
186         jsonObject.addProperty("blueprintName", blueprintName);\r
187         jsonObject.addProperty("version", version);\r
188         jsonObject.addProperty("returnContent", "json");\r
189         jsonObject.addProperty("workflowName", workflow);\r
190         jsonObject.addProperty("specType", "TOSCA");\r
191         return jsonObject.toString();\r
192     }\r
193 }\r