cff8eb98cfa116eed41b9d0b12dce246adb3816d
[sdc.git] /
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.importinfo;
22
23
24 import static org.openecomp.core.tools.commands.CommandName.IMPORT;
25
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.stream.Stream;
31 import org.apache.commons.cli.CommandLine;
32 import org.apache.commons.cli.Option;
33 import org.apache.commons.io.FileUtils;
34 import org.openecomp.core.tools.commands.Command;
35 import org.openecomp.core.tools.commands.CommandName;
36 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
37 import org.openecomp.core.tools.util.Utils;
38 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
39 import org.openecomp.sdc.common.zip.ZipUtils;
40 import org.openecomp.sdc.common.zip.exception.ZipException;
41 import org.openecomp.sdc.logging.api.Logger;
42 import org.openecomp.sdc.logging.api.LoggerFactory;
43
44 public class ImportDataCommand extends Command {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(ImportDataCommand.class);
47     private static final String FILE_OPTION = "f";
48
49     public ImportDataCommand() {
50         options.addOption(Option.builder(FILE_OPTION).hasArg().argName("file").desc("export file (zip), mandatory").build());
51     }
52
53     @Override
54     public boolean execute(String[] args) {
55         CommandLine cmd = parseArgs(args);
56
57         if (!cmd.hasOption(FILE_OPTION) || cmd.getOptionValue(FILE_OPTION) == null) {
58             LOGGER.error("Argument f is mandatory");
59             return false;
60         }
61         try {
62             CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
63             Path outputFolder = Paths.get(ImportProperties.ROOT_DIRECTORY);
64             ExportDataCommand.initDir(outputFolder); //clear old imports.
65             ZipUtils.unzip(Paths.get(cmd.getOptionValue(FILE_OPTION)), outputFolder);
66             try (Stream<Path> files = Files.list(outputFolder)) {
67                 files.forEach(new ImportSingleTable()::importFile);
68             }
69             FileUtils.forceDelete(outputFolder.toFile()); // leaves directory clean
70         } catch (final IOException | ZipException e) {
71             Utils.logError(LOGGER, e);
72         }
73         return true;
74     }
75
76     @Override
77     public CommandName getCommandName() {
78         return IMPORT;
79     }
80 }