Fixing SO build
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-lcm / etsi-sol003-lcm-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / vnfm / VnfmServiceProviderImpl.java
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.InlineResponse201.InstantiationStateEnum;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
33 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
34 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
35 import org.onap.so.rest.exceptions.RestProcessingException;
36 import org.onap.so.rest.service.HttpRestServiceProvider;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Service;
43
44 @Service
45 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
46     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
47
48     private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
49     private final VnfmUrlProvider urlProvider;
50
51     @Autowired
52     public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
53             VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration) {
54         this.vnfmServiceProviderConfiguration = vnfmServiceProviderConfiguration;
55         this.urlProvider = urlProvider;
56     }
57
58     @Override
59     public Optional<InlineResponse201> getVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
60         return getHttpServiceProvider(vnfm).get(vnfSelfLink, InlineResponse201.class);
61     }
62
63     @Override
64     public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
65             final InstantiateVnfRequest instantiateVnfRequest) {
66         logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
67
68         ResponseEntity<Void> response = null;
69         try {
70             response = getHttpServiceProvider(vnfm).postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
71                     Void.class);
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);
77         }
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);
83         }
84         final String locationHeader = response.getHeaders().get("Location").iterator().next();
85         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
86     }
87
88     @Override
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;
94         try {
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);
102         }
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);
108         }
109         return response.getBody();
110     }
111
112     @Override
113     public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
114             final TerminateVnfRequest terminateVnfRequest) {
115         logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
116
117         ResponseEntity<Void> response = null;
118         try {
119             response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
120                     Void.class);
121         } catch (final RestProcessingException restProcessingException) {
122             if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
123                 InlineResponse201 vnf = getVnf(vnfm, vnfSelfLink).get();
124                 if (vnf.getInstantiationState().equals(InstantiationStateEnum.NOT_INSTANTIATED)) {
125                     return JobManager.ALREADY_COMPLETED_OPERATION_ID;
126                 } else {
127                     final String errorMessage =
128                             "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
129                     logger.error(errorMessage, restProcessingException);
130                     throw new VnfmRequestFailureException(errorMessage, restProcessingException);
131                 }
132             }
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);
138         }
139         checkIfResponseIsAcceptable(response, vnfSelfLink, terminateVnfRequest);
140         final String locationHeader = response.getHeaders().get("Location").iterator().next();
141         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
142     }
143
144     @Override
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());
151         }
152     }
153
154     @Override
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);
158     }
159
160     @Override
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);
164         try {
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);
171         }
172     }
173
174     private void checkIfResponseIsAcceptable(ResponseEntity<Void> response, String vnfSelfLink,
175             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);
181         }
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);
187         }
188     }
189
190     private HttpRestServiceProvider getHttpServiceProvider(final EsrVnfm vnfm) {
191         return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);
192     }
193
194 }