+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * ONAP CLAMP\r
+ * ================================================================================\r
+ * Copyright (C) 2020 Huawei Technologies Co., Ltd.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ * ================================================================================\r
+ *\r
+ */\r
+\r
+package org.onap.clamp.clds.client;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+import com.google.gson.JsonElement;\r
+import com.google.gson.JsonObject;\r
+import com.google.gson.JsonParser;\r
+\r
+import java.util.Date;\r
+import java.util.Map;\r
+\r
+import org.apache.camel.CamelContext;\r
+import org.apache.camel.Exchange;\r
+import org.apache.camel.builder.ExchangeBuilder;\r
+import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;\r
+import org.onap.clamp.clds.util.JsonUtils;\r
+import org.onap.clamp.clds.util.LoggingUtils;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Component;\r
+\r
+/**\r
+ * This class implements the communication with CDS for the service inventory.\r
+ */\r
+@Component\r
+public class CdsServices {\r
+\r
+ @Autowired\r
+ CamelContext camelContext;\r
+\r
+ protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);\r
+\r
+ /**\r
+ * Constructor.\r
+ */\r
+ @Autowired\r
+ public CdsServices() {\r
+ }\r
+\r
+\r
+ /**\r
+ * Query CDS to get blueprint's workflow list.\r
+ *\r
+ * @param blueprintName CDS blueprint name\r
+ * @param blueprintVersion CDS blueprint version\r
+ * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list\r
+ */\r
+ public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {\r
+ LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");\r
+\r
+ Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
+ .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)\r
+ .build();\r
+\r
+ Exchange exchangeResponse = camelContext.createProducerTemplate()\r
+ .send("direct:get-blueprint-workflow-list", myCamelExchange);\r
+\r
+ if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
+ String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
+ logger.info("getBlueprintWorkflowList, response from CDS:" + cdsResponse);\r
+ LoggingUtils.setResponseContext("0", "Get Blueprint workflow list", this.getClass().getName());\r
+ Date startTime = new Date();\r
+ LoggingUtils.setTimeContext(startTime, new Date());\r
+ return JsonUtils.GSON_JPA_MODEL.fromJson(cdsResponse, CdsBpWorkFlowListResponse.class);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Query CDS to get input properties of workflow.\r
+ *\r
+ * @param blueprintName CDS blueprint name\r
+ * @param blueprintVersion CDS blueprint name\r
+ * @param workflow CDS blueprint's workflow\r
+ * @return input properties in json format\r
+ */\r
+ public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,\r
+ String workflow) {\r
+ LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");\r
+\r
+ Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)\r
+ .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))\r
+ .build();\r
+\r
+ Exchange exchangeResponse = camelContext.createProducerTemplate()\r
+ .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);\r
+\r
+ if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {\r
+ String cdsResponse = (String) exchangeResponse.getIn().getBody();\r
+ logger.info("getWorkflowInputProperties, response from CDS:" + cdsResponse);\r
+ LoggingUtils.setResponseContext("0", "Get Blueprint workflow input properties", this.getClass().getName());\r
+ Date startTime = new Date();\r
+ LoggingUtils.setTimeContext(startTime, new Date());\r
+ return parseCdsResponse(cdsResponse);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private JsonObject parseCdsResponse(String response) {\r
+ JsonObject root = JsonParser.parseString(response).getAsJsonObject();\r
+ JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");\r
+ JsonObject dataTypes = root.getAsJsonObject("dataTypes");\r
+\r
+ JsonObject workFlowProperties = new JsonObject();\r
+ workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes));\r
+ return workFlowProperties;\r
+ }\r
+\r
+ private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes) {\r
+ JsonObject inputObject = new JsonObject();\r
+ for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {\r
+ String key = entry.getKey();\r
+ JsonObject inputProperty = inputs.getAsJsonObject(key);\r
+ String type = inputProperty.get("type").getAsString();\r
+ if (isComplexType(type, dataTypes)) {\r
+ inputObject.add(key, handleComplexType(type, dataTypes));\r
+ } else {\r
+ inputObject.addProperty(key, "");\r
+ }\r
+ }\r
+ return inputObject;\r
+ }\r
+\r
+ private JsonObject handleComplexType(String key, JsonObject dataTypes) {\r
+ JsonObject properties = dataTypes.get(key).getAsJsonObject().get("properties").getAsJsonObject();\r
+ return getInputProperties(properties, dataTypes);\r
+ }\r
+\r
+ private boolean isComplexType(String type, JsonObject dataTypes) {\r
+ return dataTypes.get(type) != null;\r
+ }\r
+\r
+ /**\r
+ * Creates payload to query CDS to get workflow input properties.\r
+ *\r
+ * @param blueprintName CDS blueprint name\r
+ * @param version CDS blueprint version\r
+ * @param workflow CDS blueprint workflow\r
+ * @return returns payload in json format\r
+ */\r
+ public String getCdsPayloadForWorkFlow(String blueprintName, String version, String workflow) {\r
+ JsonObject jsonObject = new JsonObject();\r
+ jsonObject.addProperty("blueprintName", blueprintName);\r
+ jsonObject.addProperty("version", version);\r
+ jsonObject.addProperty("returnContent", "json");\r
+ jsonObject.addProperty("workflowName", workflow);\r
+ jsonObject.addProperty("specType", "TOSCA");\r
+ return jsonObject.toString();\r
+ }\r
+}\r