[SO] Pending Create changes for SO-API and BPMN-INFRA to support CNF's through ASD
[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.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 CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
45     private final CnfmUrlProvider cnfmUrlProvider;
46     private final HttpRestServiceProvider httpServiceProvider;
47
48     @Autowired
49     public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
50             @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
51         this.cnfmUrlProvider = cnfmUrlProvider;
52         this.httpServiceProvider = httpServiceProvider;
53     }
54
55     @Override
56     public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
57         try {
58             final String url = cnfmUrlProvider.getCreateAsRequestUrl();
59             final ResponseEntity<AsInstance> response =
60                     httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
61
62             final HttpStatus httpStatus = response.getStatusCode();
63             if (httpStatus.is2xxSuccessful()) {
64                 if (!response.hasBody()) {
65                     LOGGER.error("Received response without body: {}", response);
66                     return Optional.empty();
67                 }
68
69                 final AsInstance asInstance = response.getBody();
70                 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
71                     LOGGER.error("Received invalid response missing asInstanceid: {}", response);
72                     return Optional.empty();
73                 }
74                 return Optional.of(asInstance);
75             }
76             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
77             return Optional.empty();
78
79         } catch (final RestProcessingException | InvalidRestRequestException
80                 | HttpResouceNotFoundException httpInvocationException) {
81             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
82             return Optional.empty();
83         }
84     }
85
86     @Override
87     public Optional<URI> invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
88         try {
89
90             final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
91             final ResponseEntity<AsInstance> response =
92                     httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
93             final HttpStatus httpStatus = response.getStatusCode();
94             if (httpStatus.is2xxSuccessful()) {
95                 URI statusUri = response.getHeaders().getLocation();
96                 if (statusUri == null) {
97                     LOGGER.error("Received response without status URL for instance ID: {}", asInstanceId);
98                     return Optional.empty();
99                 }
100                 return Optional.of(statusUri);
101             }
102             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
103             return Optional.empty();
104
105         } catch (final RestProcessingException | InvalidRestRequestException
106                 | HttpResouceNotFoundException httpInvocationException) {
107             LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
108             return Optional.empty();
109         }
110
111     }
112
113     @Override
114     public Optional<AsLcmOpOcc> getInstantiateOperationJobStatus(final String url) {
115         try {
116             final ResponseEntity<AsLcmOpOcc> response = httpServiceProvider.getHttpResponse(url, AsLcmOpOcc.class);
117
118             final HttpStatus httpStatus = response.getStatusCode();
119
120             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
121                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
122                 return Optional.empty();
123             }
124
125             if (!response.hasBody()) {
126                 LOGGER.error("CNFM status response recieved without body: {}", response);
127                 return Optional.empty();
128             }
129             return Optional.of(response.getBody());
130         } catch (final RestProcessingException | InvalidRestRequestException
131                 | HttpResouceNotFoundException httpInvocationException) {
132             LOGGER.error("Unexpected error while processing job request", httpInvocationException);
133             throw httpInvocationException;
134         }
135     }
136
137 }