948f5fc269028152139325cba2bcaec51a2bc9d2
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.extclients.vnfm;
22
23 import com.google.common.base.Optional;
24 import org.onap.aai.domain.yang.EsrVnfm;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
32 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
33 import org.onap.so.rest.service.HttpRestServiceProvider;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Service;
40
41 @Service
42 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
43     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
44
45     private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
46     private final VnfmUrlProvider urlProvider;
47
48     @Autowired
49     public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
50             VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration) {
51         this.vnfmServiceProviderConfiguration = vnfmServiceProviderConfiguration;
52         this.urlProvider = urlProvider;
53     }
54
55     @Override
56     public Optional<InlineResponse201> getVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
57         return getHttpServiceProvider(vnfm).get(vnfSelfLink, InlineResponse201.class);
58     }
59
60     @Override
61     public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
62             final InstantiateVnfRequest instantiateVnfRequest) {
63         logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
64
65         ResponseEntity<Void> response = null;
66         try {
67             response = getHttpServiceProvider(vnfm).postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
68                     Void.class);
69         } catch (final Exception exception) {
70             final String errorMessage =
71                     "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
72             logger.error(errorMessage, exception);
73             throw new VnfmRequestFailureException(errorMessage, exception);
74         }
75         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
76             final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
77                     + response.getStatusCode() + ", request: " + instantiateVnfRequest;
78             logger.error(errorMessage);
79             throw new VnfmRequestFailureException(errorMessage);
80         }
81         final String locationHeader = response.getHeaders().get("Location").iterator().next();
82         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
83     }
84
85     @Override
86     public InlineResponse2001 subscribeForNotifications(final EsrVnfm vnfm,
87             final LccnSubscriptionRequest subscriptionRequest) {
88         logger.info("Subscribing for notifications {}", subscriptionRequest);
89         final String url = urlProvider.getSubscriptionsUrl(vnfm.getVnfmId());
90         ResponseEntity<InlineResponse2001> response = null;
91         try {
92             response = getHttpServiceProvider(vnfm).postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
93             logger.info("Subscribing for notifications response {}", response);
94         } catch (final Exception exception) {
95             final String errorMessage =
96                     "Subscription to VNFM " + vnfm.getVnfmId() + " resulted in exception" + subscriptionRequest;
97             logger.error(errorMessage, exception);
98             throw new VnfmRequestFailureException(errorMessage, exception);
99         }
100         if (response.getStatusCode() != HttpStatus.CREATED) {
101             final String errorMessage = "Subscription to VNFM " + vnfm.getVnfmId() + " returned status code: "
102                     + response.getStatusCode() + ", request: " + subscriptionRequest;
103             logger.error(errorMessage);
104             throw new VnfmRequestFailureException(errorMessage);
105         }
106         return response.getBody();
107     }
108
109     @Override
110     public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
111             final TerminateVnfRequest terminateVnfRequest) {
112         logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
113
114         ResponseEntity<Void> response = null;
115         try {
116             response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
117                     Void.class);
118         } catch (final Exception exception) {
119             final String errorMessage =
120                     "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
121             logger.error(errorMessage, exception);
122             throw new VnfmRequestFailureException(errorMessage, exception);
123         }
124         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
125             final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
126                     + response.getStatusCode() + ", request: " + terminateVnfRequest;
127             logger.error(errorMessage);
128             throw new VnfmRequestFailureException(errorMessage);
129         }
130         final String locationHeader = response.getHeaders().get("Location").iterator().next();
131         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
132
133     }
134
135     @Override
136     public void deleteVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
137         logger.debug("Sending delete request to : " + vnfSelfLink);
138         final ResponseEntity<Void> response = getHttpServiceProvider(vnfm).deleteHttpRequest(vnfSelfLink, Void.class);
139         if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
140             throw new VnfmRequestFailureException(
141                     "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
142         }
143     }
144
145     @Override
146     public Optional<InlineResponse200> getOperation(final EsrVnfm vnfm, final String operationId) {
147         final String url = urlProvider.getOperationUrl(vnfm.getVnfmId(), operationId);
148         return getHttpServiceProvider(vnfm).get(url, InlineResponse200.class);
149     }
150
151     @Override
152     public Optional<InlineResponse201> createVnf(final EsrVnfm vnfm, final CreateVnfRequest createVnfRequest) {
153         final String url = urlProvider.getCreationUrl(vnfm.getVnfmId());
154         logger.debug("Sending create request {} to : {}", createVnfRequest, url);
155         try {
156             return getHttpServiceProvider(vnfm).post(createVnfRequest, url, InlineResponse201.class);
157         } catch (final Exception exception) {
158             final String errorMessage =
159                     "Create request to vnfm:" + vnfm.getVnfmId() + " resulted in exception" + createVnfRequest;
160             logger.error(errorMessage, exception);
161             throw new VnfmRequestFailureException(errorMessage, exception);
162         }
163     }
164
165     private HttpRestServiceProvider getHttpServiceProvider(final EsrVnfm vnfm) {
166         return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);
167     }
168
169 }