061ecbfaf104ca94ac44a4e53a88c8bddc8c21ca
[sdc.git] /
1 package org.openecomp.core.tools.commands;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Paths;
6 import java.util.Collection;
7 import java.util.List;
8 import java.util.concurrent.Callable;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11 import java.util.concurrent.Future;
12 import java.util.stream.Collectors;
13 import java.util.stream.Stream;
14 import org.apache.commons.cli.CommandLine;
15 import org.apache.commons.cli.Option;
16 import org.openecomp.core.tools.concurrent.ItemAddContributorsTask;
17 import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException;
18 import org.openecomp.core.tools.store.ItemHandler;
19 import org.openecomp.core.tools.store.NotificationHandler;
20 import org.openecomp.core.tools.store.PermissionHandler;
21 import org.openecomp.sdc.logging.api.Logger;
22 import org.openecomp.sdc.logging.api.LoggerFactory;
23
24 public class AddContributorCommand extends Command {
25
26     private static final Logger LOGGER = LoggerFactory.getLogger(AddContributorCommand.class);
27     private static final String ITEMS_PATH_OPTION = "p";
28     private static final String UUSERS_PATH_OPTION = "u";
29     private static final int DEFAULT_THREAD_NUMBER = 8;
30     private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list";
31     private static final String COMMAND_ADD_CONTRIBUTOR_FAILED = "Command AddContributor execution failed.";
32
33     AddContributorCommand() {
34         options.addOption(Option.builder(ITEMS_PATH_OPTION).hasArg().argName("file")
35                                 .desc("file containing list of item ids, mandatory").build());
36         options.addOption(Option.builder(UUSERS_PATH_OPTION).hasArg().argName("file")
37                                 .desc("file containing list of users, mandatory").build());
38     }
39
40     @Override
41     public boolean execute(String[] args) {
42         CommandLine cmd = parseArgs(args);
43
44         if (!cmd.hasOption(ITEMS_PATH_OPTION) || !cmd.hasOption(UUSERS_PATH_OPTION)) {
45             LOGGER.error("Arguments p and u are mandatory");
46             return false;
47         }
48
49         String itemListPath = cmd.getOptionValue(ITEMS_PATH_OPTION);
50         String userListPath = cmd.getOptionValue(UUSERS_PATH_OPTION);
51
52         List<String> itemList;
53         try {
54             itemList = getItemList(itemListPath);
55         } catch (IOException e) {
56             throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + itemListPath, e);
57         }
58         List<String> userList;
59         try {
60             userList = load(userListPath).collect(Collectors.toList());
61         } catch (IOException e) {
62             throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + userListPath, e);
63         }
64
65         List<ItemAddContributorsTask> tasks =
66                 itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
67
68         ExecutorService executor = null;
69
70         try {
71             executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER);
72             executeAllTasks(executor, tasks);
73         } catch (InterruptedException e) {
74             Thread.currentThread().interrupt();
75             throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
76         } finally {
77             if (executor != null) {
78                 executor.shutdownNow();
79             }
80         }
81         return true;
82     }
83
84     @Override
85     public CommandName getCommandName() {
86         return CommandName.ADD_CONTRIBUTOR;
87     }
88
89     private static List<String> getItemList(String itemListPath) throws IOException {
90         List<String> itemList;
91         if (itemListPath != null) {
92             itemList = load(itemListPath).collect(Collectors.toList());
93         } else {
94             itemList = new ItemHandler().getItemList();
95         }
96
97         return itemList;
98     }
99
100     private static void executeAllTasks(ExecutorService executor, Collection<? extends Callable<String>> tasks)
101             throws InterruptedException {
102         List<Future<String>> futureTasks;
103         futureTasks = executor.invokeAll(tasks);
104         boolean isThreadOpen = true;
105         while (isThreadOpen) {
106             isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
107
108         }
109     }
110
111
112     private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
113         return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), itemId, users);
114     }
115
116     private static Stream<String> load(String filePath) throws IOException {
117         return Files.lines(Paths.get(filePath));
118
119     }
120
121
122 }