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