ServiceOrder Modification Implementation
[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 package org.onap.nbi.apis.serviceorder.workflow;
14
15 import org.onap.nbi.apis.serviceorder.SoClient;
16 import org.onap.nbi.apis.serviceorder.model.ActionType;
17 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
19 import org.onap.nbi.apis.serviceorder.model.StateType;
20 import org.onap.nbi.apis.serviceorder.model.consumer.GetE2ERequestStatusResponse;
21 import org.onap.nbi.apis.serviceorder.model.consumer.GetRequestStatusResponse;
22 import org.onap.nbi.apis.serviceorder.model.consumer.RequestState;
23 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28
29 @Service
30 public class SOGetStatusManager {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(SOGetStatusManager.class);
33
34     @Autowired
35     private ServiceOrderService serviceOrderService;
36
37     @Autowired
38     private SoClient soClient;
39
40
41
42     public void pollRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem, boolean e2eService)
43         throws InterruptedException {
44         if (e2eService) {
45             pollE2ESoRequestStatus(serviceOrder, serviceOrderItem);
46         } else {
47             pollSoRequestStatus(serviceOrder, serviceOrderItem);
48
49         }
50     }
51
52     /**
53      * * @param orderItem
54      */
55     private void pollSoRequestStatus(ServiceOrder serviceOrder,
56         ServiceOrderItem orderItem) throws InterruptedException {
57         boolean stopPolling = false;
58         String requestId = orderItem.getRequestId();
59         GetRequestStatusResponse response = null;
60         int nbRetries = 0;
61
62         while (!stopPolling) {
63             response = soClient.callGetRequestStatus(requestId);
64             if (response != null) {
65                 if (response.getRequest().getRequestStatus().getPercentProgress() != 100) {
66                     nbRetries++;
67                     Thread.sleep(1000);
68                     LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
69                 } else if (RequestState.COMPLETE != response.getRequest().getRequestStatus().getRequestState()) {
70                     serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.FAILED);
71                     stopPolling = true;
72                     LOGGER.debug("orderitem id {} failed, response from request status {}", orderItem.getId(),
73                         response.getRequest().getRequestStatus().getRequestState());
74                 } else {
75                     updateOrderItemIfStatusCompleted(serviceOrder, orderItem);
76                     stopPolling = true;
77                     LOGGER.debug("orderitem id {} completed");
78                 }
79             } else {
80                 stopPolling = true;
81                 LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
82             }
83             if (nbRetries == 3) {
84                 stopPolling = true;
85                 LOGGER.debug("orderitem id {} stop polling from getrequeststatus, 3 retries done", orderItem.getId());
86
87             }
88         }
89     }
90
91     private void updateOrderItemIfStatusCompleted(ServiceOrder serviceOrder, ServiceOrderItem orderItem) {
92         if(orderItem.getAction()!= ActionType.MODIFY){
93             serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
94         } else {
95             if(StateType.INPROGRESS_MODIFY_REQUEST_CREATE_SEND ==orderItem.getState()){
96                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
97             } else {
98                 serviceOrderService.updateOrderItemState(serviceOrder,orderItem,StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE);
99             }
100         }
101     }
102
103     private void pollE2ESoRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem orderItem)
104         throws InterruptedException {
105         boolean stopPolling = false;
106         String operationId = orderItem.getRequestId();
107         String serviceId = orderItem.getService().getId();
108         int nbRetries = 0;
109         GetE2ERequestStatusResponse response = null;
110         final String ERROR = "error";
111         final String FINISHED = "finished";
112         final String PROCESSING = "processing";
113         String result = null;
114         while (!stopPolling) {
115             response = soClient.callE2EGetRequestStatus(operationId, serviceId);
116             if (response != null) {
117                 result = response.getOperation().getResult();
118                 if (PROCESSING.equals(result)) {
119                     nbRetries++;
120                     Thread.sleep(1000);
121                     LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
122                 } else if (ERROR.equals(result)) {
123                     serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.FAILED);
124                     stopPolling = true;
125                     LOGGER.debug("orderitem id {} failed, response from request status {}", orderItem.getId(),
126                         response.getOperation().getResult());
127                 } else if (FINISHED.equals(result)) {
128                     updateOrderItemIfStatusCompleted(serviceOrder, orderItem);
129                     stopPolling = true;
130                     LOGGER.debug("orderitem id {} completed");
131                 }
132             } else {
133                 stopPolling = true;
134                 LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
135             }
136             if (nbRetries == 3) {
137                 stopPolling = true;
138                 LOGGER.debug("orderitem id {} stop polling from getrequeststatus, 3 retries done", orderItem.getId());
139
140             }
141         }
142     }
143
144
145
146 }