Fixing vulnerabilities and code smells
[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.apache.commons.lang3.StringUtils;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
27 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
28 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
29 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
30 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
31 import org.onap.so.rest.exceptions.InvalidRestRequestException;
32 import org.onap.so.rest.exceptions.RestProcessingException;
33 import org.onap.so.rest.service.HttpRestServiceProvider;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Service;
41
42 @Service
43 public class Sol003AdapterServiceProviderImpl implements Sol003AdapterServiceProvider {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(Sol003AdapterServiceProviderImpl.class);
46     public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
47
48     private final Sol003AdapterUrlProvider urlProvider;
49     private final HttpRestServiceProvider httpServiceProvider;
50
51     @Autowired
52     public Sol003AdapterServiceProviderImpl(final Sol003AdapterUrlProvider urlProvider,
53             @Qualifier(SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
54         this.urlProvider = urlProvider;
55         this.httpServiceProvider = httpServiceProvider;
56     }
57
58     @Override
59     public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
60             final CreateVnfRequest request) {
61         try {
62             final String url = urlProvider.getCreateInstantiateUrl(vnfId);
63
64             final ResponseEntity<CreateVnfResponse> response =
65                     httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
66
67             final HttpStatus httpStatus = response.getStatusCode();
68             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
69                 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
70                 return Optional.empty();
71             }
72
73             if (!response.hasBody()) {
74                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
75                 return Optional.empty();
76             }
77
78             final CreateVnfResponse createVnfResponse = response.getBody();
79
80             if (StringUtils.isBlank(createVnfResponse.getJobId())) {
81                 LOGGER.error("Received invalid instantiation response: {}", response);
82                 return Optional.empty();
83             }
84
85             return Optional.of(createVnfResponse);
86         } catch (final RestProcessingException | InvalidRestRequestException
87                 | HttpResouceNotFoundException httpInvocationException) {
88             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
89             return Optional.empty();
90         }
91
92     }
93
94     @Override
95     public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
96         try {
97             final String url = urlProvider.getJobStatusUrl(jobId);
98
99             final ResponseEntity<QueryJobResponse> response =
100                     httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
101
102             final HttpStatus httpStatus = response.getStatusCode();
103
104             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
105                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
106                 return Optional.empty();
107             }
108
109             if (!response.hasBody()) {
110                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
111                 return Optional.empty();
112             }
113             return Optional.of(response.getBody());
114         } catch (final RestProcessingException | InvalidRestRequestException | HttpResouceNotFoundException exception) {
115             LOGGER.error("Unexpected error while processing job request", exception);
116             throw exception;
117         }
118     }
119
120     @Override
121     public Optional<DeleteVnfResponse> invokeTerminationRequest(final String vnfId) {
122         try {
123             final String url = urlProvider.getTerminateVnfUrl(vnfId);
124
125             final ResponseEntity<DeleteVnfResponse> response =
126                     httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
127             final HttpStatus httpStatus = response.getStatusCode();
128             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
129                 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
130                 return Optional.empty();
131             }
132             if (!response.hasBody()) {
133                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
134                 return Optional.empty();
135             }
136
137             final DeleteVnfResponse deleteVnfResponse = response.getBody();
138             if (StringUtils.isBlank(deleteVnfResponse.getJobId())) {
139                 LOGGER.error("Received invalid terminate response: {}", response);
140                 return Optional.empty();
141             }
142
143             return Optional.of(deleteVnfResponse);
144         } catch (final RestProcessingException | InvalidRestRequestException
145                 | HttpResouceNotFoundException httpInvocationException) {
146             LOGGER.error("Unexpected error while processing terminate request", httpInvocationException);
147             return Optional.empty();
148         }
149     }
150 }