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 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;
35 public class SOTaskManager {
38 private ExecutionTaskRepository executionTaskRepository;
41 private SOTaskProcessor soTaskProcessor;
43 private static final Logger LOGGER = LoggerFactory.getLogger(SOTaskManager.class);
47 * @param serviceOrderInfoJson
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());
59 StringBuilder sb = new StringBuilder();
60 for (OrderItemRelationship orderItemRelationship : orderItem.getOrderItemRelationship()) {
61 sb.append(orderItemRelationship.getId()).append(" ");
63 task.setReliedTasks(sb.toString());
64 task.setServiceOrderInfoJson(serviceOrderInfoJson);
65 ExecutionTask savedTask = executionTaskRepository.save(task);
66 executionTasksSaved.add(savedTask);
68 internalIdOrderItemsMap.put(savedTask.getOrderItemId(), savedTask.getInternalId());
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);
77 LOGGER.debug("saving task with id {} , orderItemId {} , reliedtasks ", executionTask.getInternalId(),
78 executionTask.getOrderItemId(), executionTask.getReliedTasks());
79 executionTaskRepository.save(executionTask);
87 * @param serviceOrderInfo
89 public void registerServiceOrder(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {
90 String serviceOrderInfoJson = JsonEntityConverter.convertServiceOrderInfoToJson(serviceOrderInfo);
91 registerOrderItemExecutionPlan(serviceOrder.getOrderItem(), serviceOrderInfoJson);
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);