Changed to unmaintained
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / onap / sdnc / dg / loader / DGXMLLoader.java
1 package org.onap.sdnc.dg.loader;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
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;
15
16 public class DGXMLLoader {
17
18     private static final Logger logger = LoggerFactory.getLogger(DGXMLLoader.class);
19     public static final String STRING_ENCODING = "utf-8";
20
21     private final SvcLogicStore store;
22
23     public DGXMLLoader(String propfile) throws DGXMLException, SvcLogicException {
24         if (StringUtils.isBlank(propfile)) {
25             throw new DGXMLException(propfile + " Profile file is not defined");
26         }
27         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
28     }
29
30     protected DGXMLLoader(SvcLogicStore store) {
31         this.store = store;
32     }
33
34     public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
35         if (dgXMLpath != null) {
36             SvcLogicParser.load(dgXMLpath, this.store);
37         }
38     }
39
40     public void loadDGXMLDir(String xmlPath) {
41         try {
42             logger.info(
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);
51                 } else {
52                     throw new DGXMLException(xmlPath + " is not a valid XML Directory");
53                 }
54             } else {
55                 throw new DGXMLException("Failed to initialise SvcLogicStore");
56             }
57
58             if (!errors.isEmpty()) {
59                 throw new DGXMLException(errors.toString());
60             }
61         } catch (Exception e) {
62             logger.error("Failed to load DGXML directories", e);
63         }
64     }
65
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());
69             try {
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());
75             }
76         }
77     }
78 }