EXTAPI-139 : use fixedDelay
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / workflow / SOTaskManager.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 java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import org.onap.nbi.apis.serviceorder.model.OrderItemRelationship;
21 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
22 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
23 import org.onap.nbi.apis.serviceorder.model.orchestrator.ExecutionTask;
24 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
25 import org.onap.nbi.apis.serviceorder.repositories.ExecutionTaskRepository;
26 import org.onap.nbi.apis.serviceorder.utils.JsonEntityConverter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.scheduling.annotation.EnableScheduling;
31 import org.springframework.scheduling.annotation.Scheduled;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 @EnableScheduling
36 public class SOTaskManager {
37
38     @Autowired
39     private ExecutionTaskRepository executionTaskRepository;
40
41     @Autowired
42     private SOTaskProcessor soTaskProcessor;
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(SOTaskManager.class);
45
46     /**
47      * @param orderItems
48      * @param serviceOrderInfoJson
49      */
50     private void registerOrderItemExecutionPlan(List<ServiceOrderItem> orderItems,
51         String serviceOrderInfoJson) {
52         List<ExecutionTask> executionTasksSaved = new ArrayList<>();
53         Map<String, Long> internalIdOrderItemsMap = new HashMap<>();
54         if (orderItems != null) {
55             // first we save create all the execution tasks with order item id in relied tasks
56             for (ServiceOrderItem orderItem : orderItems) {
57                 ExecutionTask task = new ExecutionTask();
58                 task.setOrderItemId(orderItem.getId());
59                 task.setNbRetries(3);
60                 StringBuilder sb = new StringBuilder();
61                 for (OrderItemRelationship orderItemRelationship : orderItem.getOrderItemRelationship()) {
62                     sb.append(orderItemRelationship.getId()).append(" ");
63                 }
64                 task.setReliedTasks(sb.toString());
65                 task.setServiceOrderInfoJson(serviceOrderInfoJson);
66                 ExecutionTask savedTask = executionTaskRepository.save(task);
67                 executionTasksSaved.add(savedTask);
68
69                 internalIdOrderItemsMap.put(savedTask.getOrderItemId(), savedTask.getInternalId());
70             }
71             // then we replace all orderitem ids in reliedtasks field with internalid of the tasks
72             for (ExecutionTask executionTask : executionTasksSaved) {
73                 for (Entry<String, Long> entry : internalIdOrderItemsMap.entrySet()) {
74                     String replace = executionTask.getReliedTasks().replace(entry.getKey(),
75                         String.valueOf(entry.getValue()));
76                     executionTask.setReliedTasks(replace);
77                 }
78                 if(LOGGER.isDebugEnabled()) {
79                     LOGGER.debug("saving task with id {} , orderItemId {} , reliedtasks {}", executionTask.getInternalId(),
80                         executionTask.getOrderItemId(), executionTask.getReliedTasks());
81                 }
82                 executionTaskRepository.save(executionTask);
83             }
84         }
85     }
86
87     /**
88      *
89      * @param serviceOrder
90      * @param serviceOrderInfo
91      */
92     public void registerServiceOrder(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {
93         String serviceOrderInfoJson = JsonEntityConverter.convertServiceOrderInfoToJson(serviceOrderInfo);
94         registerOrderItemExecutionPlan(serviceOrder.getOrderItem(), serviceOrderInfoJson);
95     }
96
97     // Using fixedDelay to mitigate against Scheduler queue backlog with fixedRate 
98     @Scheduled(fixedDelay = 2000)
99     private void processExecutionPlan() throws InterruptedException {
100         List<ExecutionTask> taskToExecute = executionTaskRepository.findByReliedTasksIsEmpty();
101         for (ExecutionTask executionTask : taskToExecute) {
102             soTaskProcessor.processOrderItem(executionTask);
103         }
104     }
105 }