summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main
diff options
context:
space:
mode:
authortalig <talig@amdocs.com>2018-07-08 13:28:37 +0300
committertalig <talig@amdocs.com>2018-07-08 13:29:31 +0300
commitc108dee7bd25a976dc4fa88eef3ec859facdf5d2 (patch)
tree81a91a0153e0916efdd822e5e03ec5fadf5ba668 /openecomp-be/tools/zusammen-tools/src/main
parent68945ddb6e7c376ffb5696dddffbb99b33e6dbcc (diff)
Refactor zusammen main tool
Use command pattern instead of switch case Change-Id: I1457179f90b37b4a8cede5af23a4ebc1f30c1cce Issue-ID: SDC-1444 Signed-off-by: talig <talig@amdocs.com>
Diffstat (limited to 'openecomp-be/tools/zusammen-tools/src/main')
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java167
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/Command.java44
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandName.java10
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandsHolder.java80
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/HealAll.java185
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/PopulateUserPermissions.java28
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/SetHealingFlag.java41
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportDataCommand.java80
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportDataCommand.java45
-rw-r--r--openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java118
10 files changed, 485 insertions, 313 deletions
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java
index caa688d007..2c13ab7749 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java
@@ -1,11 +1,5 @@
package org.openecomp.core.tools.commands;
-import org.openecomp.core.tools.concurrent.ItemAddContributorsTask;
-import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException;
-import org.openecomp.core.tools.store.ItemHandler;
-import org.openecomp.core.tools.store.NotificationHandler;
-import org.openecomp.core.tools.store.PermissionHandler;
-
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
@@ -17,88 +11,111 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-
-public class AddContributorCommand {
-
-
- private static final int DEFAULT_THREAD_NUMBER = 8;
- private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list";
- private static final String COMMAND_ADD_CONTRIBUTOR_FAILED =
- "Command AddContributor execution failed.";
-
- private AddContributorCommand() {
- // it's a utility class, prevent instantiation
- }
-
- public static void add(String itemListPath, String userListPath) {
-
- List<String> itemList;
- try {
- itemList = getItemList(itemListPath);
- } catch (IOException e) {
- throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE +
- "from:" + itemListPath, e);
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.openecomp.core.tools.concurrent.ItemAddContributorsTask;
+import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException;
+import org.openecomp.core.tools.store.ItemHandler;
+import org.openecomp.core.tools.store.NotificationHandler;
+import org.openecomp.core.tools.store.PermissionHandler;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
+
+public class AddContributorCommand extends Command {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(AddContributorCommand.class);
+ private static final String ITEMS_PATH_OPTION = "p";
+ private static final String UUSERS_PATH_OPTION = "u";
+ private static final int DEFAULT_THREAD_NUMBER = 8;
+ private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list";
+ private static final String COMMAND_ADD_CONTRIBUTOR_FAILED = "Command AddContributor execution failed.";
+
+ AddContributorCommand() {
+ options.addOption(Option.builder(ITEMS_PATH_OPTION).hasArg().argName("file")
+ .desc("file containing list of item ids, mandatory").build());
+ options.addOption(Option.builder(UUSERS_PATH_OPTION).hasArg().argName("file")
+ .desc("file containing list of users, mandatory").build());
}
- List<String> userList;
- try {
- userList = load(userListPath).collect(Collectors.toList());
- } catch (IOException e) {
- throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE +
- "from:" + userListPath, e);
+
+ @Override
+ public boolean execute(String[] args) {
+ CommandLine cmd = parseArgs(args);
+
+ if (!cmd.hasOption(ITEMS_PATH_OPTION) || !cmd.hasOption(UUSERS_PATH_OPTION)) {
+ LOGGER.error("Arguments p and u are mandatory");
+ return false;
+ }
+
+ String itemListPath = cmd.getOptionValue(ITEMS_PATH_OPTION);
+ String userListPath = cmd.getOptionValue(UUSERS_PATH_OPTION);
+
+ List<String> itemList;
+ try {
+ itemList = getItemList(itemListPath);
+ } catch (IOException e) {
+ throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + itemListPath, e);
+ }
+ List<String> userList;
+ try {
+ userList = load(userListPath).collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + "from:" + userListPath, e);
+ }
+
+ List<ItemAddContributorsTask> tasks =
+ itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
+
+ ExecutorService executor = null;
+
+ try {
+ executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER);
+ executeAllTasks(executor, tasks);
+ } catch (InterruptedException e) {
+ throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
+ } finally {
+ if (executor != null) {
+ executor.shutdownNow();
+ }
+ }
+ return true;
}
- List<ItemAddContributorsTask> tasks =
- itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList());
+ @Override
+ public CommandName getCommandName() {
+ return CommandName.ADD_CONTRIBUTOR;
+ }
- ExecutorService executor = null;
+ private static List<String> getItemList(String itemListPath) throws IOException {
+ List<String> itemList;
+ if (itemListPath != null) {
+ itemList = load(itemListPath).collect(Collectors.toList());
+ } else {
+ itemList = new ItemHandler().getItemList();
+ }
- try {
- executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER);
- executeAllTasks(executor, tasks);
- } catch (InterruptedException e) {
- throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e);
- } finally {
- if (executor != null) {
- executor.shutdownNow();
- }
- }
- }
-
- private static List<String> getItemList(String itemListPath) throws IOException {
- List<String> itemList;
- if (itemListPath != null) {
- itemList = load(itemListPath).collect(Collectors.toList());
- } else {
- itemList = new ItemHandler().getItemList();
+ return itemList;
}
- return itemList;
- }
-
- private static void executeAllTasks(ExecutorService executor,
- Collection<? extends Callable<String>> tasks)
- throws InterruptedException {
- List<Future<String>> futureTasks;
- futureTasks = executor.invokeAll(tasks);
- boolean isThreadOpen = true;
- while (isThreadOpen) {
- isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
+ private static void executeAllTasks(ExecutorService executor, Collection<? extends Callable<String>> tasks)
+ throws InterruptedException {
+ List<Future<String>> futureTasks;
+ futureTasks = executor.invokeAll(tasks);
+ boolean isThreadOpen = true;
+ while (isThreadOpen) {
+ isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
+ }
}
- }
+ private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
+ return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), itemId, users);
+ }
- private static ItemAddContributorsTask createTask(String itemId, List<String> users) {
- return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(),
- itemId, users);
- }
-
- private static Stream<String> load(String filePath)
- throws IOException {
- return Files.lines(Paths.get(filePath));
+ private static Stream<String> load(String filePath) throws IOException {
+ return Files.lines(Paths.get(filePath));
- }
+ }
}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/Command.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/Command.java
new file mode 100644
index 0000000000..b8d1d51bf5
--- /dev/null
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/Command.java
@@ -0,0 +1,44 @@
+package org.openecomp.core.tools.commands;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+public abstract class Command {
+
+ static final String COMMAND_OPTION = "c";
+ protected final Options options = new Options();
+
+ protected Command() {
+ options.addOption(
+ Option.builder(COMMAND_OPTION).hasArg().argName("command").desc(getCommandName().name()).build());
+ }
+
+ protected CommandLine parseArgs(String[] args) {
+ CommandLineParser parser = new DefaultParser();
+ CommandLine cmd;
+ try {
+ cmd = parser.parse(options, args);
+ } catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ return cmd;
+ }
+
+ public void printUsage() {
+ HelpFormatter formater = new HelpFormatter();
+ formater.printHelp("zusammenMainTool", options);
+ }
+
+ public void register(){
+ CommandsHolder.addCommand(this);
+ }
+
+ public abstract boolean execute(String[] args);
+
+ public abstract CommandName getCommandName();
+}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandName.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandName.java
new file mode 100644
index 0000000000..7fa04a3907
--- /dev/null
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandName.java
@@ -0,0 +1,10 @@
+package org.openecomp.core.tools.commands;
+
+public enum CommandName {
+ RESET_OLD_VERSION,
+ EXPORT,
+ IMPORT,
+ HEAL_ALL,
+ POPULATE_USER_PERMISSIONS,
+ ADD_CONTRIBUTOR
+}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandsHolder.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandsHolder.java
new file mode 100644
index 0000000000..b6cc0487ea
--- /dev/null
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/CommandsHolder.java
@@ -0,0 +1,80 @@
+package org.openecomp.core.tools.commands;
+
+import static org.openecomp.core.tools.commands.Command.COMMAND_OPTION;
+import static org.openecomp.core.tools.util.Utils.printMessage;
+
+import java.util.EnumMap;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.openecomp.core.tools.exportinfo.ExportDataCommand;
+import org.openecomp.core.tools.importinfo.ImportDataCommand;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
+
+public class CommandsHolder {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(CommandsHolder.class);
+ private static final Options OPTIONS = new Options();
+ private static final Map<CommandName, Command> COMMANDS = new EnumMap<>(CommandName.class);
+
+ static {
+ OPTIONS.addOption(
+ Option.builder(COMMAND_OPTION).hasArg().argName("command").desc("command name, mandatory").build());
+ registerCommands();
+ }
+
+ private static void registerCommands() {
+ new SetHealingFlag().register();
+ new ExportDataCommand().register();
+ new ImportDataCommand().register();
+ new HealAll().register();
+ new PopulateUserPermissions().register();
+ new AddContributorCommand().register();
+ }
+
+ private CommandsHolder() {
+ }
+
+ public static Optional<Command> getCommand(String[] args) {
+ CommandLine cmd = parseArgs(args);
+ return cmd == null || !cmd.hasOption(COMMAND_OPTION) || cmd.getOptionValue(COMMAND_OPTION) == null
+ ? Optional.empty()
+ : getCommandName(cmd.getOptionValue(COMMAND_OPTION)).map(COMMANDS::get);
+ }
+
+ public static void printUsages() {
+ COMMANDS.values().forEach(Command::printUsage);
+ }
+
+ private static Optional<CommandName> getCommandName(String commandName) {
+ try {
+ return Optional.of(CommandName.valueOf(commandName));
+ } catch (IllegalArgumentException iae) {
+ printMessage(LOGGER, String.format("message: %s is illegal command.", commandName));
+ return Optional.empty();
+ }
+ }
+
+ private static CommandLine parseArgs(String[] args) {
+ try {
+ return new DefaultParser().parse(OPTIONS, args, true);
+ } catch (ParseException e) {
+ LOGGER.error("Error parsing arguments", e);
+ return null;
+ }
+ }
+
+ static void addCommand(Command command) {
+ CommandName commandName = command.getCommandName();
+ if (COMMANDS.containsKey(commandName)) {
+ throw new IllegalArgumentException(
+ String.format("Command with the name %s was already registered", commandName));
+ }
+ COMMANDS.put(commandName, command);
+ }
+}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/HealAll.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/HealAll.java
index d6344f75bd..8e0bb0e6b6 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/HealAll.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/HealAll.java
@@ -1,17 +1,5 @@
package org.openecomp.core.tools.commands;
-import org.apache.commons.collections.CollectionUtils;
-import org.openecomp.core.tools.concurrent.ItemHealingTask;
-import org.openecomp.core.tools.exceptions.HealingRuntimeException;
-import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader;
-import org.openecomp.sdc.healing.api.HealingManager;
-import org.openecomp.sdc.healing.factory.HealingManagerFactory;
-import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
-import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
-import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
-import org.openecomp.sdc.versioning.dao.types.Version;
-import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
-
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@@ -25,105 +13,128 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Stream;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.collections.CollectionUtils;
+import org.openecomp.core.tools.concurrent.ItemHealingTask;
+import org.openecomp.core.tools.exceptions.HealingRuntimeException;
+import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader;
+import org.openecomp.sdc.healing.api.HealingManager;
+import org.openecomp.sdc.healing.factory.HealingManagerFactory;
+import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
+import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
+import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
+import org.openecomp.sdc.versioning.dao.types.Version;
+import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
/**
* Created by ayalaben on 11/6/2017
*/
-public class HealAll {
+public class HealAll extends Command {
- private static final int DEFAULT_THREAD_NUMBER = 100;
- private static List<ItemHealingTask> tasks = new ArrayList<>();
- private static VendorSoftwareProductManager vspManager = VspManagerFactory
- .getInstance().createInterface();
- private static HealingManager healingManager = HealingManagerFactory.getInstance()
- .createInterface();
+ private static final int DEFAULT_THREAD_NUMBER = 100;
+ private static final String THREAD_NUM_OPTION = "t";
+ private static List<ItemHealingTask> tasks = new ArrayList<>();
+ private VendorSoftwareProductManager vspManager;
+ private HealingManager healingManager;
- private HealAll() {
- }
+ HealAll() {
+ options.addOption(
+ Option.builder(THREAD_NUM_OPTION).hasArg().argName("number").desc("number of threads").build());
+ }
- public static void healAll(String threadNumber) {
+ @Override
+ public boolean execute(String[] args) {
+ CommandLine cmd = parseArgs(args);
- String logFileName = "healing.log";
- try (BufferedWriter log = new BufferedWriter(new FileWriter(logFileName, true))) {
+ vspManager = VspManagerFactory.getInstance().createInterface();
+ healingManager = HealingManagerFactory.getInstance().createInterface();
- writeToLog("----starting healing------", log);
- Instant startTime = Instant.now();
+ String logFileName = "healing.log";
+ try (BufferedWriter log = new BufferedWriter(new FileWriter(logFileName, true))) {
- int numberOfThreads = Objects.nonNull(threadNumber) ? Integer.valueOf(threadNumber) :
- DEFAULT_THREAD_NUMBER;
- ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
+ writeToLog("----starting healing------", log);
+ Instant startTime = Instant.now();
- filterByEntityType(VersionInfoCassandraLoader.list(),
- VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE).forEach
- (HealAll::addTaskToTasks);
+ int numberOfThreads =
+ cmd.hasOption(THREAD_NUM_OPTION) && Objects.nonNull(cmd.getOptionValue(THREAD_NUM_OPTION))
+ ? Integer.valueOf(cmd.getOptionValue(THREAD_NUM_OPTION))
+ : DEFAULT_THREAD_NUMBER;
+ ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
- executeAllTasks(executor, log);
+ filterByEntityType(VersionInfoCassandraLoader.list(),
+ VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)
+ .forEach(this::addTaskToTasks);
- writeToLog("----finished healing------", log);
- Instant endTime = Instant.now();
- writeToLog("Total runtime was: " + Duration.between(startTime, endTime), log);
- } catch (IOException e) {
- throw new HealingRuntimeException("can't initial healing log file '" + logFileName + "'", e);
+ executeAllTasks(executor, log);
+
+ writeToLog("----finished healing------", log);
+ Instant endTime = Instant.now();
+ writeToLog("Total runtime was: " + Duration.between(startTime, endTime), log);
+ } catch (IOException e) {
+ throw new HealingRuntimeException("can't initial healing log file '" + logFileName + "'", e);
+ }
+ return true;
}
- System.exit(1);
- }
+ @Override
+ public CommandName getCommandName() {
+ return CommandName.HEAL_ALL;
+ }
- private static void executeAllTasks(ExecutorService executor, BufferedWriter log) {
- List<Future<String>> futureTasks;
- try {
- futureTasks = executor.invokeAll(tasks);
- futureTasks.forEach(future -> {
+ private static void executeAllTasks(ExecutorService executor, BufferedWriter log) {
+ List<Future<String>> futureTasks;
try {
- log.write(future.get());
- log.newLine();
- } catch (Exception e) {
- writeToLog(e.getMessage(), log);
+ futureTasks = executor.invokeAll(tasks);
+ futureTasks.forEach(future -> {
+ try {
+ log.write(future.get());
+ log.newLine();
+ } catch (Exception e) {
+ writeToLog(e.getMessage(), log);
+ }
+ });
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ writeToLog("migration tasks failed with message: " + e.getMessage(), log);
+ throw new HealingRuntimeException(e);
}
- });
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- writeToLog("migration tasks failed with message: " + e.getMessage(), log);
- throw new HealingRuntimeException(e);
- }
- boolean isThreadOpen = true;
- while (isThreadOpen) {
- isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
+ boolean isThreadOpen = true;
+ while (isThreadOpen) {
+ isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
+ }
}
- }
- private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
- if (Objects.nonNull(versionInfoEntity.getCandidate())) {
- return versionInfoEntity.getCandidate().getVersion();
- } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
+ private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
+ if (Objects.nonNull(versionInfoEntity.getCandidate())) {
+ return versionInfoEntity.getCandidate().getVersion();
+ } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
- return versionInfoEntity.getViewableVersions().stream().max(Version::compareTo)
- .orElse(new Version());
+ return versionInfoEntity.getViewableVersions().stream().max(Version::compareTo).orElse(new Version());
+ }
+ return versionInfoEntity.getActiveVersion();
}
- return versionInfoEntity.getActiveVersion();
- }
-
- private static void writeToLog(String message, BufferedWriter log) {
- try {
- log.write(message);
- log.newLine();
- } catch (IOException e) {
- throw new HealingRuntimeException("unable to write to healing all log file.", e);
+
+ private static void writeToLog(String message, BufferedWriter log) {
+ try {
+ log.write(message);
+ log.newLine();
+ } catch (IOException e) {
+ throw new HealingRuntimeException("unable to write to healing all log file.", e);
+ }
+ }
+
+ private static Stream<VersionInfoEntity> filterByEntityType(Collection<VersionInfoEntity> versionInfoEntities,
+ String entityType) {
+ return versionInfoEntities.stream()
+ .filter(versionInfoEntity -> versionInfoEntity.getEntityType().equals(entityType));
+ }
+
+ private void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
+ tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion(versionInfoEntity).toString(),
+ vspManager, healingManager));
}
- }
-
- private static Stream<VersionInfoEntity> filterByEntityType(
- Collection<VersionInfoEntity> versionInfoEntities, String entityType) {
- return versionInfoEntities.stream().filter(versionInfoEntity -> versionInfoEntity
- .getEntityType().equals(entityType));
- }
-
- private static void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
- tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion
- (versionInfoEntity).toString(),
- vspManager, healingManager));
- }
}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/PopulateUserPermissions.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/PopulateUserPermissions.java
index fb415c5653..ad280e16c6 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/PopulateUserPermissions.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/PopulateUserPermissions.java
@@ -16,31 +16,33 @@
package org.openecomp.core.tools.commands;
+import static org.openecomp.core.tools.commands.CommandName.POPULATE_USER_PERMISSIONS;
+
+import java.util.Collections;
+import java.util.List;
import org.openecomp.core.tools.store.PermissionHandler;
import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity;
-import java.util.*;
-
-
-public class PopulateUserPermissions {
- private static PermissionHandler permissionHandler = new PermissionHandler();
-
- private PopulateUserPermissions(){ }
-
- public static void execute() {
+public class PopulateUserPermissions extends Command {
+ @Override
+ public boolean execute(String[] args) {
+ PermissionHandler permissionHandler = new PermissionHandler();
List<ItemPermissionsEntity> permissions = permissionHandler.getAll();
permissions.forEach(itemPermissionsEntity -> {
if (!itemPermissionsEntity.getUserId().isEmpty() && !itemPermissionsEntity.getPermission().isEmpty()) {
- permissionHandler.addItem
- (Collections.singleton(itemPermissionsEntity.getItemId()),
- itemPermissionsEntity.getUserId(),itemPermissionsEntity.getPermission());
+ permissionHandler.addItem(Collections.singleton(itemPermissionsEntity.getItemId()),
+ itemPermissionsEntity.getUserId(), itemPermissionsEntity.getPermission());
}
});
- System.exit(0);
+ return true;
+ }
+ @Override
+ public CommandName getCommandName() {
+ return POPULATE_USER_PERMISSIONS;
}
}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/SetHealingFlag.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/SetHealingFlag.java
index fc20b66e3b..c8aee891d0 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/SetHealingFlag.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/SetHealingFlag.java
@@ -1,33 +1,48 @@
package org.openecomp.core.tools.commands;
+import static org.openecomp.core.tools.commands.CommandName.RESET_OLD_VERSION;
+
import com.datastax.driver.core.ResultSet;
+import java.util.ArrayList;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
import org.openecomp.core.tools.store.HealingHandler;
import org.openecomp.core.tools.store.VersionCassandraLoader;
import org.openecomp.core.tools.store.zusammen.datatypes.HealingEntity;
-import java.util.ArrayList;
-
/**
* Created by ayalaben on 10/15/2017
*/
-public class SetHealingFlag {
+public class SetHealingFlag extends Command {
+
+ private static final String VERSION_OPTION = "v";
+ SetHealingFlag() {
+ options.addOption(Option.builder(VERSION_OPTION).hasArg().argName("version").desc("release version").build());
+ }
- private SetHealingFlag(){}
+ @Override
+ public boolean execute(String[] args) {
+ CommandLine cmd = parseArgs(args);
+ String oldVersion = cmd.hasOption(VERSION_OPTION) ? cmd.getOptionValue(VERSION_OPTION) : null;
- public static void populateHealingTable(String oldVersion) {
+ VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
+ ResultSet listItemVersion = versionCassandraLoader.listItemVersion();
- VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
- ResultSet listItemVersion = versionCassandraLoader.listItemVersion();
+ ArrayList<HealingEntity> healingEntities = new ArrayList<>();
- ArrayList<HealingEntity> healingEntities = new ArrayList<>();
+ listItemVersion.iterator().forEachRemaining(entry -> healingEntities.add(new HealingEntity(entry.getString(0),
+ entry.getString(1), entry.getString(2), true, oldVersion)));
- listItemVersion.iterator().forEachRemaining(entry -> healingEntities.add(new HealingEntity
- (entry.getString(0),entry.getString(1),entry.getString(2),true,oldVersion)));
+ HealingHandler healingHandler = new HealingHandler();
+ healingHandler.populateHealingTable(healingEntities);
- HealingHandler healingHandler = new HealingHandler();
- healingHandler.populateHealingTable(healingEntities);
+ return true;
+ }
- }
+ @Override
+ public CommandName getCommandName() {
+ return RESET_OLD_VERSION;
+ }
}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportDataCommand.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportDataCommand.java
index decd0cee9a..64511cdea0 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportDataCommand.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exportinfo/ExportDataCommand.java
@@ -1,24 +1,18 @@
/**
* Copyright © 2016-2017 European Support Limited.
*/
+
package org.openecomp.core.tools.exportinfo;
+import static java.nio.file.Files.createDirectories;
+import static org.openecomp.core.tools.commands.CommandName.EXPORT;
+
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Session;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
-import org.apache.commons.io.FileUtils;
-import org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory;
-import org.openecomp.core.tools.importinfo.ImportProperties;
-import org.openecomp.core.tools.util.Utils;
-import org.openecomp.core.tools.util.ZipUtils;
-import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
-import org.yaml.snakeyaml.Yaml;
-
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -35,31 +29,55 @@ import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.io.FileUtils;
+import org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory;
+import org.openecomp.core.tools.commands.Command;
+import org.openecomp.core.tools.commands.CommandName;
+import org.openecomp.core.tools.importinfo.ImportProperties;
+import org.openecomp.core.tools.util.Utils;
+import org.openecomp.core.tools.util.ZipUtils;
+import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
+import org.yaml.snakeyaml.Yaml;
-import static java.nio.file.Files.createDirectories;
+public final class ExportDataCommand extends Command {
-public final class ExportDataCommand {
- private static final Logger logger = LoggerFactory.getLogger(ExportDataCommand.class);
- public static final String JOIN_DELIMITER = "$#";
+ private static final Logger LOGGER = LoggerFactory.getLogger(ExportDataCommand.class);
+ private static final String ITEM_ID_OPTION = "i";
+ static final String JOIN_DELIMITER = "$#";
public static final String JOIN_DELIMITER_SPLITTER = "\\$\\#";
- public static final String MAP_DELIMITER = "!@";
+ static final String MAP_DELIMITER = "!@";
public static final String MAP_DELIMITER_SPLITTER = "\\!\\@";
- public static final int THREAD_POOL_SIZE = 6;
+ private static final int THREAD_POOL_SIZE = 6;
public static final String NULL_REPRESENTATION = "nnuullll";
- private ExportDataCommand() {
+ public ExportDataCommand() {
+ options.addOption(
+ Option.builder(ITEM_ID_OPTION).hasArg().argName("item id").desc("id of item to export, mandatory").build());
}
- public static void exportData(String filterItem) {
+ @Override
+ public boolean execute(String[] args) {
+ CommandLine cmd = parseArgs(args);
+
+ if (!cmd.hasOption(ITEM_ID_OPTION) || cmd.getOptionValue(ITEM_ID_OPTION) == null) {
+ LOGGER.error("Argument i is mandatory");
+ return false;
+ }
+
ExecutorService executor = null;
try {
CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
Path rootDir = Paths.get(ImportProperties.ROOT_DIRECTORY);
initDir(rootDir);
try (Session session = CassandraSessionFactory.getSession()) {
- final Set<String> filteredItems = Sets.newHashSet(filterItem);
- Set<String> fis = filteredItems.stream().map(fi -> fi.replaceAll("\\r", "")).collect(Collectors.toSet());
+ final Set<String> filteredItems = Sets.newHashSet(cmd.getOptionValue(ITEM_ID_OPTION));
+ Set<String> fis =
+ filteredItems.stream().map(fi -> fi.replaceAll("\\r", "")).collect(Collectors.toSet());
Map<String, List<String>> queries;
Yaml yaml = new Yaml();
try (InputStream is = ExportDataCommand.class.getResourceAsStream("/queries.yaml")) {
@@ -86,35 +104,39 @@ public final class ExportDataCommand {
zipPath(rootDir);
FileUtils.forceDelete(rootDir.toFile());
} catch (Exception ex) {
- Utils.logError(logger, ex);
+ Utils.logError(LOGGER, ex);
} finally {
if (executor != null) {
executor.shutdown();
}
}
-
+ return true;
}
+ @Override
+ public CommandName getCommandName() {
+ return EXPORT;
+ }
- private static void executeQuery(final Session session, final String query, final Set<String> filteredItems, final String filteredColumn,
- final Set<String> vlms, final CountDownLatch donequerying, Executor executor) {
+ private static void executeQuery(final Session session, final String query, final Set<String> filteredItems,
+ final String filteredColumn, final Set<String> vlms, final CountDownLatch donequerying, Executor executor) {
ResultSetFuture resultSetFuture = session.executeAsync(query);
Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet resultSet) {
try {
- Utils.printMessage(logger, "Start to serialize " + query);
+ Utils.printMessage(LOGGER, "Start to serialize " + query);
new ExportSerializer().serializeResult(resultSet, filteredItems, filteredColumn, vlms);
donequerying.countDown();
- } catch (Exception e){
- Utils.logError(logger, "Serialization failed :" + query, e);
+ } catch (Exception e) {
+ Utils.logError(LOGGER, "Serialization failed :" + query, e);
System.exit(-1);
}
}
@Override
public void onFailure(Throwable t) {
- Utils.logError(logger, "Query failed :" + query, t);
+ Utils.logError(LOGGER, "Query failed :" + query, t);
System.exit(-1);
}
}, executor);
@@ -127,7 +149,7 @@ public final class ExportDataCommand {
dateStr = dateStr.replaceAll(":", "_");
String zipFile = System.getProperty("user.home") + File.separatorChar + "onboarding_import" + dateStr + ".zip";
ZipUtils.createZip(zipFile, rootDir);
- Utils.printMessage(logger, "Exported file :" + zipFile);
+ Utils.printMessage(LOGGER, "Exported file :" + zipFile);
}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportDataCommand.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportDataCommand.java
index 17b7d64cb4..6f18bca9b8 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportDataCommand.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/importinfo/ImportDataCommand.java
@@ -1,7 +1,18 @@
package org.openecomp.core.tools.importinfo;
+import static org.openecomp.core.tools.commands.CommandName.IMPORT;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.stream.Stream;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
import org.apache.commons.io.FileUtils;
+import org.openecomp.core.tools.commands.Command;
+import org.openecomp.core.tools.commands.CommandName;
import org.openecomp.core.tools.exportinfo.ExportDataCommand;
import org.openecomp.core.tools.util.Utils;
import org.openecomp.core.tools.util.ZipUtils;
@@ -9,28 +20,40 @@ import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.stream.Stream;
+public class ImportDataCommand extends Command {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ImportDataCommand.class);
+ private static final String FILE_OPTION = "f";
-public class ImportDataCommand {
+ public ImportDataCommand() {
+ options.addOption(Option.builder(FILE_OPTION).hasArg().argName("file").desc("export file (zip), mandatory").build());
+ }
- private static final Logger logger = LoggerFactory.getLogger(ImportDataCommand.class);
+ @Override
+ public boolean execute(String[] args) {
+ CommandLine cmd = parseArgs(args);
- public static void execute(String uploadFile) {
+ if (!cmd.hasOption(FILE_OPTION) || cmd.getOptionValue(FILE_OPTION) == null) {
+ LOGGER.error("Argument f is mandatory");
+ return false;
+ }
try {
CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
Path outputFolder = Paths.get(ImportProperties.ROOT_DIRECTORY);
ExportDataCommand.initDir(outputFolder); //clear old imports.
- ZipUtils.unzip(Paths.get(uploadFile), outputFolder);
- try( Stream<Path> files = Files.list(outputFolder)) {
+ ZipUtils.unzip(Paths.get(cmd.getOptionValue(FILE_OPTION)), outputFolder);
+ try (Stream<Path> files = Files.list(outputFolder)) {
files.forEach(new ImportSingleTable()::importFile);
}
FileUtils.forceDelete(outputFolder.toFile()); // leaves directory clean
} catch (IOException e) {
- Utils.logError(logger, e);
+ Utils.logError(LOGGER, e);
}
+ return true;
+ }
+
+ @Override
+ public CommandName getCommandName() {
+ return IMPORT;
}
}
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java
index 1080a23dfa..d9c46e103b 100644
--- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java
+++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java
@@ -1,103 +1,51 @@
package org.openecomp.core.tools.main;
-import org.openecomp.core.tools.commands.AddContributorCommand;
-import org.openecomp.core.tools.commands.HealAll;
-import org.openecomp.core.tools.commands.PopulateUserPermissions;
-import org.openecomp.core.tools.commands.SetHealingFlag;
-import org.openecomp.core.tools.exportinfo.ExportDataCommand;
-import org.openecomp.core.tools.importinfo.ImportDataCommand;
-import org.openecomp.core.tools.util.ToolsUtil;
-import org.openecomp.sdc.common.session.SessionContextProviderFactory;
-import org.openecomp.sdc.logging.api.Logger;
-import org.openecomp.sdc.logging.api.LoggerFactory;
+import static org.openecomp.core.tools.util.Utils.printMessage;
import java.time.Duration;
import java.time.Instant;
-
-import static org.openecomp.core.tools.util.Utils.printMessage;
+import java.util.Optional;
+import org.openecomp.core.tools.commands.Command;
+import org.openecomp.core.tools.commands.CommandsHolder;
+import org.openecomp.sdc.common.session.SessionContextProviderFactory;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
public class ZusammenMainTool {
- private static final String GLOBAL_USER = "GLOBAL_USER";
- private static Logger logger = LoggerFactory.getLogger(ZusammenMainTool.class);
- private static int status = 0;
+ private static final Logger LOGGER = LoggerFactory.getLogger(ZusammenMainTool.class);
- public static void main(String[] args) {
+ public static void main(String[] args) {
+ Command command = getCommandToRun(args);
- COMMANDS command = getCommand(args);
+ Instant startTime = Instant.now();
+ SessionContextProviderFactory.getInstance().createInterface().create("GLOBAL_USER", "dox");
+ if (!command.execute(args)) {
+ command.printUsage();
+ System.exit(-1);
+ }
+ Instant stopTime = Instant.now();
- if (command == null) {
- printMessage(logger,
- "parameter -c is mandatory. script usage: zusammenMainTool.sh -c {command name} " +
- "[additional arguments depending on the command] ");
- printMessage(logger,
- "reset old version: -c RESET_OLD_VERSION [-v {version}]");
- printMessage(logger,
- "export: -c EXPORT [-i {item id}]");
- printMessage(logger,
- "import: -c IMPORT -f {zip file full path}");
- printMessage(logger,
- "heal all: -c HEAL_ALL [-t {number of threads}]");
- printMessage(logger,
- "add users as contributors: -c ADD_CONTRIBUTOR [-p {item id list file path}] -u {user " +
- "list file path}");
- System.exit(-1);
+ printDuration(command, startTime, stopTime);
+ System.exit(0);
}
- Instant startTime = Instant.now();
-
- SessionContextProviderFactory.getInstance().createInterface().create(GLOBAL_USER, "dox");
-
- switch (command) {
- case RESET_OLD_VERSION:
- SetHealingFlag.populateHealingTable(ToolsUtil.getParam("v", args));
- break;
- case EXPORT:
- ExportDataCommand.exportData(ToolsUtil.getParam("i", args));
- break;
- case IMPORT:
- ImportDataCommand.execute(ToolsUtil.getParam("f", args));
- break;
- case HEAL_ALL:
- HealAll.healAll(ToolsUtil.getParam("t", args));
- break;
- case POPULATE_USER_PERMISSIONS:
- PopulateUserPermissions.execute();
- break;
- case ADD_CONTRIBUTOR:
- AddContributorCommand.add(ToolsUtil.getParam("p", args), ToolsUtil.getParam("u", args));
+ private static Command getCommandToRun(String[] args) {
+ Optional<Command> command = CommandsHolder.getCommand(args);
+ if (!command.isPresent()) {
+ LOGGER.error("Illegal execution.");
+ CommandsHolder.printUsages();
+ System.exit(-1);
+ }
+ return command.get();
}
- Instant stopTime = Instant.now();
- Duration duration = Duration.between(startTime, stopTime);
- long minutesPart = duration.toMinutes();
- long secondsPart = duration.minusMinutes(minutesPart).getSeconds();
-
+ private static void printDuration(Command command, Instant startTime, Instant stopTime) {
+ Duration duration = Duration.between(startTime, stopTime);
+ long minutesPart = duration.toMinutes();
+ long secondsPart = duration.minusMinutes(minutesPart).getSeconds();
- printMessage(logger,
- "Zusammen tools command:[] finished . Total run time was : " + minutesPart + ":" +
- secondsPart
- + " minutes");
- System.exit(status);
-
- }
-
- private static COMMANDS getCommand(String[] args) {
- String commandSrt = ToolsUtil.getParam("c", args);
- try {
- return COMMANDS.valueOf(commandSrt);
- } catch (IllegalArgumentException iae) {
- printMessage(logger, "message:" + commandSrt + " is illegal.");
+ printMessage(LOGGER, String.format("Zusammen tools command %s finished. Total run time was %s:%s minutes.",
+ command.getCommandName(), minutesPart, secondsPart));
}
- return null;
- }
-
- private enum COMMANDS {
- RESET_OLD_VERSION,
- EXPORT,
- IMPORT,
- HEAL_ALL,
- POPULATE_USER_PERMISSIONS,
- ADD_CONTRIBUTOR
- }
}