2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.tools.commands;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.concurrent.Callable;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.Future;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import org.apache.commons.cli.CommandLine;
35 import org.apache.commons.cli.Option;
36 import org.openecomp.core.tools.concurrent.ItemAddContributorsTask;
37 import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException;
38 import org.openecomp.core.tools.store.ItemHandler;
39 import org.openecomp.core.tools.store.NotificationHandler;
40 import org.openecomp.core.tools.store.PermissionHandler;
41 import org.openecomp.sdc.logging.api.Logger;
42 import org.openecomp.sdc.logging.api.LoggerFactory;
44 public class AddContributorCommand extends Command {
46 private static final Logger LOGGER = LoggerFactory.getLogger(AddContributorCommand.class);
47 private static final String ITEMS_PATH_OPTION = "p";
48 private static final String UUSERS_PATH_OPTION = "u";
49 private static final int DEFAULT_THREAD_NUMBER = 8;
50 private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list";
51 private static final String COMMAND_ADD_CONTRIBUTOR_FAILED = "Command AddContributor execution failed.";
53 AddContributorCommand() {
54 options.addOption(Option.builder(ITEMS_PATH_OPTION).hasArg().argName("file")
55 .desc("file containing list of item ids, mandatory").build());
56 options.addOption(Option.builder(UUSERS_PATH_OPTION).hasArg().argName("file")
57 .desc("file containing list of users, mandatory").build());
61 public boolean execute(String[] args) {
62 CommandLine cmd = parseArgs(args);
64 if (!cmd.hasOption(ITEMS_PATH_OPTION) || !cmd.hasOption(UUSERS_PATH_OPTION)) {
65 LOGGER.error("Arguments p and u are mandatory");
69 String itemListPath = cmd.getOptionValue(ITEMS_PATH_OPTION);
70 String userListPath = cmd.getOptionValue(UUSERS_PATH_OPTION);
72 List<String> itemList;
74 itemList = getItemList(itemListPath);
75 } catch (IOException e) {
76 throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + itemListPath, e);
78 List<String> userList;
80 userList = load(userListPath).collect(Collectors.toList());
81 } catch (IOException e) {
82 throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + userListPath, e);
85 List<ItemAddContributorsTask> tasks =
86 itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
88 ExecutorService executor = null;
91 executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER);
92 executeAllTasks(executor, tasks);
93 } catch (InterruptedException e) {
94 Thread.currentThread().interrupt();
95 throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
97 if (executor != null) {
98 executor.shutdownNow();
105 public CommandName getCommandName() {
106 return CommandName.ADD_CONTRIBUTOR;
109 private static List<String> getItemList(String itemListPath) throws IOException {
110 List<String> itemList;
111 if (itemListPath != null) {
112 itemList = load(itemListPath).collect(Collectors.toList());
114 itemList = new ItemHandler().getItemList();
120 private static void executeAllTasks(ExecutorService executor, Collection<? extends Callable<String>> tasks)
121 throws InterruptedException {
122 List<Future<String>> futureTasks;
123 futureTasks = executor.invokeAll(tasks);
124 boolean isThreadOpen = true;
125 while (isThreadOpen) {
126 isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
132 private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
133 return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), itemId, users);
136 private static Stream<String> load(String filePath) throws IOException {
137 return Files.lines(Paths.get(filePath));