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 org.onap.so.rest.exceptions.InvalidRestRequestException;
24 import org.onap.so.rest.exceptions.RestProcessingException;
25 import org.onap.so.rest.service.HttpRestServiceProvider;
26 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
27 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
28 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Service;
35 import com.google.common.base.Optional;
38 * @author waqas.ikram@est.tech
41 public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvider {
43 private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterServiceProviderImpl.class);
45 private final VnfmAdapterUrlProvider urlProvider;
46 private final HttpRestServiceProvider httpServiceProvider;
49 public VnfmAdapterServiceProviderImpl(final VnfmAdapterUrlProvider urlProvider,
50 final HttpRestServiceProvider httpServiceProvider) {
51 this.urlProvider = urlProvider;
52 this.httpServiceProvider = httpServiceProvider;
56 public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
57 final CreateVnfRequest request) {
59 final String url = urlProvider.getCreateInstantiateUrl(vnfId);
61 final ResponseEntity<CreateVnfResponse> response =
62 httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
64 final HttpStatus httpStatus = response.getStatusCode();
65 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
66 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
67 return Optional.absent();
70 if (!response.hasBody()) {
71 LOGGER.error("Received response without body: {}", response);
72 return Optional.absent();
75 final CreateVnfResponse createVnfResponse = response.getBody();
77 if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
78 LOGGER.error("Received invalid instantiation response: {}", response);
79 return Optional.absent();
82 return Optional.of(createVnfResponse);
83 } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
84 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
85 return Optional.absent();
91 public Optional<DeleteVnfResponse> invokeDeleteRequest(final String vnfId) {
93 final String url = urlProvider.getDeleteUrl(vnfId);
94 LOGGER.debug("Will send request to vnfm adapter using url: {}", url);
96 final ResponseEntity<DeleteVnfResponse> response =
97 httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
99 LOGGER.debug("Response received: ", response);
101 final HttpStatus httpStatus = response.getStatusCode();
103 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
104 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
105 return Optional.absent();
108 if (!response.hasBody()) {
109 LOGGER.error("Received response without body: {}", response);
110 return Optional.absent();
112 final DeleteVnfResponse deleteVnfResponse = response.getBody();
114 if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
115 LOGGER.error("Received invalid delete response: {}", response);
116 return Optional.absent();
118 return Optional.of(deleteVnfResponse);
119 } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
120 LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
121 return Optional.absent();