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