1 package org.openecomp.core.tools.importinfo;
4 import static org.openecomp.core.tools.commands.CommandName.IMPORT;
6 import java.io.IOException;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10 import java.util.stream.Stream;
11 import org.apache.commons.cli.CommandLine;
12 import org.apache.commons.cli.Option;
13 import org.apache.commons.io.FileUtils;
14 import org.openecomp.core.tools.commands.Command;
15 import org.openecomp.core.tools.commands.CommandName;
16 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
17 import org.openecomp.core.tools.util.Utils;
18 import org.openecomp.core.tools.util.ZipUtils;
19 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
20 import org.openecomp.sdc.logging.api.Logger;
21 import org.openecomp.sdc.logging.api.LoggerFactory;
23 public class ImportDataCommand extends Command {
25 private static final Logger LOGGER = LoggerFactory.getLogger(ImportDataCommand.class);
26 private static final String FILE_OPTION = "f";
28 public ImportDataCommand() {
29 options.addOption(Option.builder(FILE_OPTION).hasArg().argName("file").desc("export file (zip), mandatory").build());
33 public boolean execute(String[] args) {
34 CommandLine cmd = parseArgs(args);
36 if (!cmd.hasOption(FILE_OPTION) || cmd.getOptionValue(FILE_OPTION) == null) {
37 LOGGER.error("Argument f is mandatory");
41 CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
42 Path outputFolder = Paths.get(ImportProperties.ROOT_DIRECTORY);
43 ExportDataCommand.initDir(outputFolder); //clear old imports.
44 ZipUtils.unzip(Paths.get(cmd.getOptionValue(FILE_OPTION)), outputFolder);
45 try (Stream<Path> files = Files.list(outputFolder)) {
46 files.forEach(new ImportSingleTable()::importFile);
48 FileUtils.forceDelete(outputFolder.toFile()); // leaves directory clean
49 } catch (IOException e) {
50 Utils.logError(LOGGER, e);
56 public CommandName getCommandName() {