Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / workflow / SOGetStatusManager.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11  * specific language governing permissions and limitations under the License.
12  */
13
14 package org.onap.nbi.apis.serviceorder.workflow;
15
16 import org.onap.nbi.apis.serviceorder.SoClient;
17 import org.onap.nbi.apis.serviceorder.model.ActionType;
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
19 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
20 import org.onap.nbi.apis.serviceorder.model.StateType;
21 import org.onap.nbi.apis.serviceorder.model.consumer.GetE2ERequestStatusResponse;
22 import org.onap.nbi.apis.serviceorder.model.consumer.GetRequestStatusResponse;
23 import org.onap.nbi.apis.serviceorder.model.consumer.RequestState;
24 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service;
29
30 @Service
31 public class SOGetStatusManager {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(SOGetStatusManager.class);
34
35     @Autowired
36     private ServiceOrderService serviceOrderService;
37
38     @Autowired
39     private SoClient soClient;
40
41     public void pollRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem, boolean e2eService) {
42         if (e2eService) {
43             pollE2ESoRequestStatus(serviceOrder, serviceOrderItem);
44         } else {
45             pollSoRequestStatus(serviceOrder, serviceOrderItem);
46
47         }
48     }
49
50     /**
51      * * @param orderItem
52      */
53     private void pollSoRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem orderItem) {
54         String requestId = orderItem.getRequestId();
55         GetRequestStatusResponse response = null;
56
57         response = soClient.callGetRequestStatus(requestId);
58         if (response != null) {
59             orderItem.setPercentProgress(String.valueOf(response.getRequest().getRequestStatus().getPercentProgress()));
60             if (response.getRequest().getRequestStatus().getPercentProgress() != 100) {
61                 LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
62             } else if (RequestState.COMPLETE != response.getRequest().getRequestStatus().getRequestState()) {
63                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.FAILED);
64                 LOGGER.debug("orderitem id {} failed, response from request status {}", orderItem.getId(),
65                         response.getRequest().getRequestStatus().getRequestState());
66             } else {
67                 updateOrderItemIfStatusCompleted(serviceOrder, orderItem);
68                 LOGGER.debug("orderitem id {} completed");
69             }
70         } else {
71             LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
72         }
73
74     }
75
76     private void updateOrderItemIfStatusCompleted(ServiceOrder serviceOrder, ServiceOrderItem orderItem) {
77         if (orderItem.getAction() != ActionType.MODIFY) {
78             serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
79         } else {
80             if (StateType.INPROGRESS_MODIFY_REQUEST_CREATE_SEND == orderItem.getState()) {
81                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
82             } else {
83                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem,
84                         StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE);
85             }
86         }
87     }
88
89     private void pollE2ESoRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem orderItem) {
90         String operationId = orderItem.getRequestId();
91         String serviceId = orderItem.getService().getId();
92         GetE2ERequestStatusResponse response = null;
93         final String ERROR = "error";
94         final String FINISHED = "finished";
95         final String PROCESSING = "processing";
96         String result = null;
97         response = soClient.callE2EGetRequestStatus(operationId, serviceId);
98         if (response != null) {
99             orderItem.setPercentProgress(response.getOperation().getProgress());
100             result = response.getOperation().getResult();
101             if (PROCESSING.equals(result)) {
102                 LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
103             } else if (ERROR.equals(result)) {
104                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.FAILED);
105                 LOGGER.debug("orderitem id {} failed, response from request status {}", orderItem.getId(),
106                         response.getOperation().getResult());
107             } else if (FINISHED.equals(result)) {
108                 updateOrderItemIfStatusCompleted(serviceOrder, orderItem);
109                 LOGGER.debug("orderitem id {} completed");
110             }
111         } else {
112             LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
113         }
114     }
115
116 }