f861855b9964694da7afa5ddb5a3ebba8f6ab565
[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
38 @Service
39 public class CnfmHttpServiceProviderImpl implements CnfmHttpServiceProvider {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(CnfmHttpServiceProviderImpl.class);
42     private final CnfmUrlProvider cnfmUrlProvider;
43     private final HttpRestServiceProvider httpServiceProvider;
44
45     @Autowired
46     public CnfmHttpServiceProviderImpl(final CnfmUrlProvider cnfmUrlProvider,
47             @Qualifier(CNFM_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
48         this.cnfmUrlProvider = cnfmUrlProvider;
49         this.httpServiceProvider = httpServiceProvider;
50     }
51
52     @Override
53     public Optional<AsInstance> invokeCreateAsRequest(final CreateAsRequest createAsRequest) {
54         try {
55             final String url = cnfmUrlProvider.getCreateAsRequestUrl();
56             final ResponseEntity<AsInstance> response =
57                     httpServiceProvider.postHttpRequest(createAsRequest, url, AsInstance.class);
58
59             final HttpStatus httpStatus = response.getStatusCode();
60             if (httpStatus.is2xxSuccessful()) {
61                 if (!response.hasBody()) {
62                     LOGGER.error("Received response without body: {}", response);
63                     return Optional.empty();
64                 }
65
66                 final AsInstance asInstance = response.getBody();
67                 if (asInstance.getAsInstanceid() == null || asInstance.getAsInstanceid().isEmpty()) {
68                     LOGGER.error("Received invalid response missing asInstanceid: {}", response);
69                     return Optional.empty();
70                 }
71                 return Optional.of(asInstance);
72             }
73             LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
74             return Optional.empty();
75
76         } catch (final RestProcessingException | InvalidRestRequestException
77                 | HttpResouceNotFoundException httpInvocationException) {
78             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
79             return Optional.empty();
80         }
81     }
82
83 }