ac2e0bf17a77067718a7767f132e64154030bf91
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / VnfmAdapterServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.bpmn.infrastructure.adapter.vnfm.tasks;
22
23 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
24 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
25 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
27 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
28 import org.onap.so.rest.exceptions.InvalidRestRequestException;
29 import org.onap.so.rest.exceptions.RestProcessingException;
30 import org.onap.so.rest.service.HttpRestServiceProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37 import com.google.common.base.Optional;
38
39 /**
40  * @author waqas.ikram@est.tech
41  */
42 @Service
43 public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvider {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterServiceProviderImpl.class);
46     public static final String RECEIVED_RESPONSE_WITHOUT_BODY = "Received response without body: {}";
47
48     private final VnfmAdapterUrlProvider urlProvider;
49     private final HttpRestServiceProvider httpServiceProvider;
50
51     @Autowired
52     public VnfmAdapterServiceProviderImpl(final VnfmAdapterUrlProvider urlProvider,
53             final HttpRestServiceProvider httpServiceProvider) {
54         this.urlProvider = urlProvider;
55         this.httpServiceProvider = httpServiceProvider;
56     }
57
58     @Override
59     public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
60             final CreateVnfRequest request) {
61         try {
62             final String url = urlProvider.getCreateInstantiateUrl(vnfId);
63
64             final ResponseEntity<CreateVnfResponse> response =
65                     httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
66
67             final HttpStatus httpStatus = response.getStatusCode();
68             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
69                 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
70                 return Optional.absent();
71             }
72
73             if (!response.hasBody()) {
74                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
75                 return Optional.absent();
76             }
77
78             final CreateVnfResponse createVnfResponse = response.getBody();
79
80             if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
81                 LOGGER.error("Received invalid instantiation response: {}", response);
82                 return Optional.absent();
83             }
84
85             return Optional.of(createVnfResponse);
86         } catch (final RestProcessingException | InvalidRestRequestException
87                 | HttpResouceNotFoundException httpInvocationException) {
88             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
89             return Optional.absent();
90         }
91
92     }
93
94     @Override
95     public Optional<DeleteVnfResponse> invokeDeleteRequest(final String vnfId) {
96         try {
97             final String url = urlProvider.getDeleteUrl(vnfId);
98             LOGGER.debug("Will send request to vnfm adapter using url: {}", url);
99
100             final ResponseEntity<DeleteVnfResponse> response =
101                     httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
102
103             LOGGER.debug("Response received: ", response);
104
105             final HttpStatus httpStatus = response.getStatusCode();
106
107             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
108                 LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
109                 return Optional.absent();
110             }
111
112             if (!response.hasBody()) {
113                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
114                 return Optional.absent();
115             }
116             final DeleteVnfResponse deleteVnfResponse = response.getBody();
117
118             if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
119                 LOGGER.error("Received invalid delete response: {}", response);
120                 return Optional.absent();
121             }
122             return Optional.of(deleteVnfResponse);
123         } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
124             LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
125             return Optional.absent();
126         }
127     }
128
129     @Override
130     public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
131         try {
132             final String url = urlProvider.getJobStatusUrl(jobId);
133
134             final ResponseEntity<QueryJobResponse> response =
135                     httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
136
137             final HttpStatus httpStatus = response.getStatusCode();
138
139             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
140                 LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
141                 return Optional.absent();
142             }
143
144             if (!response.hasBody()) {
145                 LOGGER.error(RECEIVED_RESPONSE_WITHOUT_BODY, response);
146                 return Optional.absent();
147             }
148             return Optional.of(response.getBody());
149         } catch (final RestProcessingException | InvalidRestRequestException
150                 | HttpResouceNotFoundException httpInvocationException) {
151             LOGGER.error("Unexpected error while processing job request", httpInvocationException);
152             throw httpInvocationException;
153         }
154     }
155 }