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.adapters.vnfmadapter.extclients.vnfm;
23 import com.google.common.base.Optional;
24 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
28 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
29 import org.onap.so.rest.service.HttpRestServiceProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
39 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
40 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
42 private final HttpRestServiceProvider httpServiceProvider;
43 private final VnfmUrlProvider urlProvider;
46 public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
47 @Qualifier("vnfmServiceProvider") final HttpRestServiceProvider httpServiceProvider) {
48 this.httpServiceProvider = httpServiceProvider;
49 this.urlProvider = urlProvider;
53 public Optional<InlineResponse201> getVnf(final String vnfSelfLink) {
54 return httpServiceProvider.get(vnfSelfLink, InlineResponse201.class);
58 public String instantiateVnf(final String vnfSelfLink, final InstantiateVnfRequest instantiateVnfRequest) {
59 logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
61 ResponseEntity<Void> response = null;
63 response = httpServiceProvider.postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
65 } catch (final Exception exception) {
66 final String errorMessage =
67 "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
68 logger.error(errorMessage, exception);
69 throw new VnfmRequestFailureException(errorMessage, exception);
71 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
72 final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
73 + response.getStatusCode() + ", request: " + instantiateVnfRequest;
74 logger.error(errorMessage);
75 throw new VnfmRequestFailureException(errorMessage);
77 final String locationHeader = response.getHeaders().get("Location").iterator().next();
78 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
83 public String terminateVnf(final String vnfSelfLink, final TerminateVnfRequest terminateVnfRequest) {
84 logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
86 ResponseEntity<Void> response = null;
88 response = httpServiceProvider.postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate", Void.class);
89 } catch (final Exception exception) {
90 final String errorMessage =
91 "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
92 logger.error(errorMessage, exception);
93 throw new VnfmRequestFailureException(errorMessage, exception);
95 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
96 final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
97 + response.getStatusCode() + ", request: " + terminateVnfRequest;
98 logger.error(errorMessage);
99 throw new VnfmRequestFailureException(errorMessage);
101 final String locationHeader = response.getHeaders().get("Location").iterator().next();
102 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
107 public void deleteVnf(final String vnfSelfLink) {
108 logger.debug("Sending delete request to : " + vnfSelfLink);
109 final ResponseEntity<Void> response = httpServiceProvider.deleteHttpRequest(vnfSelfLink, Void.class);
110 if (response.getStatusCode() != HttpStatus.OK) {
111 throw new VnfmRequestFailureException(
112 "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
118 public Optional<InlineResponse200> getOperation(final String vnfmId, final String operationId) {
119 final String url = urlProvider.getOperationUrl(vnfmId, operationId);
120 return httpServiceProvider.get(url, InlineResponse200.class);