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