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