Added instatntiation job monitoring
[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
46   private final VnfmAdapterUrlProvider urlProvider;
47   private final HttpRestServiceProvider httpServiceProvider;
48
49   @Autowired
50   public VnfmAdapterServiceProviderImpl(final VnfmAdapterUrlProvider urlProvider,
51       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.absent();
69       }
70
71       if (!response.hasBody()) {
72         LOGGER.error("Received response without body: {}", response);
73         return Optional.absent();
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.absent();
81       }
82
83       return Optional.of(createVnfResponse);
84     } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
85       LOGGER.error("Unexpected error while processing create and instantiation request", httpInvocationException);
86       return Optional.absent();
87     }
88
89   }
90
91   @Override
92   public Optional<DeleteVnfResponse> invokeDeleteRequest(final String vnfId) {
93     try {
94       final String url = urlProvider.getDeleteUrl(vnfId);
95       LOGGER.debug("Will send request to vnfm adapter using url: {}", url);
96
97       final ResponseEntity<DeleteVnfResponse> response =
98           httpServiceProvider.deleteHttpRequest(url, DeleteVnfResponse.class);
99
100       LOGGER.debug("Response received: ", response);
101
102       final HttpStatus httpStatus = response.getStatusCode();
103
104       if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
105         LOGGER.error("Unable to invoke HTTP DELETE using URL: {}, Response Code: {}", url, httpStatus.value());
106         return Optional.absent();
107       }
108
109       if (!response.hasBody()) {
110         LOGGER.error("Received response without body: {}", response);
111         return Optional.absent();
112       }
113       final DeleteVnfResponse deleteVnfResponse = response.getBody();
114
115       if (deleteVnfResponse.getJobId() == null || deleteVnfResponse.getJobId().isEmpty()) {
116         LOGGER.error("Received invalid delete response: {}", response);
117         return Optional.absent();
118       }
119       return Optional.of(deleteVnfResponse);
120     } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
121       LOGGER.error("Unexpected error while processing delete request", httpInvocationException);
122       return Optional.absent();
123     }
124   }
125
126   @Override
127   public Optional<QueryJobResponse> getInstantiateOperationJobStatus(final String jobId) {
128     try {
129       final String url = urlProvider.getJobStatusUrl(jobId);
130
131       final ResponseEntity<QueryJobResponse> response =
132           httpServiceProvider.getHttpResponse(url, QueryJobResponse.class);
133
134       final HttpStatus httpStatus = response.getStatusCode();
135
136       if (!(httpStatus.equals(HttpStatus.ACCEPTED)) && !(httpStatus.equals(HttpStatus.OK))) {
137         LOGGER.error("Unable to invoke HTTP GET using URL: {}, Response Code: ", url, httpStatus.value());
138         return Optional.absent();
139       }
140
141       if (!response.hasBody()) {
142         LOGGER.error("Received response without body: {}", response);
143         return Optional.absent();
144       }
145       return Optional.of(response.getBody());
146     } catch (final RestProcessingException | InvalidRestRequestException httpInvocationException) {
147       LOGGER.error("Unexpected error while processing job request", httpInvocationException);
148       return Optional.absent();
149     }
150   }
151 }