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=========================================================
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;
24 import java.util.Optional;
25 import org.onap.so.cnfm.lcm.model.AsInstance;
26 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
27 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
28 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
29 import org.onap.so.cnfm.lcm.model.TerminateAsRequest;
30 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
31 import org.onap.so.rest.exceptions.InvalidRestRequestException;
32 import org.onap.so.rest.exceptions.RestProcessingException;
33 import org.onap.so.rest.service.HttpRestServiceProvider;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Service;
43 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
45 private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
46 private final CnfmUrlProvider cnfmUrlProvider;
47 private final HttpRestServiceProvider httpServiceProvider;
50 public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
51 @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
52 this.cnfmUrlProvider = cnfmUrlProvider;
53 this.httpServiceProvider = httpServiceProvider;
57 public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
59 final String url = cnfmUrlProvider.getCreateAsRequestUrl();
60 final ResponseEntity<AsInstance> response =
61 httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
63 final HttpStatus httpStatus = response.getStatusCode();
64 if (httpStatus.is2xxSuccessful()) {
65 if (!response.hasBody()) {
66 LOGGER.error("Received response without body: {}", response);
67 return Optional.empty();
70 final AsInstance asInstance = response.getBody();
71 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
72 LOGGER.error("Received invalid response missing asInstanceid: {}", response);
73 return Optional.empty();
75 return Optional.of(asInstance);
77 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
78 return Optional.empty();
80 } catch (final RestProcessingException | InvalidRestRequestException
81 | HttpResouceNotFoundException httpInvocationException) {
82 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
83 return Optional.empty();
88 public Optional<URI> invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
91 final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
92 final ResponseEntity<AsInstance> response =
93 httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
94 final HttpStatus httpStatus = response.getStatusCode();
95 if (httpStatus.is2xxSuccessful()) {
96 URI statusUri = response.getHeaders().getLocation();
97 if (statusUri == null) {
98 LOGGER.error("Received response without status URL for instance ID: {}", asInstanceId);
99 return Optional.empty();
101 return Optional.of(statusUri);
103 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
104 return Optional.empty();
106 } catch (final RestProcessingException | InvalidRestRequestException
107 | HttpResouceNotFoundException httpInvocationException) {
108 LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
109 return Optional.empty();
115 public Optional<AsLcmOpOcc> getOperationJobStatus(final String url) {
117 final ResponseEntity<AsLcmOpOcc> response = httpServiceProvider.getHttpResponse(url, AsLcmOpOcc.class);
119 final HttpStatus httpStatus = response.getStatusCode();
121 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
122 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: {}", url, httpStatus.value());
123 return Optional.empty();
126 if (!response.hasBody()) {
127 LOGGER.error("CNFM status response recieved without body: {}", response);
128 return Optional.empty();
130 return Optional.of(response.getBody());
131 } catch (final RestProcessingException | InvalidRestRequestException
132 | HttpResouceNotFoundException httpInvocationException) {
133 LOGGER.error("Unexpected error while processing job request", httpInvocationException);
134 throw httpInvocationException;
139 public Optional<Boolean> invokeDeleteAsRequest(String asInstanceId) {
142 final String url = cnfmUrlProvider.getDeleteAsRequestUrl(asInstanceId);
143 LOGGER.debug("Will send request to CNFM by uisng the url: {}", url);
145 ResponseEntity<Void> response = httpServiceProvider.deleteHttpRequest(url, Void.class);
146 final HttpStatus httpStatus = response.getStatusCode();
147 if (!(httpStatus.is2xxSuccessful())) {
148 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
149 return Optional.empty();
151 return Optional.of(Boolean.TRUE);
152 } catch (final RestProcessingException | InvalidRestRequestException
153 | HttpResouceNotFoundException httpInvocationException) {
154 LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
155 return Optional.empty();
160 public Optional<URI> invokeTerminateAsRequest(String asInstanceId, TerminateAsRequest terminateAsRequest) {
163 final String url = cnfmUrlProvider.getTerminateAsRequestUrl(asInstanceId);
164 LOGGER.debug("Will send request to CNFM to terminate by uisng the url: {}", url);
166 ResponseEntity<Void> response = httpServiceProvider.postHttpRequest(terminateAsRequest, url, Void.class);
167 final HttpStatus httpStatus = response.getStatusCode();
168 if (httpStatus.is2xxSuccessful()) {
169 URI statusUri = response.getHeaders().getLocation();
170 if (statusUri == null) {
171 LOGGER.error("Received response without status URL while terminating of instance with ID: {}",
173 return Optional.empty();
175 return Optional.of(statusUri);
177 LOGGER.error("Unable to invoke HTTP DELETE while terminating by using URL: {}, Response Code: {}", url,
179 return Optional.empty();
181 } catch (final RestProcessingException | InvalidRestRequestException
182 | HttpResouceNotFoundException httpInvocationException) {
183 LOGGER.error("Unexpected error while processing terminate request", httpInvocationException);
184 return Optional.empty();