2c13ab77494e0eba9f73e49168988565e9eac5ff
[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             throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
75         } finally {
76             if (executor != null) {
77                 executor.shutdownNow();
78             }
79         }
80         return true;
81     }
82
83     @Override
84     public CommandName getCommandName() {
85         return CommandName.ADD_CONTRIBUTOR;
86     }
87
88     private static List<String> getItemList(String itemListPath) throws IOException {
89         List<String> itemList;
90         if (itemListPath != null) {
91             itemList = load(itemListPath).collect(Collectors.toList());
92         } else {
93             itemList = new ItemHandler().getItemList();
94         }
95
96         return itemList;
97     }
98
99     private static void executeAllTasks(ExecutorService executor, Collection<? extends Callable<String>> tasks)
100             throws InterruptedException {
101         List<Future<String>> futureTasks;
102         futureTasks = executor.invokeAll(tasks);
103         boolean isThreadOpen = true;
104         while (isThreadOpen) {
105             isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
106
107         }
108     }
109
110
111     private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
112         return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), itemId, users);
113     }
114
115     private static Stream<String> load(String filePath) throws IOException {
116         return Files.lines(Paths.get(filePath));
117
118     }
119
120
121 }