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 static java.lang.Boolean.parseBoolean;
\r
27 import com.att.eelf.configuration.EELFLogger;
\r
28 import com.att.eelf.configuration.EELFManager;
\r
29 import com.google.gson.JsonElement;
\r
30 import com.google.gson.JsonObject;
\r
31 import com.google.gson.JsonParser;
\r
33 import java.util.Date;
\r
34 import java.util.Map;
\r
36 import org.apache.camel.CamelContext;
\r
37 import org.apache.camel.Exchange;
\r
38 import org.apache.camel.builder.ExchangeBuilder;
\r
39 import org.onap.clamp.clds.exception.cds.CdsParametersException;
\r
40 import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;
\r
41 import org.onap.clamp.clds.util.JsonUtils;
\r
42 import org.onap.clamp.clds.util.LoggingUtils;
\r
43 import org.springframework.beans.factory.annotation.Autowired;
\r
44 import org.springframework.stereotype.Component;
\r
47 * This class implements the communication with CDS for the service inventory.
\r
50 public class CdsServices {
\r
53 CamelContext camelContext;
\r
55 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);
\r
57 private static final String TYPE = "type";
\r
58 private static final String PROPERTIES = "properties";
\r
59 private static final String LIST = "list";
\r
65 public CdsServices() {
\r
70 * Query CDS to get blueprint's workflow list.
\r
72 * @param blueprintName CDS blueprint name
\r
73 * @param blueprintVersion CDS blueprint version
\r
74 * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list
\r
76 public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {
\r
77 LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");
\r
79 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
\r
80 .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)
\r
83 Exchange exchangeResponse = camelContext.createProducerTemplate()
\r
84 .send("direct:get-blueprint-workflow-list", myCamelExchange);
\r
86 if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
\r
87 String cdsResponse = (String) exchangeResponse.getIn().getBody();
\r
88 logger.info("getBlueprintWorkflowList, response from CDS:" + cdsResponse);
\r
89 LoggingUtils.setResponseContext("0", "Get Blueprint workflow list", this.getClass().getName());
\r
90 Date startTime = new Date();
\r
91 LoggingUtils.setTimeContext(startTime, new Date());
\r
92 return JsonUtils.GSON_JPA_MODEL.fromJson(cdsResponse, CdsBpWorkFlowListResponse.class);
\r
94 logger.error("CDS getBlueprintWorkflowList FAILED");
\r
101 * Query CDS to get input properties of workflow.
\r
103 * @param blueprintName CDS blueprint name
\r
104 * @param blueprintVersion CDS blueprint name
\r
105 * @param workflow CDS blueprint's workflow
\r
106 * @return input properties in json format
\r
108 public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,
\r
110 LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");
\r
112 Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
\r
113 .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))
\r
116 Exchange exchangeResponse = camelContext.createProducerTemplate()
\r
117 .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);
\r
119 if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
\r
120 String cdsResponse = (String) exchangeResponse.getIn().getBody();
\r
121 logger.info("getWorkflowInputProperties, response from CDS:" + cdsResponse);
\r
122 LoggingUtils.setResponseContext("0", "Get Blueprint workflow input properties", this.getClass().getName());
\r
123 Date startTime = new Date();
\r
124 LoggingUtils.setTimeContext(startTime, new Date());
\r
125 return parseCdsResponse(cdsResponse);
\r
127 logger.error("CDS getWorkflowInputProperties FAILED");
\r
132 protected JsonObject parseCdsResponse(String response) {
\r
133 JsonObject root = JsonParser.parseString(response).getAsJsonObject();
\r
134 JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");
\r
135 JsonObject dataTypes = root.getAsJsonObject("dataTypes");
\r
137 JsonObject workFlowProperties = new JsonObject();
\r
138 workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes, new JsonObject()));
\r
139 return workFlowProperties;
\r
142 private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes,
\r
143 JsonObject inputObject) {
\r
144 if (inputs == null) {
\r
145 return inputObject;
\r
148 for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {
\r
149 String key = entry.getKey();
\r
150 JsonObject inputProperty = inputs.getAsJsonObject(key);
\r
151 String type = inputProperty.get(TYPE).getAsString();
\r
152 if (isComplexType(type, dataTypes)) {
\r
153 inputObject.add(key, handleComplexType(type, dataTypes));
\r
154 } else if (LIST.equalsIgnoreCase(type)) {
\r
155 handleListType(key, inputProperty, dataTypes, inputObject);
\r
156 } else if (isInputParam(inputProperty)) {
\r
157 inputObject.add(key, entry.getValue());
\r
160 return inputObject;
\r
163 private void handleListType(String propertyName,
\r
164 JsonObject inputProperty,
\r
165 JsonObject dataTypes,
\r
166 JsonObject inputObject) {
\r
167 if (inputProperty.get("entry_schema") == null) {
\r
168 throw new CdsParametersException("Entry schema is null for " + propertyName);
\r
171 String type = inputProperty.get("entry_schema").getAsJsonObject().get(
\r
172 TYPE).getAsString();
\r
173 if (dataTypes.get(type) != null) {
\r
174 JsonObject jsonObject = new JsonObject();
\r
175 jsonObject.addProperty(TYPE, LIST);
\r
176 jsonObject.add(PROPERTIES, getPropertiesObject(type, dataTypes));
\r
177 inputObject.add(propertyName, jsonObject);
\r
178 } else if (isInputParam(inputProperty)) {
\r
179 inputObject.add(propertyName, inputProperty);
\r
183 private JsonObject handleComplexType(String key, JsonObject dataTypes) {
\r
184 JsonObject jsonObject = new JsonObject();
\r
185 jsonObject.addProperty(TYPE, "object");
\r
186 jsonObject.add(PROPERTIES, getPropertiesObject(key, dataTypes));
\r
190 private JsonObject getPropertiesObject(String key, JsonObject dataTypes) {
\r
191 JsonObject properties = dataTypes.get(key).getAsJsonObject().get(PROPERTIES).getAsJsonObject();
\r
192 JsonObject object = new JsonObject();
\r
193 getInputProperties(properties, dataTypes, object);
\r
197 private boolean isComplexType(String type, JsonObject dataTypes) {
\r
198 if (dataTypes == null) {
\r
201 return dataTypes.get(type) != null;
\r
204 private boolean isInputParam(JsonObject inputProperty) {
\r
205 JsonElement inputParam = inputProperty.get("input-param");
\r
206 if (inputParam == null) {
\r
209 return parseBoolean(inputParam.getAsString());
\r
213 * Creates payload to query CDS to get workflow input properties.
\r
215 * @param blueprintName CDS blueprint name
\r
216 * @param version CDS blueprint version
\r
217 * @param workflow CDS blueprint workflow
\r
218 * @return returns payload in json format
\r
220 public String getCdsPayloadForWorkFlow(String blueprintName, String version, String workflow) {
\r
221 JsonObject jsonObject = new JsonObject();
\r
222 jsonObject.addProperty("blueprintName", blueprintName);
\r
223 jsonObject.addProperty("version", version);
\r
224 jsonObject.addProperty("returnContent", "json");
\r
225 jsonObject.addProperty("workflowName", workflow);
\r
226 jsonObject.addProperty("specType", "TOSCA");
\r
227 return jsonObject.toString();
\r