bb789607e40093991d1b94d6203e6f9b00140474
[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 import java.util.Optional;
24 import org.onap.so.cnfm.lcm.model.AsInstance;
25 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
26 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
27 import org.onap.so.rest.exceptions.InvalidRestRequestException;
28 import org.onap.so.rest.exceptions.RestProcessingException;
29 import org.onap.so.rest.service.HttpRestServiceProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
38
39 @Service
40 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
43     private final CnfmUrlProvider cnfmUrlProvider;
44     private final HttpRestServiceProvider httpServiceProvider;
45
46     @Autowired
47     public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
48             @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
49         this.cnfmUrlProvider = cnfmUrlProvider;
50         this.httpServiceProvider = httpServiceProvider;
51     }
52
53     @Override
54     public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
55         try {
56             final String url = cnfmUrlProvider.getCreateAsRequestUrl();
57             final ResponseEntity<AsInstance> response =
58                     httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
59
60             final HttpStatus httpStatus = response.getStatusCode();
61             if (httpStatus.is2xxSuccessful()) {
62                 if (!response.hasBody()) {
63                     LOGGER.error("Received response without body: {}", response);
64                     return Optional.empty();
65                 }
66
67                 final AsInstance asInstance = response.getBody();
68                 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
69                     LOGGER.error("Received invalid response missing asInstanceid: {}", response);
70                     return Optional.empty();
71                 }
72                 return Optional.of(asInstance);
73             }
74             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
75             return Optional.empty();
76
77         } catch (final RestProcessingException | InvalidRestRequestException
78                 | HttpResouceNotFoundException httpInvocationException) {
79             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
80             return Optional.empty();
81         }
82     }
83
84     @Override
85     public void invokeInstantiateAsRequest(InstantiateAsRequest instantiateAsRequest, String asInstanceId) {
86         try {
87
88             final String url = cnfmUrlProvider.getInstantiateAsRequestUrl(asInstanceId);
89             final ResponseEntity<AsInstance> response =
90                     httpServiceProvider.postHttpRequest(instantiateAsRequest, url, AsInstance.class);
91
92             final HttpStatus httpStatus = response.getStatusCode();
93             if (httpStatus.is2xxSuccessful()) {
94                 if (!response.hasBody()) {
95                     LOGGER.error("Received response without body: {}", response);
96                 }
97
98             }
99             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
100
101         } catch (final RestProcessingException | InvalidRestRequestException
102                 | HttpResouceNotFoundException httpInvocationException) {
103             LOGGER.error("Unexpected error while processing instantiation request", httpInvocationException);
104         }
105
106     }
107
108 }