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