2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation.
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=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
23 import static org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks.CnfmHttpServiceConfiguration.CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN;
24 import java.util.Optional;
25 import org.onap.so.cnfm.lcm.model.AsInstance;
26 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
27 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
28 import org.onap.so.rest.exceptions.InvalidRestRequestException;
29 import org.onap.so.rest.exceptions.RestProcessingException;
30 import org.onap.so.rest.service.HttpRestServiceProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Qualifier;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Service;
38 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
41 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
43 private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
44 private final CnfmUrlProvider cnfmUrlProvider;
45 private final HttpRestServiceProvider httpServiceProvider;
48 public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
49 @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
50 this.cnfmUrlProvider = cnfmUrlProvider;
51 this.httpServiceProvider = httpServiceProvider;
55 public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
57 final String url = cnfmUrlProvider.getCreateAsRequestUrl();
58 final ResponseEntity<AsInstance> response =
59 httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
61 final HttpStatus httpStatus = response.getStatusCode();
62 if (httpStatus.is2xxSuccessful()) {
63 if (!response.hasBody()) {
64 LOGGER.error("Received response without body: {}", response);
65 return Optional.empty();
68 final AsInstance asInstance = response.getBody();
69 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
70 LOGGER.error("Received invalid response missing asInstanceid: {}", response);
71 return Optional.empty();
73 return Optional.of(asInstance);
75 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
76 return Optional.empty();
78 } catch (final RestProcessingException | InvalidRestRequestException
79 | HttpResouceNotFoundException httpInvocationException) {
80 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
81 return Optional.empty();
86 public void invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
89 final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
90 final ResponseEntity<AsInstance> response =
91 httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
93 final HttpStatus httpStatus = response.getStatusCode();
94 if (httpStatus.is2xxSuccessful() && !(response.hasBody())) {
95 LOGGER.error("Response received without body: {}", response);
97 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
99 } catch (final RestProcessingException | InvalidRestRequestException
100 | HttpResouceNotFoundException httpInvocationException) {
101 LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);