2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterConfiguration.SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN;
24 import java.util.Optional;
25 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
27 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
28 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
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 Sol003AdapterServiceProviderImpl implements Sol003AdapterServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(Sol003AdapterServiceProviderImpl.class);
45 public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
47 private final Sol003AdapterUrlProvider urlProvider;
48 private final HttpRestServiceProvider httpServiceProvider;
51 public Sol003AdapterServiceProviderImpl(final Sol003AdapterUrlProvider urlProvider,
52 @Qualifier(SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
53 this.urlProvider = urlProvider;
54 this.httpServiceProvider = httpServiceProvider;
58 public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
59 final CreateVnfRequest request) {
61 final String url = urlProvider.getCreateInstantiateUrl(vnfId);
63 final ResponseEntity<CreateVnfResponse> response =
64 httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
66 final HttpStatus httpStatus = response.getStatusCode();
67 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
68 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
69 return Optional.empty();
72 if (!response.hasBody()) {
73 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
74 return Optional.empty();
77 final CreateVnfResponse createVnfResponse = response.getBody();
79 if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
80 LOGGER.error("Received invalid instantiation response: {}", response);
81 return Optional.empty();
84 return Optional.of(createVnfResponse);
85 } catch (final RestProcessingException | InvalidRestRequestException
86 | HttpResouceNotFoundException httpInvocationException) {
87 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
88 return Optional.empty();
94 public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
96 final String url = urlProvider.getJobStatusUrl(jobId);
98 final ResponseEntity<QueryJobResponse> response =
99 httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
101 final HttpStatus httpStatus = response.getStatusCode();
103 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
104 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
105 return Optional.empty();
108 if (!response.hasBody()) {
109 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
110 return Optional.empty();
112 return Optional.of(response.getBody());
113 } catch (final RestProcessingException | InvalidRestRequestException | HttpResouceNotFoundException exception) {
114 LOGGER.error("Unexpected error while processing job request", exception);
120 public Optional<DeleteVnfResponse> invokeTerminationRequest(final String vnfId) {
122 final String url = urlProvider.getTerminateVnfUrl(vnfId);
124 final ResponseEntity<DeleteVnfResponse> response =
125 httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
126 final HttpStatus httpStatus = response.getStatusCode();
127 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
128 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
129 return Optional.empty();
131 if (!response.hasBody()) {
132 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
133 return Optional.empty();
136 final DeleteVnfResponse deleteVnfResponse = response.getBody();
137 if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
138 LOGGER.error("Received invalid terminate response: {}", response);
139 return Optional.empty();
142 return Optional.of(deleteVnfResponse);
143 } catch (final RestProcessingException | InvalidRestRequestException
144 | HttpResouceNotFoundException httpInvocationException) {
145 LOGGER.error("Unexpected error while processing terminate request", httpInvocationException);
146 return Optional.empty();