2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
23 package org.onap.clamp.clds.client;
\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
31 import java.util.Date;
\r
32 import java.util.Map;
\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
44 * This class implements the communication with CDS for the service inventory.
\r
47 public class CdsServices {
\r
50 CamelContext camelContext;
\r
52 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);
\r
58 public CdsServices() {
\r
63 * Query CDS to get blueprint's workflow list.
\r
65 * @param blueprintName CDS blueprint name
\r
66 * @param blueprintVersion CDS blueprint version
\r
67 * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list
\r
69 public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {
\r
70 LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");
\r
72 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
\r
73 .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)
\r
76 Exchange exchangeResponse = camelContext.createProducerTemplate()
\r
77 .send("direct:get-blueprint-workflow-list", myCamelExchange);
\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
91 * Query CDS to get input properties of workflow.
\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
98 public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,
\r
100 LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");
\r
102 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
\r
103 .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))
\r
106 Exchange exchangeResponse = camelContext.createProducerTemplate()
\r
107 .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);
\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
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
125 JsonObject workFlowProperties = new JsonObject();
\r
126 workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes));
\r
127 return workFlowProperties;
\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
139 inputObject.addProperty(key, "");
\r
142 return inputObject;
\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
150 private boolean isComplexType(String type, JsonObject dataTypes) {
\r
151 return dataTypes.get(type) != null;
\r
155 * Creates payload to query CDS to get workflow input properties.
\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
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