Adding logic to communicate with SOL003 Adpater
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / extclients / vnfm / Sol003AdapterServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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
21 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm;
22
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterConfiguration.SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN;
24 import java.util.Optional;
25 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
27 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
28 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
29 import org.onap.so.rest.exceptions.InvalidRestRequestException;
30 import org.onap.so.rest.exceptions.RestProcessingException;
31 import org.onap.so.rest.service.HttpRestServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 public class Sol003AdapterServiceProviderImpl implements Sol003AdapterServiceProvider {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(Sol003AdapterServiceProviderImpl.class);
44     public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
45
46     private final Sol003AdapterUrlProvider urlProvider;
47     private final HttpRestServiceProvider httpServiceProvider;
48
49     @Autowired
50     public Sol003AdapterServiceProviderImpl(final Sol003AdapterUrlProvider urlProvider,
51             @Qualifier(SOL003_ADAPTER_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
52         this.urlProvider = urlProvider;
53         this.httpServiceProvider = httpServiceProvider;
54     }
55
56     @Override
57     public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
58             final CreateVnfRequest request) {
59         try {
60             final String url = urlProvider.getCreateInstantiateUrl(vnfId);
61
62             final ResponseEntity<CreateVnfResponse> response =
63                     httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
64
65             final HttpStatus httpStatus = response.getStatusCode();
66             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
67                 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
68                 return Optional.empty();
69             }
70
71             if (!response.hasBody()) {
72                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
73                 return Optional.empty();
74             }
75
76             final CreateVnfResponse createVnfResponse = response.getBody();
77
78             if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
79                 LOGGER.error("Received invalid instantiation response: {}", response);
80                 return Optional.empty();
81             }
82
83             return Optional.of(createVnfResponse);
84         } catch (final RestProcessingException | InvalidRestRequestException
85                 | HttpResouceNotFoundException httpInvocationException) {
86             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
87             return Optional.empty();
88         }
89
90     }
91
92     @Override
93     public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
94         try {
95             final String url = urlProvider.getJobStatusUrl(jobId);
96
97             final ResponseEntity<QueryJobResponse> response =
98                     httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
99
100             final HttpStatus httpStatus = response.getStatusCode();
101
102             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
103                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
104                 return Optional.empty();
105             }
106
107             if (!response.hasBody()) {
108                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
109                 return Optional.empty();
110             }
111             return Optional.of(response.getBody());
112         } catch (final RestProcessingException | InvalidRestRequestException | HttpResouceNotFoundException exception) {
113             LOGGER.error("Unexpected error while processing job request", exception);
114             throw exception;
115         }
116     }
117 }