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