2 * Copyright (c) 2018 Orange
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
13 package org.onap.nbi.apis.serviceorder.workflow;
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.List;
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;
37 public class SOTaskManager {
40 private ExecutionTaskRepository executionTaskRepository;
43 private SOTaskProcessor soTaskProcessor;
45 private static final Logger LOGGER = LoggerFactory.getLogger(SOTaskManager.class);
49 * @param serviceOrderInfoJson
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(" ");
65 task.setReliedTasks(sb.toString());
66 task.setServiceOrderInfoJson(serviceOrderInfoJson);
67 ExecutionTask savedTask = executionTaskRepository.save(task);
68 executionTasksSaved.add(savedTask);
70 internalIdOrderItemsMap.put(savedTask.getOrderItemId(), savedTask.getInternalId());
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);
79 if(LOGGER.isDebugEnabled()) {
80 LOGGER.debug("saving task with id {} , orderItemId {} , reliedtasks {}", executionTask.getInternalId(),
81 executionTask.getOrderItemId(), executionTask.getReliedTasks());
83 executionTaskRepository.save(executionTask);
91 * @param serviceOrderInfo
93 public void registerServiceOrder(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {
94 String serviceOrderInfoJson = JsonEntityConverter.convertServiceOrderInfoToJson(serviceOrderInfo);
95 registerOrderItemExecutionPlan(serviceOrder.getOrderItem(), serviceOrderInfoJson);
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);