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;
25 import java.util.Optional;
27 import org.onap.so.cnfm.lcm.model.AsInstance;
28 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
29 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
30 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
31 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
32 import org.onap.so.rest.exceptions.InvalidRestRequestException;
33 import org.onap.so.rest.exceptions.RestProcessingException;
34 import org.onap.so.rest.service.HttpRestServiceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Qualifier;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Service;
44 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
46 private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
47 private final CnfmUrlProvider cnfmUrlProvider;
48 private final HttpRestServiceProvider httpServiceProvider;
51 public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
52 @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
53 this.cnfmUrlProvider = cnfmUrlProvider;
54 this.httpServiceProvider = httpServiceProvider;
58 public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
60 final String url = cnfmUrlProvider.getCreateAsRequestUrl();
61 final ResponseEntity<AsInstance> response =
62 httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
64 final HttpStatus httpStatus = response.getStatusCode();
65 if (httpStatus.is2xxSuccessful()) {
66 if (!response.hasBody()) {
67 LOGGER.error("Received response without body: {}", response);
68 return Optional.empty();
71 final AsInstance asInstance = response.getBody();
72 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
73 LOGGER.error("Received invalid response missing asInstanceid: {}", response);
74 return Optional.empty();
76 return Optional.of(asInstance);
78 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
79 return Optional.empty();
81 } catch (final RestProcessingException | InvalidRestRequestException
82 | HttpResouceNotFoundException httpInvocationException) {
83 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
84 return Optional.empty();
89 public Optional<URI> invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
92 final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
93 final ResponseEntity<AsInstance> response =
94 httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
95 final HttpStatus httpStatus = response.getStatusCode();
96 if (httpStatus.is2xxSuccessful()) {
97 URI statusUri = response.getHeaders().getLocation();
98 if (statusUri == null) {
99 LOGGER.error("Received response without status URL for instance ID: {}", asInstanceId);
100 return Optional.empty();
102 return Optional.of(statusUri);
104 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
105 return Optional.empty();
107 } catch (final RestProcessingException | InvalidRestRequestException
108 | HttpResouceNotFoundException httpInvocationException) {
109 LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
110 return Optional.empty();
116 public Optional<AsLcmOpOcc> getInstantiateOperationJobStatus(final String url) {
118 final ResponseEntity<AsLcmOpOcc> response = httpServiceProvider.getHttpResponse(url, AsLcmOpOcc.class);
120 final HttpStatus httpStatus = response.getStatusCode();
122 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
123 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
124 return Optional.empty();
127 if (!response.hasBody()) {
128 LOGGER.error("CNFM status response recieved without body: {}", response);
129 return Optional.empty();
131 return Optional.of(response.getBody());
132 } catch (final RestProcessingException | InvalidRestRequestException
133 | HttpResouceNotFoundException httpInvocationException) {
134 LOGGER.error("Unexpected error while processing job request", httpInvocationException);
135 throw httpInvocationException;