[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / CnfmHttpServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
21
22 import static org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks.CnfmHttpServiceConfiguration.CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN;
23 import java.net.URI;
24 import java.util.Optional;
25 import org.onap.so.cnfm.lcm.model.AsInstance;
26 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
27 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
28 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
29 import org.onap.so.cnfm.lcm.model.TerminateAsRequest;
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 CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
46     private final CnfmUrlProvider cnfmUrlProvider;
47     private final HttpRestServiceProvider httpServiceProvider;
48
49     @Autowired
50     public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
51             @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
52         this.cnfmUrlProvider = cnfmUrlProvider;
53         this.httpServiceProvider = httpServiceProvider;
54     }
55
56     @Override
57     public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
58         try {
59             final String url = cnfmUrlProvider.getCreateAsRequestUrl();
60             final ResponseEntity<AsInstance> response =
61                     httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
62
63             final HttpStatus httpStatus = response.getStatusCode();
64             if (httpStatus.is2xxSuccessful()) {
65                 if (!response.hasBody()) {
66                     LOGGER.error("Received response without body: {}", response);
67                     return Optional.empty();
68                 }
69
70                 final AsInstance asInstance = response.getBody();
71                 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
72                     LOGGER.error("Received invalid response missing asInstanceid: {}", response);
73                     return Optional.empty();
74                 }
75                 return Optional.of(asInstance);
76             }
77             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
78             return Optional.empty();
79
80         } catch (final RestProcessingException | InvalidRestRequestException
81                 | HttpResouceNotFoundException httpInvocationException) {
82             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
83             return Optional.empty();
84         }
85     }
86
87     @Override
88     public Optional<URI> invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
89         try {
90
91             final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
92             final ResponseEntity<AsInstance> response =
93                     httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
94             final HttpStatus httpStatus = response.getStatusCode();
95             if (httpStatus.is2xxSuccessful()) {
96                 URI statusUri = response.getHeaders().getLocation();
97                 if (statusUri == null) {
98                     LOGGER.error("Received response without status URL for instance ID: {}", asInstanceId);
99                     return Optional.empty();
100                 }
101                 return Optional.of(statusUri);
102             }
103             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
104             return Optional.empty();
105
106         } catch (final RestProcessingException | InvalidRestRequestException
107                 | HttpResouceNotFoundException httpInvocationException) {
108             LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
109             return Optional.empty();
110         }
111
112     }
113
114     @Override
115     public Optional<AsLcmOpOcc> getOperationJobStatus(final String url) {
116         try {
117             final ResponseEntity<AsLcmOpOcc> response = httpServiceProvider.getHttpResponse(url, AsLcmOpOcc.class);
118
119             final HttpStatus httpStatus = response.getStatusCode();
120
121             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
122                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: {}", url, httpStatus.value());
123                 return Optional.empty();
124             }
125
126             if (!response.hasBody()) {
127                 LOGGER.error("CNFM status response recieved without body: {}", response);
128                 return Optional.empty();
129             }
130             return Optional.of(response.getBody());
131         } catch (final RestProcessingException | InvalidRestRequestException
132                 | HttpResouceNotFoundException httpInvocationException) {
133             LOGGER.error("Unexpected error while processing job request", httpInvocationException);
134             throw httpInvocationException;
135         }
136     }
137
138     @Override
139     public Optional<Boolean> invokeDeleteAsRequest(String asInstanceId) {
140         try {
141
142             final String url = cnfmUrlProvider.getDeleteAsRequestUrl(asInstanceId);
143             LOGGER.debug("Will send request to CNFM by uisng the url: {}", url);
144
145             ResponseEntity<Void> response = httpServiceProvider.deleteHttpRequest(url, Void.class);
146             final HttpStatus httpStatus = response.getStatusCode();
147             if (!(httpStatus.is2xxSuccessful())) {
148                 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
149                 return Optional.empty();
150             }
151             return Optional.of(Boolean.TRUE);
152         } catch (final RestProcessingException | InvalidRestRequestException
153                 | HttpResouceNotFoundException httpInvocationException) {
154             LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
155             return Optional.empty();
156         }
157     }
158
159     @Override
160     public Optional<URI> invokeTerminateAsRequest(String asInstanceId, TerminateAsRequest terminateAsRequest) {
161         try {
162
163             final String url = cnfmUrlProvider.getTerminateAsRequestUrl(asInstanceId);
164             LOGGER.debug("Will send request to CNFM to terminate by uisng the url: {}", url);
165
166             ResponseEntity<Void> response = httpServiceProvider.postHttpRequest(terminateAsRequest, url, Void.class);
167             final HttpStatus httpStatus = response.getStatusCode();
168             if (httpStatus.is2xxSuccessful()) {
169                 URI statusUri = response.getHeaders().getLocation();
170                 if (statusUri == null) {
171                     LOGGER.error("Received response without status URL while terminating of instance with ID: {}",
172                             asInstanceId);
173                     return Optional.empty();
174                 }
175                 return Optional.of(statusUri);
176             }
177             LOGGER.error("Unable to invoke HTTP DELETE while terminating by using URL: {}, Response Code: {}", url,
178                     httpStatus.value());
179             return Optional.empty();
180
181         } catch (final RestProcessingException | InvalidRestRequestException
182                 | HttpResouceNotFoundException httpInvocationException) {
183             LOGGER.error("Unexpected error while processing terminate request", httpInvocationException);
184             return Optional.empty();
185         }
186     }
187
188 }