6f18bca9b8d71db20ea9adc4b5952e365e8a05f3
[sdc.git] /
1 package org.openecomp.core.tools.importinfo;
2
3
4 import static org.openecomp.core.tools.commands.CommandName.IMPORT;
5
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;
22
23 public class ImportDataCommand extends Command {
24
25     private static final Logger LOGGER = LoggerFactory.getLogger(ImportDataCommand.class);
26     private static final String FILE_OPTION = "f";
27
28     public ImportDataCommand() {
29         options.addOption(Option.builder(FILE_OPTION).hasArg().argName("file").desc("export file (zip), mandatory").build());
30     }
31
32     @Override
33     public boolean execute(String[] args) {
34         CommandLine cmd = parseArgs(args);
35
36         if (!cmd.hasOption(FILE_OPTION) || cmd.getOptionValue(FILE_OPTION) == null) {
37             LOGGER.error("Argument f is mandatory");
38             return false;
39         }
40         try {
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);
47             }
48             FileUtils.forceDelete(outputFolder.toFile()); // leaves directory clean
49         } catch (IOException e) {
50             Utils.logError(LOGGER, e);
51         }
52         return true;
53     }
54
55     @Override
56     public CommandName getCommandName() {
57         return IMPORT;
58     }
59 }