1 package org.openecomp.core.tools.commands;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Paths;
6 import java.util.Collection;
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;
24 public class AddContributorCommand extends Command {
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.";
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());
41 public boolean execute(String[] args) {
42 CommandLine cmd = parseArgs(args);
44 if (!cmd.hasOption(ITEMS_PATH_OPTION) || !cmd.hasOption(UUSERS_PATH_OPTION)) {
45 LOGGER.error("Arguments p and u are mandatory");
49 String itemListPath = cmd.getOptionValue(ITEMS_PATH_OPTION);
50 String userListPath = cmd.getOptionValue(UUSERS_PATH_OPTION);
52 List<String> itemList;
54 itemList = getItemList(itemListPath);
55 } catch (IOException e) {
56 throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + itemListPath, e);
58 List<String> userList;
60 userList = load(userListPath).collect(Collectors.toList());
61 } catch (IOException e) {
62 throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + userListPath, e);
65 List<ItemAddContributorsTask> tasks =
66 itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
68 ExecutorService executor = null;
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);
77 if (executor != null) {
78 executor.shutdownNow();
85 public CommandName getCommandName() {
86 return CommandName.ADD_CONTRIBUTOR;
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());
94 itemList = new ItemHandler().getItemList();
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());
112 private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
113 return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), itemId, users);
116 private static Stream<String> load(String filePath) throws IOException {
117 return Files.lines(Paths.get(filePath));