caa688d0075f84243c9016db56017216ff1fd5e1
[sdc.git] /
1 package org.openecomp.core.tools.commands;
2
3 import org.openecomp.core.tools.concurrent.ItemAddContributorsTask;
4 import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException;
5 import org.openecomp.core.tools.store.ItemHandler;
6 import org.openecomp.core.tools.store.NotificationHandler;
7 import org.openecomp.core.tools.store.PermissionHandler;
8
9 import java.io.IOException;
10 import java.nio.file.Files;
11 import java.nio.file.Paths;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.Future;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 public class AddContributorCommand {
22
23
24   private static final int DEFAULT_THREAD_NUMBER = 8;
25   private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list";
26   private static final String COMMAND_ADD_CONTRIBUTOR_FAILED =
27       "Command AddContributor execution failed.";
28
29   private AddContributorCommand() {
30     // it's a utility class, prevent instantiation
31   }
32
33   public static void add(String itemListPath, String userListPath) {
34
35     List<String> itemList;
36     try {
37       itemList = getItemList(itemListPath);
38     } catch (IOException e) {
39       throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE +
40           "from:" + itemListPath, e);
41     }
42     List<String> userList;
43     try {
44       userList = load(userListPath).collect(Collectors.toList());
45     } catch (IOException e) {
46       throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE +
47           "from:" + userListPath, e);
48     }
49
50     List<ItemAddContributorsTask> tasks =
51         itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
52
53     ExecutorService executor = null;
54
55     try {
56       executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER);
57       executeAllTasks(executor, tasks);
58     } catch (InterruptedException e) {
59       throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
60     } finally {
61       if (executor != null) {
62         executor.shutdownNow();
63       }
64     }
65   }
66
67   private static List<String> getItemList(String itemListPath) throws IOException {
68     List<String> itemList;
69     if (itemListPath != null) {
70       itemList = load(itemListPath).collect(Collectors.toList());
71     } else {
72       itemList = new ItemHandler().getItemList();
73     }
74
75     return itemList;
76   }
77
78   private static void executeAllTasks(ExecutorService executor,
79                                       Collection<? extends Callable<String>> tasks)
80       throws InterruptedException {
81     List<Future<String>> futureTasks;
82     futureTasks = executor.invokeAll(tasks);
83     boolean isThreadOpen = true;
84     while (isThreadOpen) {
85       isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
86
87     }
88   }
89
90
91
92   private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
93     return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(),
94         itemId, users);
95   }
96
97   private static Stream<String> load(String filePath)
98       throws IOException {
99     return Files.lines(Paths.get(filePath));
100
101   }
102
103
104 }