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