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.onap.vnfmadapter.v1.model.QueryJobResponse;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Service;
36 import com.google.common.base.Optional;
39 * @author waqas.ikram@est.tech
42 public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterServiceProviderImpl.class);
45 public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
47 private final VnfmAdapterUrlProvider urlProvider;
48 private final HttpRestServiceProvider httpServiceProvider;
51 public VnfmAdapterServiceProviderImpl(final VnfmAdapterUrlProvider urlProvider,
52 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.absent();
72 if (!response.hasBody()) {
73 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
74 return Optional.absent();
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.absent();
84 return Optional.of(createVnfResponse);
85 } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
86 LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
87 return Optional.absent();
93 public Optional<DeleteVnfResponse> invokeDeleteRequest(final String vnfId) {
95 final String url = urlProvider.getDeleteUrl(vnfId);
96 LOGGER.debug("Will send request to vnfm adapter using url: {}", url);
98 final ResponseEntity<DeleteVnfResponse> response =
99 httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
101 LOGGER.debug("Response received: ", response);
103 final HttpStatus httpStatus = response.getStatusCode();
105 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
106 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
107 return Optional.absent();
110 if (!response.hasBody()) {
111 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
112 return Optional.absent();
114 final DeleteVnfResponse deleteVnfResponse = response.getBody();
116 if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
117 LOGGER.error("Received invalid delete response: {}", response);
118 return Optional.absent();
120 return Optional.of(deleteVnfResponse);
121 } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
122 LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
123 return Optional.absent();
128 public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
130 final String url = urlProvider.getJobStatusUrl(jobId);
132 final ResponseEntity<QueryJobResponse> response =
133 httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
135 final HttpStatus httpStatus = response.getStatusCode();
137 if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
138 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
139 return Optional.absent();
142 if (!response.hasBody()) {
143 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
144 return Optional.absent();
146 return Optional.of(response.getBody());
147 } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
148 LOGGER.error("Unexpected error while processing job request", httpInvocationException);
149 return Optional.absent();