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