aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-common/src/main/java/org/onap/so/apihandler/common/CamundaClient.java
blob: 33e474074046ef507a95b6e23111302f981ebf28 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (C) 2018 IBM.
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.so.apihandler.common;


import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.onap.so.apihandler.camundabeans.CamundaBooleanInput;
import org.onap.so.apihandler.camundabeans.CamundaInput;
import org.onap.so.apihandler.camundabeans.CamundaIntegerInput;
import org.onap.so.apihandler.camundabeans.CamundaRequest;
import org.onap.so.apihandler.camundabeans.CamundaResponse;
import org.onap.so.apihandler.camundabeans.CamundaVIDRequest;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
import org.onap.so.utils.CryptoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Component
public class CamundaClient {
    private static Logger logger = LoggerFactory.getLogger(CamundaClient.class);
    private static final String BASIC = "Basic ";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Environment env;

    @Autowired
    private ResponseHandler responseHandler;

    public ResponseEntity<String> post(String camundaReqXML, String requestId, String requestTimeout,
            String schemaVersion, String serviceInstanceId, String action, String orchestrationURI)
            throws ApiException {
        String jsonReq = wrapRequest(camundaReqXML, requestId, serviceInstanceId, requestTimeout, schemaVersion,
                orchestrationURI);
        logger.info("Camunda Request Content: {}", jsonReq);

        return post(jsonReq, orchestrationURI);
    }

    public ResponseEntity<String> post(RequestClientParameter parameterObject, String orchestrationURI)
            throws ApiException {
        String jsonReq = wrapVIDRequest(parameterObject.getRequestId(), parameterObject.isBaseVfModule(),
                parameterObject.getRecipeTimeout(), parameterObject.getRequestAction(),
                parameterObject.getServiceInstanceId(), parameterObject.getPnfCorrelationId(),
                parameterObject.getVnfId(), parameterObject.getVfModuleId(), parameterObject.getVolumeGroupId(),
                parameterObject.getNetworkId(), parameterObject.getConfigurationId(), parameterObject.getServiceType(),
                parameterObject.getVnfType(), parameterObject.getVfModuleType(), parameterObject.getNetworkType(),
                parameterObject.getRequestDetails(), parameterObject.getApiVersion(), parameterObject.isaLaCarte(),
                parameterObject.getRequestUri(), parameterObject.getRecipeParamXsd(),
                parameterObject.getInstanceGroupId(), parameterObject.isGenerateIdsOnly());

        return post(jsonReq, orchestrationURI);
    }

    public ResponseEntity<String> get(String url) throws ApiException {
        url = env.getRequiredProperty(CommonConstants.CAMUNDA_URL) + url;
        HttpEntity<?> requestEntity = new HttpEntity<>(setHeaders());
        try {
            return restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        } catch (HttpStatusCodeException e) {
            logger.error("Error returned from sending GET request to BPMN", e);
            throw createBPMNFailureException(e);
        } catch (ResourceAccessException e) {
            logger.error("Error sending GET to BPMN", e);
            ClientConnectionException clientException = new ClientConnectionException.Builder(url,
                    HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).build();
            throw clientException;
        }
    }

    public ResponseEntity<String> post(String jsonReq, String url) throws ApiException {
        url = env.getRequiredProperty(CommonConstants.CAMUNDA_URL) + url;
        HttpEntity<String> request = new HttpEntity<String>(jsonReq, setHeaders());
        try {
            return restTemplate.postForEntity(url, request, String.class);
        } catch (HttpStatusCodeException e) {
            logger.error("Error returned after sending POST request to BPMN", e);
            throw createBPMNFailureException(e);
        } catch (ResourceAccessException e) {
            logger.error("Error sending POST to BPMN", e);
            ClientConnectionException clientException = new ClientConnectionException.Builder(url,
                    HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).build();
            throw clientException;
        }
    }

    protected String wrapRequest(String reqXML, String requestId, String serviceInstanceId, String requestTimeout,
            String schemaVersion, String url) {
        String jsonReq = null;
        try {
            CamundaRequest camundaRequest = new CamundaRequest();
            CamundaInput camundaInput = new CamundaInput();
            CamundaInput host = new CamundaInput();
            CamundaInput schema = new CamundaInput();
            CamundaInput reqid = new CamundaInput();
            CamundaInput svcid = new CamundaInput();
            CamundaInput timeout = new CamundaInput();
            camundaInput.setValue(StringUtils.defaultString(reqXML));
            host.setValue(CommonConstants.CAMUNDA_URL);
            schema.setValue(StringUtils.defaultString(schemaVersion));
            reqid.setValue(requestId);
            svcid.setValue(serviceInstanceId);
            timeout.setValue(StringUtils.defaultString(requestTimeout));
            camundaRequest.setServiceInput(camundaInput);
            camundaRequest.setHost(host);
            camundaRequest.setReqid(reqid);
            camundaRequest.setSvcid(svcid);
            camundaRequest.setSchema(schema);
            camundaRequest.setTimeout(timeout);
            ObjectMapper mapper = new ObjectMapper();

            mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

            jsonReq = mapper.writeValueAsString(camundaRequest);
            logger.trace("request body is {}", jsonReq);
        } catch (Exception e) {
            logger.error("Error in APIH Wrap request", e);
        }
        return jsonReq;
    }


    protected String wrapVIDRequest(String requestId, boolean isBaseVfModule, int recipeTimeout, String requestAction,
            String serviceInstanceId, String pnfCorrelationId, String vnfId, String vfModuleId, String volumeGroupId,
            String networkId, String configurationId, String serviceType, String vnfType, String vfModuleType,
            String networkType, String requestDetails, String apiVersion, boolean aLaCarte, String requestUri,
            String paramXsd, String instanceGroupId, boolean generateIdsOnly) {
        String jsonReq = null;

        try {
            CamundaVIDRequest camundaRequest = new CamundaVIDRequest();
            CamundaInput serviceInput = new CamundaInput();
            CamundaInput host = new CamundaInput();
            CamundaInput requestIdInput = new CamundaInput();
            CamundaBooleanInput isBaseVfModuleInput = new CamundaBooleanInput();
            CamundaIntegerInput recipeTimeoutInput = new CamundaIntegerInput();
            CamundaInput requestActionInput = new CamundaInput();
            CamundaInput serviceInstanceIdInput = new CamundaInput();
            CamundaInput pnfCorrelationIdInput = new CamundaInput();
            CamundaInput vnfIdInput = new CamundaInput();
            CamundaInput vfModuleIdInput = new CamundaInput();
            CamundaInput volumeGroupIdInput = new CamundaInput();
            CamundaInput networkIdInput = new CamundaInput();
            CamundaInput configurationIdInput = new CamundaInput();
            CamundaInput serviceTypeInput = new CamundaInput();
            CamundaInput vnfTypeInput = new CamundaInput();
            CamundaInput vfModuleTypeInput = new CamundaInput();
            CamundaInput networkTypeInput = new CamundaInput();
            CamundaBooleanInput aLaCarteInput = new CamundaBooleanInput();
            CamundaInput apiVersionInput = new CamundaInput();
            CamundaInput requestUriInput = new CamundaInput();
            CamundaInput recipeParamsInput = new CamundaInput();
            CamundaInput instanceGroupIdInput = new CamundaInput();
            CamundaBooleanInput generateIds = new CamundaBooleanInput();


            requestIdInput.setValue(StringUtils.defaultString(requestId));
            isBaseVfModuleInput.setValue(isBaseVfModule);
            recipeTimeoutInput.setValue(recipeTimeout);
            requestActionInput.setValue(StringUtils.defaultString(requestAction));
            serviceInstanceIdInput.setValue(StringUtils.defaultString(serviceInstanceId));
            pnfCorrelationIdInput.setValue(StringUtils.defaultString(pnfCorrelationId));
            vnfIdInput.setValue(StringUtils.defaultString(vnfId));
            vfModuleIdInput.setValue(StringUtils.defaultString(vfModuleId));
            volumeGroupIdInput.setValue(StringUtils.defaultString(volumeGroupId));
            networkIdInput.setValue(StringUtils.defaultString(networkId));
            configurationIdInput.setValue(StringUtils.defaultString(configurationId));
            serviceTypeInput.setValue(StringUtils.defaultString(serviceType));
            vnfTypeInput.setValue(StringUtils.defaultString(vnfType));
            vfModuleTypeInput.setValue(StringUtils.defaultString(vfModuleType));
            networkTypeInput.setValue(StringUtils.defaultString(networkType));
            aLaCarteInput.setValue(aLaCarte);
            apiVersionInput.setValue(StringUtils.defaultString(apiVersion));
            requestUriInput.setValue(StringUtils.defaultString(requestUri));
            recipeParamsInput.setValue(paramXsd);
            instanceGroupIdInput.setValue(StringUtils.defaultString(instanceGroupId));
            generateIds.setValue(generateIdsOnly);

            serviceInput.setValue(requestDetails);
            camundaRequest.setServiceInput(serviceInput);
            camundaRequest.setHost(host);
            camundaRequest.setRequestId(requestIdInput);
            camundaRequest.setMsoRequestId(requestIdInput);
            camundaRequest.setIsBaseVfModule(isBaseVfModuleInput);
            camundaRequest.setRecipeTimeout(recipeTimeoutInput);
            camundaRequest.setRequestAction(requestActionInput);
            camundaRequest.setServiceInstanceId(serviceInstanceIdInput);
            camundaRequest.setPnfCorrelationId(pnfCorrelationIdInput);
            camundaRequest.setVnfId(vnfIdInput);
            camundaRequest.setVfModuleId(vfModuleIdInput);
            camundaRequest.setVolumeGroupId(volumeGroupIdInput);
            camundaRequest.setNetworkId(networkIdInput);
            camundaRequest.setConfigurationId(configurationIdInput);
            camundaRequest.setServiceType(serviceTypeInput);
            camundaRequest.setVnfType(vnfTypeInput);
            camundaRequest.setVfModuleType(vfModuleTypeInput);
            camundaRequest.setNetworkType(networkTypeInput);
            camundaRequest.setaLaCarte(aLaCarteInput);
            camundaRequest.setApiVersion(apiVersionInput);
            camundaRequest.setRequestUri(requestUriInput);
            camundaRequest.setRecipeParams(recipeParamsInput);
            camundaRequest.setInstanceGroupId(instanceGroupIdInput);
            camundaRequest.setGenerateIds(generateIds);

            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

            jsonReq = mapper.writeValueAsString(camundaRequest);
            logger.trace("request body is {}", jsonReq);
        } catch (Exception e) {
            logger.error("Error in wrapVIDRequest", e);
        }
        return jsonReq;
    }

    protected HttpHeaders setHeaders() {
        HttpHeaders headers = new HttpHeaders();
        List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>();
        acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON);
        headers.setAccept(acceptableMediaTypes);
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
        headers.add(HttpHeaders.AUTHORIZATION, addAuthorizationHeader(env.getRequiredProperty("mso.camundaAuth"),
                env.getRequiredProperty("mso.msoKey")));
        return headers;
    }

    protected String addAuthorizationHeader(String auth, String msoKey) {
        String basicAuth = null;
        try {
            String userCredentials = CryptoUtils.decrypt(auth, msoKey);
            if (userCredentials != null) {
                basicAuth = BASIC + DatatypeConverter.printBase64Binary(userCredentials.getBytes());
            }
        } catch (GeneralSecurityException e) {
            logger.error("Security exception", e);
        }
        return basicAuth;
    }

    protected BPMNFailureException createBPMNFailureException(HttpStatusCodeException e) {
        ObjectMapper mapper = new ObjectMapper();
        String responseText = null;
        String message = null;
        try {
            CamundaResponse response = mapper.readValue(e.getResponseBodyAsString(), CamundaResponse.class);
            responseText = response.getResponse();
        } catch (IOException ex) {
            responseText = e.getResponseBodyAsString();
        }
        message = String.valueOf(e.getStatusCode());
        if (responseText != null && !responseText.isEmpty()) {
            message = message + " " + responseText;
        }
        BPMNFailureException bpmnException =
                new BPMNFailureException.Builder(message, responseHandler.setStatus(e.getStatusCode().value()),
                        ErrorNumbers.SVC_DETAILED_SERVICE_ERROR, e.getStatusCode()).build();
        return bpmnException;
    }

}