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;
45 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
46 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
48 private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
49 private final VnfmUrlProvider urlProvider;
52 public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
53 final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration) {
54 this.vnfmServiceProviderConfiguration = vnfmServiceProviderConfiguration;
55 this.urlProvider = urlProvider;
59 public Optional<InlineResponse201> getVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
60 return getHttpServiceProvider(vnfm).get(vnfSelfLink, InlineResponse201.class);
64 public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
65 final InstantiateVnfRequest instantiateVnfRequest) {
66 logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
68 ResponseEntity<Void> response = null;
70 response = getHttpServiceProvider(vnfm).postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
72 } catch (final Exception exception) {
73 final String errorMessage =
74 "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
75 logger.error(errorMessage, exception);
76 throw new VnfmRequestFailureException(errorMessage, exception);
78 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
79 final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
80 + response.getStatusCode() + ", request: " + instantiateVnfRequest;
81 logger.error(errorMessage);
82 throw new VnfmRequestFailureException(errorMessage);
84 final String locationHeader = response.getHeaders().get("Location").iterator().next();
85 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
89 public InlineResponse2001 subscribeForNotifications(final EsrVnfm vnfm,
90 final LccnSubscriptionRequest subscriptionRequest) {
91 logger.info("Subscribing for notifications {}", subscriptionRequest);
92 final String url = urlProvider.getSubscriptionsUrl(vnfm.getVnfmId());
93 ResponseEntity<InlineResponse2001> response = null;
95 response = getHttpServiceProvider(vnfm).postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
96 logger.info("Subscribing for notifications response {}", response);
97 } catch (final Exception exception) {
98 final String errorMessage =
99 "Subscription to VNFM " + vnfm.getVnfmId() + " resulted in exception" + subscriptionRequest;
100 logger.error(errorMessage, exception);
101 throw new VnfmRequestFailureException(errorMessage, exception);
103 if (response.getStatusCode() != HttpStatus.CREATED) {
104 final String errorMessage = "Subscription to VNFM " + vnfm.getVnfmId() + " returned status code: "
105 + response.getStatusCode() + ", request: " + subscriptionRequest;
106 logger.error(errorMessage);
107 throw new VnfmRequestFailureException(errorMessage);
109 return response.getBody();
113 public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
114 final TerminateVnfRequest terminateVnfRequest) {
115 logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
117 ResponseEntity<Void> response = null;
119 response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
121 } catch (final RestProcessingException restProcessingException) {
122 if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
123 final InlineResponse201 vnf = getVnf(vnfm, vnfSelfLink).get();
124 if (vnf.getInstantiationState().equals(InstantiationStateEnum.NOT_INSTANTIATED)) {
125 return JobManager.ALREADY_COMPLETED_OPERATION_ID;
127 final String errorMessage =
128 "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
129 logger.error(errorMessage, restProcessingException);
130 throw new VnfmRequestFailureException(errorMessage, restProcessingException);
133 } catch (final Exception exception) {
134 final String errorMessage =
135 "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
136 logger.error(errorMessage, exception);
137 throw new VnfmRequestFailureException(errorMessage, exception);
139 checkIfResponseIsAcceptable(response, vnfSelfLink, terminateVnfRequest);
140 final String locationHeader = response.getHeaders().get("Location").iterator().next();
141 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
145 public void deleteVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
146 logger.debug("Sending delete request to : " + vnfSelfLink);
147 final ResponseEntity<Void> response = getHttpServiceProvider(vnfm).deleteHttpRequest(vnfSelfLink, Void.class);
148 if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
149 throw new VnfmRequestFailureException(
150 "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
155 public Optional<InlineResponse200> getOperation(final EsrVnfm vnfm, final String operationId) {
156 final String url = urlProvider.getOperationUrl(vnfm.getVnfmId(), operationId);
157 return getHttpServiceProvider(vnfm).get(url, InlineResponse200.class);
161 public Optional<InlineResponse201> createVnf(final EsrVnfm vnfm, final CreateVnfRequest createVnfRequest) {
162 final String url = urlProvider.getCreationUrl(vnfm.getVnfmId());
163 logger.debug("Sending create request {} to : {}", createVnfRequest, url);
165 return getHttpServiceProvider(vnfm).post(createVnfRequest, url, InlineResponse201.class);
166 } catch (final Exception exception) {
167 final String errorMessage =
168 "Create request to vnfm:" + vnfm.getVnfmId() + " resulted in exception" + createVnfRequest;
169 logger.error(errorMessage, exception);
170 throw new VnfmRequestFailureException(errorMessage, exception);
174 private void checkIfResponseIsAcceptable(final ResponseEntity<Void> response, final String vnfSelfLink,
175 final TerminateVnfRequest terminateVnfRequest) {
176 if (response == null) {
177 final String errorMessage =
178 "Terminate request to " + vnfSelfLink + ", response is null, " + "request: " + terminateVnfRequest;
179 logger.error(errorMessage);
180 throw new VnfmRequestFailureException(errorMessage);
182 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
183 final String errorMessage = "Terminate request to " + vnfSelfLink + ", returned status code: "
184 + response.getStatusCode() + ", request: " + terminateVnfRequest;
185 logger.error(errorMessage);
186 throw new VnfmRequestFailureException(errorMessage);
190 private HttpRestServiceProvider getHttpServiceProvider(final EsrVnfm vnfm) {
191 return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);