Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / commands / AddContributorCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.tools.commands;
22
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;
43
44 public class AddContributorCommand extends Command {
45
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.";
52
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());
58     }
59
60     @Override
61     public boolean execute(String[] args) {
62         CommandLine cmd = parseArgs(args);
63
64         if (!cmd.hasOption(ITEMS_PATH_OPTION) || !cmd.hasOption(UUSERS_PATH_OPTION)) {
65             LOGGER.error("Arguments p and u are mandatory");
66             return false;
67         }
68
69         String itemListPath = cmd.getOptionValue(ITEMS_PATH_OPTION);
70         String userListPath = cmd.getOptionValue(UUSERS_PATH_OPTION);
71
72         List<String> itemList;
73         try {
74             itemList = getItemList(itemListPath);
75         } catch (IOException e) {
76             throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + itemListPath, e);
77         }
78         List<String> userList;
79         try {
80             userList = load(userListPath).collect(Collectors.toList());
81         } catch (IOException e) {
82             throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + userListPath, e);
83         }
84
85         List<ItemAddContributorsTask> tasks =
86                 itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
87
88         ExecutorService executor = null;
89
90         try {
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);
96         } finally {
97             if (executor != null) {
98                 executor.shutdownNow();
99             }
100         }
101         return true;
102     }
103
104     @Override
105     public CommandName getCommandName() {
106         return CommandName.ADD_CONTRIBUTOR;
107     }
108
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());
113         } else {
114             itemList = new ItemHandler().getItemList();
115         }
116
117         return itemList;
118     }
119
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());
127
128         }
129     }
130
131
132     private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
133         return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), itemId, users);
134     }
135
136     private static Stream<String> load(String filePath) throws IOException {
137         return Files.lines(Paths.get(filePath));
138
139     }
140
141
142 }