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