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