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.CreateVnfRequest;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
31 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
32 import org.onap.so.rest.service.HttpRestServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Service;
42 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
43 private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
45 private final HttpRestServiceProvider httpServiceProvider;
46 private final VnfmUrlProvider urlProvider;
49 public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
50 @Qualifier("vnfmServiceProvider") final HttpRestServiceProvider httpServiceProvider) {
51 this.httpServiceProvider = httpServiceProvider;
52 this.urlProvider = urlProvider;
56 public Optional<InlineResponse201> getVnf(final String vnfSelfLink) {
57 return httpServiceProvider.get(vnfSelfLink, InlineResponse201.class);
61 public String instantiateVnf(final String vnfSelfLink, final InstantiateVnfRequest instantiateVnfRequest) {
62 logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
64 ResponseEntity<Void> response = null;
66 response = httpServiceProvider.postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
68 } catch (final Exception exception) {
69 final String errorMessage =
70 "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
71 logger.error(errorMessage, exception);
72 throw new VnfmRequestFailureException(errorMessage, exception);
74 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
75 final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
76 + response.getStatusCode() + ", request: " + instantiateVnfRequest;
77 logger.error(errorMessage);
78 throw new VnfmRequestFailureException(errorMessage);
80 final String locationHeader = response.getHeaders().get("Location").iterator().next();
81 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
85 public InlineResponse2001 subscribeForNotifications(final String vnfmId,
86 final LccnSubscriptionRequest subscriptionRequest) {
87 logger.info("Subscribing for notifications {}", subscriptionRequest);
88 final String url = urlProvider.getSubscriptionsUrl(vnfmId);
89 ResponseEntity<InlineResponse2001> response = null;
91 response = httpServiceProvider.postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
92 logger.info("Subscribing for notifications response {}", response);
93 } catch (final Exception exception) {
94 final String errorMessage =
95 "Subscription to VNFM " + vnfmId + " resulted in exception" + subscriptionRequest;
96 logger.error(errorMessage, exception);
97 throw new VnfmRequestFailureException(errorMessage, exception);
99 if (response.getStatusCode() != HttpStatus.OK) {
100 final String errorMessage = "Subscription to VNFM " + vnfmId + " returned status code: "
101 + response.getStatusCode() + ", request: " + subscriptionRequest;
102 logger.error(errorMessage);
103 throw new VnfmRequestFailureException(errorMessage);
105 return response.getBody();
109 public String terminateVnf(final String vnfSelfLink, final TerminateVnfRequest terminateVnfRequest) {
110 logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
112 ResponseEntity<Void> response = null;
114 response = httpServiceProvider.postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate", Void.class);
115 } catch (final Exception exception) {
116 final String errorMessage =
117 "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
118 logger.error(errorMessage, exception);
119 throw new VnfmRequestFailureException(errorMessage, exception);
121 if (response.getStatusCode() != HttpStatus.ACCEPTED) {
122 final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
123 + response.getStatusCode() + ", request: " + terminateVnfRequest;
124 logger.error(errorMessage);
125 throw new VnfmRequestFailureException(errorMessage);
127 final String locationHeader = response.getHeaders().get("Location").iterator().next();
128 return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
133 public void deleteVnf(final String vnfSelfLink) {
134 logger.debug("Sending delete request to : " + vnfSelfLink);
135 final ResponseEntity<Void> response = httpServiceProvider.deleteHttpRequest(vnfSelfLink, Void.class);
136 if (response.getStatusCode() != HttpStatus.OK) {
137 throw new VnfmRequestFailureException(
138 "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
143 public Optional<InlineResponse200> getOperation(final String vnfmId, final String operationId) {
144 final String url = urlProvider.getOperationUrl(vnfmId, operationId);
145 return httpServiceProvider.get(url, InlineResponse200.class);
149 public Optional<InlineResponse201> createVnf(final String vnfmId, final CreateVnfRequest createVnfRequest) {
150 final String url = urlProvider.getCreationUrl(vnfmId);
151 logger.debug("Sending create request {} to : {}", createVnfRequest, url);
153 return httpServiceProvider.post(createVnfRequest, url, InlineResponse201.class);
154 } catch (final Exception exception) {
155 final String errorMessage =
156 "Create request to vnfm:" + vnfmId + " resulted in exception" + createVnfRequest;
157 logger.error(errorMessage, exception);
158 throw new VnfmRequestFailureException(errorMessage, exception);