aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/policy/clamp/clds/client/CdsServices.java
blob: ff79ef41392c0a8c34652cdde4513ed6ea9982e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 Huawei Technologies Co., Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 * ================================================================================
 *
 */

package org.onap.policy.clamp.clds.client;

import static java.lang.Boolean.parseBoolean;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.Date;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.ExchangeBuilder;
import org.onap.policy.clamp.clds.exception.cds.CdsParametersException;
import org.onap.policy.clamp.clds.model.cds.CdsBpWorkFlowListResponse;
import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.clamp.clds.util.LoggingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * This class implements the communication with CDS for the service inventory.
 */
@Component
public class CdsServices {

    @Autowired
    CamelContext camelContext;

    protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsServices.class);

    private static final String TYPE = "type";
    private static final String PROPERTIES = "properties";
    private static final String LIST = "list";

    /**
     * Constructor.
     */
    @Autowired
    public CdsServices() {
    }


    /**
     * Query CDS to get blueprint's workflow list.
     *
     * @param blueprintName    CDS blueprint name
     * @param blueprintVersion CDS blueprint version
     * @return CdsBpWorkFlowListResponse CDS blueprint's workflow list
     */
    public CdsBpWorkFlowListResponse getBlueprintWorkflowList(String blueprintName, String blueprintVersion) {
        LoggingUtils.setTargetContext("CDS", "getBlueprintWorkflowList");

        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
                .withProperty("blueprintName", blueprintName).withProperty("blueprintVersion", blueprintVersion)
                .build();

        Exchange exchangeResponse = camelContext.createProducerTemplate()
                .send("direct:get-blueprint-workflow-list", myCamelExchange);

        if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
            String cdsResponse = (String) exchangeResponse.getIn().getBody();
            logger.info("getBlueprintWorkflowList, response from CDS:" + cdsResponse);
            LoggingUtils.setResponseContext("0", "Get Blueprint workflow list", this.getClass().getName());
            Date startTime = new Date();
            LoggingUtils.setTimeContext(startTime, new Date());
            return JsonUtils.GSON_JPA_MODEL.fromJson(cdsResponse, CdsBpWorkFlowListResponse.class);
        } else {
            logger.error("CDS getBlueprintWorkflowList FAILED");
            return null;
        }

    }

    /**
     * Query CDS to get input properties of workflow.
     *
     * @param blueprintName    CDS blueprint name
     * @param blueprintVersion CDS blueprint name
     * @param workflow         CDS blueprint's workflow
     * @return input properties in json format
     */
    public JsonObject getWorkflowInputProperties(String blueprintName, String blueprintVersion,
                                                 String workflow) {
        LoggingUtils.setTargetContext("CDS", "getWorkflowInputProperties");

        Exchange myCamelExchange = ExchangeBuilder.anExchange(camelContext)
                .withBody(getCdsPayloadForWorkFlow(blueprintName, blueprintVersion, workflow))
                .build();

        Exchange exchangeResponse = camelContext.createProducerTemplate()
                .send("direct:get-blueprint-workflow-input-properties", myCamelExchange);

        if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) {
            String cdsResponse = (String) exchangeResponse.getIn().getBody();
            logger.info("getWorkflowInputProperties, response from CDS:" + cdsResponse);
            LoggingUtils.setResponseContext("0", "Get Blueprint workflow input properties", this.getClass().getName());
            Date startTime = new Date();
            LoggingUtils.setTimeContext(startTime, new Date());
            return parseCdsResponse(cdsResponse);
        } else {
            logger.error("CDS getWorkflowInputProperties FAILED");
            return null;
        }
    }

    protected JsonObject parseCdsResponse(String response) {
        JsonObject root = JsonParser.parseString(response).getAsJsonObject();
        JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");
        JsonObject dataTypes = root.getAsJsonObject("dataTypes");

        JsonObject workFlowProperties = new JsonObject();
        workFlowProperties.add("inputs", getInputProperties(inputs, dataTypes, new JsonObject()));
        return workFlowProperties;
    }

    private JsonObject getInputProperties(JsonObject inputs, JsonObject dataTypes,
                                          JsonObject inputObject) {
        if (inputs == null) {
            return inputObject;
        }

        for (Map.Entry<String, JsonElement> entry : inputs.entrySet()) {
            String key = entry.getKey();
            JsonObject inputProperty = inputs.getAsJsonObject(key);
            String type = inputProperty.get(TYPE).getAsString();
            if (isComplexType(type, dataTypes)) {
                inputObject.add(key, handleComplexType(type, dataTypes));
            } else if (LIST.equalsIgnoreCase(type)) {
                handleListType(key, inputProperty, dataTypes, inputObject);
            } else if (isInputParam(inputProperty)) {
                inputObject.add(key, entry.getValue());
            }
        }
        return inputObject;
    }

    private void handleListType(String propertyName,
                                      JsonObject inputProperty,
                                      JsonObject dataTypes,
                                      JsonObject inputObject) {
        if (inputProperty.get("entry_schema") == null) {
            throw new CdsParametersException("Entry schema is null for " + propertyName);
        }

        String type = inputProperty.get("entry_schema").getAsJsonObject().get(
                TYPE).getAsString();
        if (dataTypes.get(type) != null) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty(TYPE, LIST);
            jsonObject.add(PROPERTIES, getPropertiesObject(type, dataTypes));
            inputObject.add(propertyName, jsonObject);
        } else if (isInputParam(inputProperty)) {
            inputObject.add(propertyName, inputProperty);
        }
    }

    private JsonObject handleComplexType(String key, JsonObject dataTypes) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty(TYPE, "object");
        jsonObject.add(PROPERTIES, getPropertiesObject(key, dataTypes));
        return jsonObject;
    }

    private JsonObject getPropertiesObject(String key, JsonObject dataTypes) {
        JsonObject properties = dataTypes.get(key).getAsJsonObject().get(PROPERTIES).getAsJsonObject();
        JsonObject object = new JsonObject();
        getInputProperties(properties, dataTypes, object);
        return object;
    }

    private boolean isComplexType(String type, JsonObject dataTypes) {
        if (dataTypes == null) {
            return false;
        }
        return dataTypes.get(type) != null;
    }

    private boolean isInputParam(JsonObject inputProperty) {
        JsonElement inputParam = inputProperty.get("input-param");
        if (inputParam == null) {
            return false;
        }
        return parseBoolean(inputParam.getAsString());
    }

    /**
     * Creates payload to query CDS to get workflow input properties.
     *
     * @param blueprintName CDS blueprint name
     * @param version       CDS blueprint version
     * @param workflow      CDS blueprint workflow
     * @return returns payload in json format
     */
    public String getCdsPayloadForWorkFlow(String blueprintName, String version, String workflow) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("blueprintName", blueprintName);
        jsonObject.addProperty("version", version);
        jsonObject.addProperty("returnContent", "json");
        jsonObject.addProperty("workflowName", workflow);
        jsonObject.addProperty("specType", "TOSCA");
        return jsonObject.toString();
    }
}