a0798f6e55c50d90cdfccc80fbd92857adf44b91
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / extclients / vnfm / Sol003AdapterServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm;
22
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterConfiguration.SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN;
24 import java.util.Optional;
25 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
27 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
28 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
29 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
30 import org.onap.so.rest.exceptions.InvalidRestRequestException;
31 import org.onap.so.rest.exceptions.RestProcessingException;
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 Sol003AdapterServiceProviderImpl implements Sol003AdapterServiceProvider {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(Sol003AdapterServiceProviderImpl.class);
45     public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
46
47     private final Sol003AdapterUrlProvider urlProvider;
48     private final HttpRestServiceProvider httpServiceProvider;
49
50     @Autowired
51     public Sol003AdapterServiceProviderImpl(final Sol003AdapterUrlProvider urlProvider,
52             @Qualifier(SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
53         this.urlProvider = urlProvider;
54         this.httpServiceProvider = httpServiceProvider;
55     }
56
57     @Override
58     public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
59             final CreateVnfRequest request) {
60         try {
61             final String url = urlProvider.getCreateInstantiateUrl(vnfId);
62
63             final ResponseEntity<CreateVnfResponse> response =
64                     httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
65
66             final HttpStatus httpStatus = response.getStatusCode();
67             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
68                 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
69                 return Optional.empty();
70             }
71
72             if (!response.hasBody()) {
73                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
74                 return Optional.empty();
75             }
76
77             final CreateVnfResponse createVnfResponse = response.getBody();
78
79             if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
80                 LOGGER.error("Received invalid instantiation response: {}", response);
81                 return Optional.empty();
82             }
83
84             return Optional.of(createVnfResponse);
85         } catch (final RestProcessingException | InvalidRestRequestException
86                 | HttpResouceNotFoundException httpInvocationException) {
87             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
88             return Optional.empty();
89         }
90
91     }
92
93     @Override
94     public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
95         try {
96             final String url = urlProvider.getJobStatusUrl(jobId);
97
98             final ResponseEntity<QueryJobResponse> response =
99                     httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
100
101             final HttpStatus httpStatus = response.getStatusCode();
102
103             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
104                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
105                 return Optional.empty();
106             }
107
108             if (!response.hasBody()) {
109                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
110                 return Optional.empty();
111             }
112             return Optional.of(response.getBody());
113         } catch (final RestProcessingException | InvalidRestRequestException | HttpResouceNotFoundException exception) {
114             LOGGER.error("Unexpected error while processing job request", exception);
115             throw exception;
116         }
117     }
118
119     @Override
120     public Optional<DeleteVnfResponse> invokeTerminationRequest(final String vnfId) {
121         try {
122             final String url = urlProvider.getTerminateVnfUrl(vnfId);
123
124             final ResponseEntity<DeleteVnfResponse> response =
125                     httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
126             final HttpStatus httpStatus = response.getStatusCode();
127             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
128                 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
129                 return Optional.empty();
130             }
131             if (!response.hasBody()) {
132                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
133                 return Optional.empty();
134             }
135
136             final DeleteVnfResponse deleteVnfResponse = response.getBody();
137             if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
138                 LOGGER.error("Received invalid terminate response: {}", response);
139                 return Optional.empty();
140             }
141
142             return Optional.of(deleteVnfResponse);
143         } catch (final RestProcessingException | InvalidRestRequestException
144                 | HttpResouceNotFoundException httpInvocationException) {
145             LOGGER.error("Unexpected error while processing terminate request", httpInvocationException);
146             return Optional.empty();
147         }
148     }
149 }