1 package org.onap.sdnc.dg.loader;
4 import java.io.IOException;
5 import java.util.ArrayList;
7 import org.apache.commons.io.FileUtils;
8 import org.apache.commons.lang.StringUtils;
9 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
10 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
11 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
12 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 public class DGXMLLoader {
18 private static final Logger logger = LoggerFactory.getLogger(DGXMLLoader.class);
19 public static final String STRING_ENCODING = "utf-8";
21 private final SvcLogicStore store;
23 public DGXMLLoader(String propfile) throws DGXMLException, SvcLogicException {
24 if (StringUtils.isBlank(propfile)) {
25 throw new DGXMLException(propfile + " Profile file is not defined");
27 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
30 protected DGXMLLoader(SvcLogicStore store) {
34 public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
35 if (dgXMLpath != null) {
36 SvcLogicParser.load(dgXMLpath, this.store);
40 public void loadDGXMLDir(String xmlPath) {
43 "******************** Loading DG into Database *****************************");
44 List<String> errors = new ArrayList<>();
45 if (this.store != null) {
46 File xmlDir = new File(xmlPath);
47 if (xmlDir.isDirectory()) {
48 String[] extensions = new String[]{"xml", "XML"};
49 List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
50 tryLoadXmls(errors, files);
52 throw new DGXMLException(xmlPath + " is not a valid XML Directory");
55 throw new DGXMLException("Failed to initialise SvcLogicStore");
58 if (!errors.isEmpty()) {
59 throw new DGXMLException(errors.toString());
61 } catch (Exception e) {
62 logger.error("Failed to load DGXML directories", e);
66 private void tryLoadXmls(List<String> errors, List<File> files) throws IOException {
67 for (File file : files) {
68 logger.info("Loading DG XML file :" + file.getCanonicalPath());
70 SvcLogicParser.load(file.getCanonicalPath(), store);
71 } catch (Exception e) {
72 logger.error("Failed to load XML " + file.getCanonicalPath(), e);
73 errors.add("Failed to load XML " + file.getCanonicalPath()
74 + ", Exception : " + e.getMessage());