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.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
45 * This class implements the communication with CDS for the service inventory.
\r
48 public class CdsServices {
\r
51 CamelContext camelContext;
\r
53 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);
\r
59 public CdsServices() {
\r
64 * Query CDS to get blueprint's workflow list.
\r
66 * @param blueprintName CDS blueprint name
\r
67 * @param blueprintVersion CDS blueprint version
\r
68 * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list
\r
70 public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {
\r
71 LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");
\r
73 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
\r
74 .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)
\r
77 Exchange exchangeResponse = camelContext.createProducerTemplate()
\r
78 .send("direct:get-blueprint-workflow-list", myCamelExchange);
\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
88 logger.error("CDS getBlueprintWorkflowList FAILED");
\r
95 * Query CDS to get input properties of workflow.
\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
102 public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,
\r
104 LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");
\r
106 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
\r
107 .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))
\r
110 Exchange exchangeResponse = camelContext.createProducerTemplate()
\r
111 .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);
\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
121 logger.error("CDS getWorkflowInputProperties FAILED");
\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
131 JsonObject workFlowProperties = new JsonObject();
\r
132 workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes));
\r
133 return workFlowProperties;
\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
148 inputObject.add(key, entry.getValue());
\r
151 return inputObject;
\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
166 return inputProperty;
\r
169 throw new CdsParametersException("Entry schema is null for " + propertyName);
\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
177 private boolean isComplexType(String type, JsonObject dataTypes) {
\r
178 return dataTypes.get(type) != null;
\r
182 * Creates payload to query CDS to get workflow input properties.
\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
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