aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/ImportCommand.java
blob: cebff91160008291c6deb34a6e73fcf4cdfac308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.openecomp.core.tools.Commands;

import com.amdocs.zusammen.datatypes.SessionContext;
import org.apache.commons.io.FileUtils;
import org.openecomp.core.tools.Commands.importdata.TreeWalker;
import org.openecomp.core.tools.Commands.exportdata.ImportProperties;
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 java.nio.file.Path;
import java.nio.file.Paths;

public class ImportCommand {

    private static final Logger logger = LoggerFactory.getLogger(ImportCommand.class);


    public static void importData(SessionContext context, String zippedFile, String filterItem) {
        try {
            ImportProperties.initParams();
            CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
            if (zippedFile == null){
                logger.error("Import must have a valid file as an input.");
            }
            if (zippedFile != null) {
                zippedFile = zippedFile.replaceAll("\\r", "");
                if (filterItem != null) {
                    filterItem = filterItem.replaceAll("\\r", "");
                }
                Path rootDir = Paths.get(ImportProperties.ROOT_DIRECTORY);
                ExportDataCommand.initDir(rootDir);
                ZipUtils.unzip(Paths.get(zippedFile), rootDir);
                TreeWalker.walkFiles(context, rootDir, filterItem);
                FileUtils.forceDelete(rootDir.toFile()); // clear all unzip data at the end.
            }


        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

}