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.etsisol003adapter.lcm.extclients.vnfm;
23 import org.onap.aai.domain.yang.EsrVnfm;
24 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.CreateVnfRequest;
25 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse200;
26 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse2001;
27 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201;
28 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum;
29 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InstantiateVnfRequest;
30 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.LccnSubscriptionRequest;
31 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.TerminateVnfRequest;
32 import org.onap.so.adapters.etsisol003adapter.lcm.jobmanagement.JobManager;
33 import org.onap.so.adapters.etsisol003adapter.lcm.rest.exceptions.VnfmRequestFailureException;
34 import org.onap.so.rest.exceptions.RestProcessingException;
35 import org.onap.so.rest.service.HttpRestServiceProvider;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Service;
42 import com.google.common.base.Optional;
43 import java.util.List;
46 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
47 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
48 private static final String MESSAGE_RESULTED_IN_EXCEPTION = " resulted in exception";
49 private static final String MESSAGE_REQUEST = ", request: ";
50 private static final String MESSAGE_TERMINATE_REQUEST_TO = "Terminate request to ";
52 private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
53 private final VnfmUrlProvider urlProvider;
56 public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
57 final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration) {
58 this.vnfmServiceProviderConfiguration = vnfmServiceProviderConfiguration;
59 this.urlProvider = urlProvider;
63 public Optional<InlineResponse201> getVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
64 return getHttpServiceProvider(vnfm).get(vnfSelfLink, InlineResponse201.class);
68 public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
69 final InstantiateVnfRequest instantiateVnfRequest) {
70 logger.debug("Sending instantiate request {} to : {}", instantiateVnfRequest, vnfSelfLink);
71 ResponseEntity<Void> response = null;
73 response = getHttpServiceProvider(vnfm).postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
75 } catch (final Exception exception) {
76 final String errorMessage =
77 "Instantiate request to " + vnfSelfLink + MESSAGE_RESULTED_IN_EXCEPTION + instantiateVnfRequest;
78 logger.error(errorMessage);
79 throw new VnfmRequestFailureException(errorMessage, exception);
81 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
82 final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
83 + response.getStatusCode() + MESSAGE_REQUEST + instantiateVnfRequest;
84 logger.error(errorMessage);
85 throw new VnfmRequestFailureException(errorMessage);
87 String locationHeader = getLocationHeader(response);
88 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
92 public InlineResponse2001 subscribeForNotifications(final EsrVnfm vnfm,
93 final LccnSubscriptionRequest subscriptionRequest) {
94 logger.info("Subscribing for notifications {}", subscriptionRequest);
95 final String url = urlProvider.getSubscriptionsUrl(vnfm.getVnfmId());
96 ResponseEntity<InlineResponse2001> response = null;
98 response = getHttpServiceProvider(vnfm).postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
99 logger.info("Subscribing for notifications response {}", response);
100 } catch (final Exception exception) {
101 final String errorMessage =
102 "Subscription to VNFM " + vnfm.getVnfmId() + MESSAGE_RESULTED_IN_EXCEPTION + subscriptionRequest;
103 logger.error(errorMessage);
104 throw new VnfmRequestFailureException(errorMessage, exception);
106 if (response.getStatusCode() != HttpStatus.CREATED) {
107 final String errorMessage = "Subscription to VNFM " + vnfm.getVnfmId() + " returned status code: "
108 + response.getStatusCode() + MESSAGE_REQUEST + subscriptionRequest;
109 logger.error(errorMessage);
110 throw new VnfmRequestFailureException(errorMessage);
112 return response.getBody();
116 public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
117 final TerminateVnfRequest terminateVnfRequest) {
118 logger.debug("Sending terminate request {} to : {}", terminateVnfRequest, vnfSelfLink);
119 ResponseEntity<Void> response = null;
121 response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
123 } catch (final RestProcessingException restProcessingException) {
124 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
125 final InlineResponse201 vnf = getVnf(vnfm, vnfSelfLink).get();
126 if (vnf.getInstantiationState().equals(InstantiationStateEnum.NOT_INSTANTIATED)) {
127 return JobManager.ALREADY_COMPLETED_OPERATION_ID;
129 final String errorMessage = MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink
130 + MESSAGE_RESULTED_IN_EXCEPTION + terminateVnfRequest;
131 logger.error(errorMessage, restProcessingException);
132 throw new VnfmRequestFailureException(errorMessage, restProcessingException);
135 } catch (final Exception exception) {
136 final String errorMessage =
137 MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink + MESSAGE_RESULTED_IN_EXCEPTION + terminateVnfRequest;
138 logger.error(errorMessage);
139 throw new VnfmRequestFailureException(errorMessage, exception);
141 checkIfResponseIsAcceptable(response, vnfSelfLink, terminateVnfRequest);
142 String locationHeader = getLocationHeader(response);
143 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
147 public void deleteVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
148 logger.debug("Sending delete request to : {}", vnfSelfLink);
149 final ResponseEntity<Void> response = getHttpServiceProvider(vnfm).deleteHttpRequest(vnfSelfLink, Void.class);
150 if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
151 throw new VnfmRequestFailureException(
152 "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
157 public Optional<InlineResponse200> getOperation(final EsrVnfm vnfm, final String operationId) {
158 final String url = urlProvider.getOperationUrl(vnfm.getVnfmId(), operationId);
159 return getHttpServiceProvider(vnfm).get(url, InlineResponse200.class);
163 public Optional<InlineResponse201> createVnf(final EsrVnfm vnfm, final CreateVnfRequest createVnfRequest) {
164 final String url = urlProvider.getCreationUrl(vnfm.getVnfmId());
165 logger.debug("Sending create request {} to : {}", createVnfRequest, url);
167 return getHttpServiceProvider(vnfm).post(createVnfRequest, url, InlineResponse201.class);
168 } catch (final Exception exception) {
169 final String errorMessage =
170 "Create request to vnfm:" + vnfm.getVnfmId() + MESSAGE_RESULTED_IN_EXCEPTION + createVnfRequest;
171 logger.error(errorMessage);
172 throw new VnfmRequestFailureException(errorMessage, exception);
176 private void checkIfResponseIsAcceptable(final ResponseEntity<Void> response, final String vnfSelfLink,
177 final TerminateVnfRequest terminateVnfRequest) {
178 if (response == null) {
179 final String errorMessage = MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink + ", response is null, "
180 + "request: " + terminateVnfRequest;
181 logger.error(errorMessage);
182 throw new VnfmRequestFailureException(errorMessage);
184 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
185 final String errorMessage = MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink + ", returned status code: "
186 + response.getStatusCode() + MESSAGE_REQUEST + terminateVnfRequest;
187 logger.error(errorMessage);
188 throw new VnfmRequestFailureException(errorMessage);
192 private HttpRestServiceProvider getHttpServiceProvider(final EsrVnfm vnfm) {
193 return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);
196 private String getLocationHeader(ResponseEntity<Void> response) {
197 List<String> headers = response.getHeaders().get("Location");
198 if ((headers == null) || (headers.isEmpty())) {
199 throw new VnfmRequestFailureException("No headers found in response");
201 return headers.iterator().next();