CLEANED UP IMPORTS IN MSO-VFNM-ETSI Adapter
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-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.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;
40
41 @Service
42 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
43     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
44
45     private final HttpRestServiceProvider httpServiceProvider;
46     private final VnfmUrlProvider urlProvider;
47
48     @Autowired
49     public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
50             @Qualifier("vnfmServiceProvider") final HttpRestServiceProvider httpServiceProvider) {
51         this.httpServiceProvider = httpServiceProvider;
52         this.urlProvider = urlProvider;
53     }
54
55     @Override
56     public Optional<InlineResponse201> getVnf(final String vnfSelfLink) {
57         return httpServiceProvider.get(vnfSelfLink, InlineResponse201.class);
58     }
59
60     @Override
61     public String instantiateVnf(final String vnfSelfLink, final InstantiateVnfRequest instantiateVnfRequest) {
62         logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
63
64         ResponseEntity<Void> response = null;
65         try {
66             response = httpServiceProvider.postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
67                     Void.class);
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);
73         }
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);
79         }
80         final String locationHeader = response.getHeaders().get("Location").iterator().next();
81         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
82     }
83
84     @Override
85     public InlineResponse2001 subscribeForNotifications(final String vnfmId,
86             final LccnSubscriptionRequest subscriptionRequest) {
87         final String url = urlProvider.getSubscriptionsUrl(vnfmId);
88         ResponseEntity<InlineResponse2001> response = null;
89         try {
90             response = httpServiceProvider.postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
91         } catch (final Exception exception) {
92             final String errorMessage =
93                     "Subscription to VNFM " + vnfmId + " resulted in exception" + subscriptionRequest;
94             logger.error(errorMessage, exception);
95             throw new VnfmRequestFailureException(errorMessage, exception);
96         }
97         if (response.getStatusCode() != HttpStatus.OK) {
98             final String errorMessage = "Subscription to VNFM " + vnfmId + " returned status code: "
99                     + response.getStatusCode() + ", request: " + subscriptionRequest;
100             logger.error(errorMessage);
101             throw new VnfmRequestFailureException(errorMessage);
102         }
103         return response.getBody();
104     }
105
106     @Override
107     public String terminateVnf(final String vnfSelfLink, final TerminateVnfRequest terminateVnfRequest) {
108         logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
109
110         ResponseEntity<Void> response = null;
111         try {
112             response = httpServiceProvider.postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate", Void.class);
113         } catch (final Exception exception) {
114             final String errorMessage =
115                     "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
116             logger.error(errorMessage, exception);
117             throw new VnfmRequestFailureException(errorMessage, exception);
118         }
119         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
120             final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
121                     + response.getStatusCode() + ", request: " + terminateVnfRequest;
122             logger.error(errorMessage);
123             throw new VnfmRequestFailureException(errorMessage);
124         }
125         final String locationHeader = response.getHeaders().get("Location").iterator().next();
126         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
127
128     }
129
130     @Override
131     public void deleteVnf(final String vnfSelfLink) {
132         logger.debug("Sending delete request to : " + vnfSelfLink);
133         final ResponseEntity<Void> response = httpServiceProvider.deleteHttpRequest(vnfSelfLink, Void.class);
134         if (response.getStatusCode() != HttpStatus.OK) {
135             throw new VnfmRequestFailureException(
136                     "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
137         }
138     }
139
140     @Override
141     public Optional<InlineResponse200> getOperation(final String vnfmId, final String operationId) {
142         final String url = urlProvider.getOperationUrl(vnfmId, operationId);
143         return httpServiceProvider.get(url, InlineResponse200.class);
144     }
145
146     @Override
147     public Optional<InlineResponse201> createVnf(final String vnfmId, final CreateVnfRequest createVnfRequest) {
148         final String url = urlProvider.getCreationUrl(vnfmId);
149         try {
150             return httpServiceProvider.post(createVnfRequest, url, InlineResponse201.class);
151         } catch (final Exception exception) {
152             final String errorMessage =
153                     "Create request to vnfm:" + vnfmId + " resulted in exception" + createVnfRequest;
154             logger.error(errorMessage, exception);
155             throw new VnfmRequestFailureException(errorMessage, exception);
156         }
157     }
158
159 }