diff options
author | 2018-02-21 15:24:46 +0100 | |
---|---|---|
committer | 2018-02-22 16:02:32 -0500 | |
commit | 7353bcd8f2b85a50720b7cc7ca04c837c7b1796a (patch) | |
tree | 92a9c90b5852b3aea04da1526b5f81a1ec1b4223 /appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoader.java | |
parent | 78cc8df2d203e32296e15e5564d9f809734c2e26 (diff) |
DGXML classes refactoring
Did some refactor in org.onap.sdnc.dg.loader package in order to reduce code duplication
Change-Id: I5c063487046c7ff72042d067213ea3d639a37227
Issue-ID: APPC-655
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Diffstat (limited to 'appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoader.java')
-rw-r--r-- | appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoader.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoader.java b/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoader.java new file mode 100644 index 000000000..c85b22d1a --- /dev/null +++ b/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoader.java @@ -0,0 +1,78 @@ +package org.onap.sdnc.dg.loader; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicParser; +import org.onap.ccsdk.sli.core.sli.SvcLogicStore; +import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DGXMLLoader { + + private static final Logger logger = LoggerFactory.getLogger(DGXMLLoader.class); + public static final String STRING_ENCODING = "utf-8"; + + private final SvcLogicStore store; + + public DGXMLLoader(String propfile) throws DGXMLException, SvcLogicException { + if (StringUtils.isBlank(propfile)) { + throw new DGXMLException(propfile + " Profile file is not defined"); + } + this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile); + } + + protected DGXMLLoader(SvcLogicStore store) { + this.store = store; + } + + public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException { + if (dgXMLpath != null) { + SvcLogicParser.load(dgXMLpath, this.store); + } + } + + public void loadDGXMLDir(String xmlPath) { + try { + logger.info( + "******************** Loading DG into Database *****************************"); + List<String> errors = new ArrayList<>(); + if (this.store != null) { + File xmlDir = new File(xmlPath); + if (xmlDir.isDirectory()) { + String[] extensions = new String[]{"xml", "XML"}; + List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true); + tryLoadXmls(errors, files); + } else { + throw new DGXMLException(xmlPath + " is not a valid XML Directory"); + } + } else { + throw new DGXMLException("Failed to initialise SvcLogicStore"); + } + + if (!errors.isEmpty()) { + throw new DGXMLException(errors.toString()); + } + } catch (Exception e) { + logger.error("Failed to load DGXML directories", e); + } + } + + private void tryLoadXmls(List<String> errors, List<File> files) throws IOException { + for (File file : files) { + logger.info("Loading DG XML file :" + file.getCanonicalPath()); + try { + SvcLogicParser.load(file.getCanonicalPath(), store); + } catch (Exception e) { + logger.error("Failed to load XML " + file.getCanonicalPath(), e); + errors.add("Failed to load XML " + file.getCanonicalPath() + + ", Exception : " + e.getMessage()); + } + } + } +} |