Implement E2EService activation/deactivation for NetworkSlicing
[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.ServiceStateType;
21 import org.onap.nbi.apis.serviceorder.model.StateType;
22 import org.onap.nbi.apis.serviceorder.model.consumer.GetE2ERequestStatusResponse;
23 import org.onap.nbi.apis.serviceorder.model.consumer.GetRequestStatusResponse;
24 import org.onap.nbi.apis.serviceorder.model.consumer.RequestState;
25 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30
31 @Service
32 public class SOGetStatusManager {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(SOGetStatusManager.class);
35
36     @Autowired
37     private ServiceOrderService serviceOrderService;
38
39     @Autowired
40     private SoClient soClient;
41
42     public void pollRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem, boolean e2eService) {
43         if (e2eService) {
44             pollE2ESoRequestStatus(serviceOrder, serviceOrderItem);
45         } else {
46             pollSoRequestStatus(serviceOrder, serviceOrderItem);
47
48         }
49     }
50
51     /**
52      * * @param orderItem
53      */
54     private void pollSoRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem orderItem) {
55         String requestId = orderItem.getRequestId();
56         GetRequestStatusResponse response = null;
57
58         response = soClient.callGetRequestStatus(requestId);
59         if (response != null) {
60             orderItem.setPercentProgress(String.valueOf(response.getRequest().getRequestStatus().getPercentProgress()));
61             if (response.getRequest().getRequestStatus().getPercentProgress() != 100) {
62                 LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
63             } else if (RequestState.COMPLETE != response.getRequest().getRequestStatus().getRequestState()) {
64                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.FAILED);
65                 LOGGER.debug("orderitem id {} failed, response from request status {}", orderItem.getId(),
66                         response.getRequest().getRequestStatus().getRequestState());
67             } else {
68                 boolean e2eService = false;
69                 updateOrderItemIfStatusCompleted(serviceOrder, orderItem, e2eService);
70                 LOGGER.debug("orderitem id {} completed");
71             }
72         } else {
73             LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
74         }
75
76     }
77
78         private void updateOrderItemIfStatusCompleted(ServiceOrder serviceOrder, ServiceOrderItem orderItem,boolean e2eService) {
79         boolean serviceActivationReq = orderItem.getService().getServiceState() == ServiceStateType.ACTIVE || 
80                         orderItem.getService().getServiceState() == ServiceStateType.INACTIVE;
81         if (orderItem.getAction() != ActionType.MODIFY) {
82             serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
83         }else if(orderItem.getAction() == ActionType.MODIFY && serviceActivationReq && e2eService) {
84                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
85         }else {
86             if (StateType.INPROGRESS_MODIFY_REQUEST_CREATE_SEND == orderItem.getState()) {
87                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.COMPLETED);
88             } else {
89                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem,
90                         StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE);
91             }
92         }
93     }
94
95     private void pollE2ESoRequestStatus(ServiceOrder serviceOrder, ServiceOrderItem orderItem) {
96         String operationId = orderItem.getRequestId();
97         String serviceId = orderItem.getService().getId();
98         GetE2ERequestStatusResponse response = null;
99         final String ERROR = "error";
100         final String FINISHED = "finished";
101         final String PROCESSING = "processing";
102         String result = null;
103         response = soClient.callE2EGetRequestStatus(operationId, serviceId);
104         if (response != null) {
105             orderItem.setPercentProgress(response.getOperation().getProgress());
106             result = response.getOperation().getResult();
107             if (PROCESSING.equals(result)) {
108                 LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
109             } else if (ERROR.equals(result)) {
110                 serviceOrderService.updateOrderItemState(serviceOrder, orderItem, StateType.FAILED);
111                 LOGGER.debug("orderitem id {} failed, response from request status {}", orderItem.getId(),
112                         response.getOperation().getResult());
113             } else if (FINISHED.equals(result)) {
114                 boolean e2eService = true;
115                 updateOrderItemIfStatusCompleted(serviceOrder, orderItem,e2eService);
116                 LOGGER.debug("orderitem id {} completed");
117             }
118         } else {
119             LOGGER.debug("orderitem id {} still in progress from so", orderItem.getId());
120         }
121     }
122
123 }