Varibles Name Changed
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / workflow / SOTaskManager.java
1 /**\r
2  * Copyright (c) 2018 Orange\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with\r
5  * the License. You may obtain a copy of the License at\r
6  *\r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  *\r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on\r
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the\r
11  * specific language governing permissions and limitations under the License.\r
12  */\r
13 package org.onap.nbi.apis.serviceorder.workflow;\r
14 \r
15 import java.util.*;\r
16 import java.util.Map.Entry;\r
17 import org.onap.nbi.apis.serviceorder.model.OrderItemRelationship;\r
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;\r
19 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;\r
20 import org.onap.nbi.apis.serviceorder.model.orchestrator.ExecutionTask;\r
21 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;\r
22 import org.onap.nbi.apis.serviceorder.repositories.ExecutionTaskRepository;\r
23 import org.onap.nbi.apis.serviceorder.utils.JsonEntityConverter;\r
24 import org.slf4j.Logger;\r
25 import org.slf4j.LoggerFactory;\r
26 import org.springframework.beans.factory.annotation.Autowired;\r
27 import org.springframework.scheduling.annotation.EnableScheduling;\r
28 import org.springframework.scheduling.annotation.Scheduled;\r
29 import org.springframework.stereotype.Service;\r
30 \r
31 @Service\r
32 @EnableScheduling\r
33 public class SOTaskManager {\r
34 \r
35     @Autowired\r
36     private ExecutionTaskRepository executionTaskRepository;\r
37 \r
38     @Autowired\r
39     private SOTaskProcessor soTaskProcessor;\r
40 \r
41     private static final Logger LOGGER = LoggerFactory.getLogger(SOTaskManager.class);\r
42 \r
43     /**\r
44      * @param orderItems\r
45      * @param serviceOrderInfoJson\r
46      */\r
47     private void registerOrderItemExecutionPlan(List<ServiceOrderItem> orderItems,\r
48         String serviceOrderInfoJson) {\r
49         List<ExecutionTask> executionTasksSaved = new ArrayList<>();\r
50         Map<String, Long> internalIdOrderItemsMap = new HashMap<>();\r
51         if (orderItems != null) {\r
52             // first we save create all the execution tasks with order item id in relied tasks\r
53             for (ServiceOrderItem orderItem : orderItems) {\r
54                 ExecutionTask task = new ExecutionTask();\r
55                 task.setOrderItemId(orderItem.getId());\r
56                 task.setCreateDate(new Date());\r
57                 StringBuilder sb = new StringBuilder();\r
58                 for (OrderItemRelationship orderItemRelationship : orderItem.getOrderItemRelationship()) {\r
59                     sb.append(orderItemRelationship.getId()).append(" ");\r
60                 }\r
61                 task.setReliedTasks(sb.toString());\r
62                 task.setServiceOrderInfoJson(serviceOrderInfoJson);\r
63                 ExecutionTask savedTask = executionTaskRepository.save(task);\r
64                 executionTasksSaved.add(savedTask);\r
65 \r
66                 internalIdOrderItemsMap.put(savedTask.getOrderItemId(), savedTask.getInternalId());\r
67             }\r
68             // then we replace all orderitem ids in reliedtasks field with internalid of the tasks\r
69             for (ExecutionTask executionTask : executionTasksSaved) {\r
70                 List<String> reliedOrderItemsIds = new ArrayList<String>(Arrays.asList(executionTask.getReliedTasks().split(" ")));\r
71                 List<String> reliedTasksInternalIds = new ArrayList<String>();\r
72                 for (Entry<String, Long> entry : internalIdOrderItemsMap.entrySet()){\r
73                    if(reliedOrderItemsIds.contains(entry.getKey())) {\r
74                        reliedTasksInternalIds.add(entry.getValue().toString());\r
75                     }\r
76                 }\r
77             String reliedTasksString = String.join(" ", reliedTasksInternalIds);\r
78             executionTask.setReliedTasks(reliedTasksString);\r
79                 if(LOGGER.isDebugEnabled()) {\r
80                     LOGGER.debug("saving task with id {} , orderItemId {} , reliedtasks {}", executionTask.getInternalId(), executionTask.getOrderItemId(), executionTask.getReliedTasks());\r
81                 }\r
82                 executionTaskRepository.save(executionTask);\r
83             }\r
84         }\r
85     }\r
86 \r
87     /**\r
88      *\r
89      * @param serviceOrder\r
90      * @param serviceOrderInfo\r
91      */\r
92     public void registerServiceOrder(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {\r
93         String serviceOrderInfoJson = JsonEntityConverter.convertServiceOrderInfoToJson(serviceOrderInfo);\r
94         registerOrderItemExecutionPlan(serviceOrder.getOrderItem(), serviceOrderInfoJson);\r
95     }\r
96 \r
97     // Using fixedDelay to mitigate against Scheduler queue backlog with fixedRate \r
98     @Scheduled(fixedDelay = 2000)\r
99     private void processExecutionPlan() throws InterruptedException {\r
100         List<ExecutionTask> taskToExecute = executionTaskRepository.findByReliedTasksIsEmpty();\r
101         for (ExecutionTask executionTask : taskToExecute) {\r
102             soTaskProcessor.processOrderItem(executionTask);\r
103         }\r
104     }\r
105 }\r