2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.vnfm.tasks;
23 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.VnfmAdapterCreateVnfTaskConfiguration.VNFM_HTTP_REST_SERVICE_PROVIDER_BEAN;
24 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
25 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
27 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
28 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
29 import org.onap.so.rest.exceptions.InvalidRestRequestException;
30 import org.onap.so.rest.exceptions.RestProcessingException;
31 import org.onap.so.rest.service.HttpRestServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Service;
39 import com.google.common.base.Optional;
42 * @author waqas.ikram@est.tech
45 public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvider {
47 private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterServiceProviderImpl.class);
48 public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
50 private final VnfmAdapterUrlProvider urlProvider;
51 private final HttpRestServiceProvider httpServiceProvider;
54 public VnfmAdapterServiceProviderImpl(final VnfmAdapterUrlProvider urlProvider,
55 @Qualifier(VNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
56 this.urlProvider = urlProvider;
57 this.httpServiceProvider = httpServiceProvider;
61 public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
62 final CreateVnfRequest request) {
64 final String url = urlProvider.getCreateInstantiateUrl(vnfId);
66 final ResponseEntity<CreateVnfResponse> response =
67 httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
69 final HttpStatus httpStatus = response.getStatusCode();
70 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
71 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
72 return Optional.absent();
75 if (!response.hasBody()) {
76 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
77 return Optional.absent();
80 final CreateVnfResponse createVnfResponse = response.getBody();
82 if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
83 LOGGER.error("Received invalid instantiation response: {}", response);
84 return Optional.absent();
87 return Optional.of(createVnfResponse);
88 } catch (final RestProcessingException | InvalidRestRequestException
89 | HttpResouceNotFoundException httpInvocationException) {
90 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
91 return Optional.absent();
97 public Optional<DeleteVnfResponse> invokeDeleteRequest(final String vnfId) {
99 final String url = urlProvider.getDeleteUrl(vnfId);
100 LOGGER.debug("Will send request to vnfm adapter using url: {}", url);
102 final ResponseEntity<DeleteVnfResponse> response =
103 httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
105 LOGGER.debug("Response received: ", response);
107 final HttpStatus httpStatus = response.getStatusCode();
109 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
110 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
111 return Optional.absent();
114 if (!response.hasBody()) {
115 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
116 return Optional.absent();
118 final DeleteVnfResponse deleteVnfResponse = response.getBody();
120 if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
121 LOGGER.error("Received invalid delete response: {}", response);
122 return Optional.absent();
124 return Optional.of(deleteVnfResponse);
125 } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
126 LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
127 return Optional.absent();
132 public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
134 final String url = urlProvider.getJobStatusUrl(jobId);
136 final ResponseEntity<QueryJobResponse> response =
137 httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
139 final HttpStatus httpStatus = response.getStatusCode();
141 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
142 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
143 return Optional.absent();
146 if (!response.hasBody()) {
147 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
148 return Optional.absent();
150 return Optional.of(response.getBody());
151 } catch (final RestProcessingException | InvalidRestRequestException
152 | HttpResouceNotFoundException httpInvocationException) {
153 LOGGER.error("Unexpected error while processing job request", httpInvocationException);
154 throw httpInvocationException;