2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
22 import static org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks.CnfmHttpServiceConfiguration.CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN;
23 import java.util.Optional;
24 import org.onap.so.cnfm.lcm.model.AsInstance;
25 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
26 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
27 import org.onap.so.rest.exceptions.InvalidRestRequestException;
28 import org.onap.so.rest.exceptions.RestProcessingException;
29 import org.onap.so.rest.service.HttpRestServiceProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
40 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
42 private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
43 private final CnfmUrlProvider cnfmUrlProvider;
44 private final HttpRestServiceProvider httpServiceProvider;
47 public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
48 @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
49 this.cnfmUrlProvider = cnfmUrlProvider;
50 this.httpServiceProvider = httpServiceProvider;
54 public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
56 final String url = cnfmUrlProvider.getCreateAsRequestUrl();
57 final ResponseEntity<AsInstance> response =
58 httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
60 final HttpStatus httpStatus = response.getStatusCode();
61 if (httpStatus.is2xxSuccessful()) {
62 if (!response.hasBody()) {
63 LOGGER.error("Received response without body: {}", response);
64 return Optional.empty();
67 final AsInstance asInstance = response.getBody();
68 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
69 LOGGER.error("Received invalid response missing asInstanceid: {}", response);
70 return Optional.empty();
72 return Optional.of(asInstance);
74 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
75 return Optional.empty();
77 } catch (final RestProcessingException | InvalidRestRequestException
78 | HttpResouceNotFoundException httpInvocationException) {
79 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
80 return Optional.empty();
85 public void invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
88 final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
89 final ResponseEntity<AsInstance> response =
90 httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
92 final HttpStatus httpStatus = response.getStatusCode();
93 if (httpStatus.is2xxSuccessful()) {
94 if (!response.hasBody()) {
95 LOGGER.error("Received response without body: {}", response);
99 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
101 } catch (final RestProcessingException | InvalidRestRequestException
102 | HttpResouceNotFoundException httpInvocationException) {
103 LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);