Merge "Add WorkflowSpecification Beans for APIH"
[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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34
35 import com.google.common.base.Optional;
36
37 /**
38  * @author waqas.ikram@est.tech
39  */
40 @Service
41 public class VnfmAdapterServiceProviderImpl implements VnfmAdapterServiceProvider {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterServiceProviderImpl.class);
44
45     private final VnfmAdapterUrlProvider urlProvider;
46     private final HttpRestServiceProvider httpServiceProvider;
47
48     @Autowired
49     public VnfmAdapterServiceProviderImpl(final VnfmAdapterUrlProvider urlProvider,
50             final HttpRestServiceProvider httpServiceProvider) {
51         this.urlProvider = urlProvider;
52         this.httpServiceProvider = httpServiceProvider;
53     }
54
55     @Override
56     public Optional<CreateVnfResponse> invokeCreateInstantiationRequest(final String vnfId,
57             final CreateVnfRequest request) {
58         try {
59             final String url = urlProvider.getCreateInstantiateUrl(vnfId);
60
61             final ResponseEntity<CreateVnfResponse> response =
62                     httpServiceProvider.postHttpRequest(request, url, CreateVnfResponse.class);
63
64             final HttpStatus httpStatus = response.getStatusCode();
65             if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
66                 LOGGER.error("Unable to invoke HTTP POST using URL: {}, Response Code: {}", url, httpStatus.value());
67                 return Optional.absent();
68             }
69
70             if (!response.hasBody()) {
71                 LOGGER.error("Received response without body: {}", response);
72                 return Optional.absent();
73             }
74
75             final CreateVnfResponse createVnfResponse = response.getBody();
76
77             if (createVnfResponse.getJobId() == null || createVnfResponse.getJobId().isEmpty()) {
78                 LOGGER.error("Received invalid instantiation response: {}", response);
79                 return Optional.absent();
80             }
81
82             return Optional.of(createVnfResponse);
83         } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
84             LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
85             return Optional.absent();
86         }
87
88     }
89 }