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
5  * in compliance with 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
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.nbi.apis.serviceorder.workflow;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
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.model.orchestrator.ServiceOrderInfoJson;
26 import org.onap.nbi.apis.serviceorder.repositories.ExecutionTaskRepository;
27 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderInfoRepository;
28 import org.onap.nbi.apis.serviceorder.utils.JsonEntityConverter;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.scheduling.annotation.EnableScheduling;
33 import org.springframework.scheduling.annotation.Scheduled;
34 import org.springframework.stereotype.Service;
35
36 @Service
37 @EnableScheduling
38 public class SOTaskManager {
39
40     @Autowired
41     private ExecutionTaskRepository executionTaskRepository;
42
43     @Autowired
44     private ServiceOrderInfoRepository serviceOrderInfoRepository;
45
46     @Autowired
47     private SOTaskProcessor soTaskProcessor;
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(SOTaskManager.class);
50
51     /**
52      * @param orderItems
53      * @param serviceOrderInfoJson
54      */
55     private void registerOrderItemExecutionPlan(List<ServiceOrderItem> orderItems,
56             ServiceOrderInfoJson serviceOrderInfoJson) {
57         List<ExecutionTask> executionTasksSaved = new ArrayList<>();
58         Map<String, Long> internalIdOrderItemsMap = new HashMap<>();
59         if (orderItems != null) {
60             // first we save create all the execution tasks with order item id in relied tasks
61             for (ServiceOrderItem orderItem : orderItems) {
62                 ExecutionTask task = new ExecutionTask();
63                 task.setOrderItemId(orderItem.getId());
64                 task.setNbRetries(3);
65                 StringBuilder sb = new StringBuilder();
66                 for (OrderItemRelationship orderItemRelationship : orderItem.getOrderItemRelationship()) {
67                     sb.append(orderItemRelationship.getId()).append(" ");
68                 }
69                 task.setReliedTasks(sb.toString());
70                 task.setServiceOrderInfoJson(serviceOrderInfoJson);
71                 ExecutionTask savedTask = executionTaskRepository.save(task);
72                 executionTasksSaved.add(savedTask);
73                 internalIdOrderItemsMap.put(savedTask.getOrderItemId(), savedTask.getInternalId());
74             }
75             // then we replace all orderitem ids in reliedtasks field with internalid of the tasks
76             for (ExecutionTask executionTask : executionTasksSaved) {
77                 for (String key : internalIdOrderItemsMap.keySet()) {
78                     String replace = executionTask.getReliedTasks().replace(key,
79                             String.valueOf(internalIdOrderItemsMap.get(key)));
80                     executionTask.setReliedTasks(replace);
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 json = JsonEntityConverter.convertServiceOrderInfoToJson(serviceOrderInfo);
94         ServiceOrderInfoJson serviceOrderInfoJson = new ServiceOrderInfoJson(serviceOrder.getId(), json);
95         serviceOrderInfoRepository.save(serviceOrderInfoJson);
96         registerOrderItemExecutionPlan(serviceOrder.getOrderItem(), serviceOrderInfoJson);
97     }
98
99
100     @Scheduled(fixedRate = 2000)
101     private void processExecutionPlan() throws InterruptedException {
102         List<ExecutionTask> taskToExecute = executionTaskRepository.findByReliedTasksIsEmpty();
103         for (ExecutionTask executionTask : taskToExecute) {
104             soTaskProcessor.processOrderItem(executionTask);
105         }
106     }
107 }