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.rest.exceptions.HttpResouceNotFoundException;
30 import org.onap.so.rest.exceptions.InvalidRestRequestException;
31 import org.onap.so.rest.exceptions.RestProcessingException;
32 import org.onap.so.rest.service.HttpRestServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Service;
42 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
45 private final CnfmUrlProvider cnfmUrlProvider;
46 private final HttpRestServiceProvider httpServiceProvider;
49 public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
50 @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
51 this.cnfmUrlProvider = cnfmUrlProvider;
52 this.httpServiceProvider = httpServiceProvider;
56 public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
58 final String url = cnfmUrlProvider.getCreateAsRequestUrl();
59 final ResponseEntity<AsInstance> response =
60 httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
62 final HttpStatus httpStatus = response.getStatusCode();
63 if (httpStatus.is2xxSuccessful()) {
64 if (!response.hasBody()) {
65 LOGGER.error("Received response without body: {}", response);
66 return Optional.empty();
69 final AsInstance asInstance = response.getBody();
70 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
71 LOGGER.error("Received invalid response missing asInstanceid: {}", response);
72 return Optional.empty();
74 return Optional.of(asInstance);
76 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
77 return Optional.empty();
79 } catch (final RestProcessingException | InvalidRestRequestException
80 | HttpResouceNotFoundException httpInvocationException) {
81 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
82 return Optional.empty();
87 public Optional<URI> invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
90 final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
91 final ResponseEntity<AsInstance> response =
92 httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
93 final HttpStatus httpStatus = response.getStatusCode();
94 if (httpStatus.is2xxSuccessful()) {
95 URI statusUri = response.getHeaders().getLocation();
96 if (statusUri == null) {
97 LOGGER.error("Received response without status URL for instance ID: {}", asInstanceId);
98 return Optional.empty();
100 return Optional.of(statusUri);
102 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
103 return Optional.empty();
105 } catch (final RestProcessingException | InvalidRestRequestException
106 | HttpResouceNotFoundException httpInvocationException) {
107 LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
108 return Optional.empty();
114 public Optional<AsLcmOpOcc> getInstantiateOperationJobStatus(final String url) {
116 final ResponseEntity<AsLcmOpOcc> response = httpServiceProvider.getHttpResponse(url, AsLcmOpOcc.class);
118 final HttpStatus httpStatus = response.getStatusCode();
120 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
121 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
122 return Optional.empty();
125 if (!response.hasBody()) {
126 LOGGER.error("CNFM status response recieved without body: {}", response);
127 return Optional.empty();
129 return Optional.of(response.getBody());
130 } catch (final RestProcessingException | InvalidRestRequestException
131 | HttpResouceNotFoundException httpInvocationException) {
132 LOGGER.error("Unexpected error while processing job request", httpInvocationException);
133 throw httpInvocationException;