ced6b007cb6d50a9796db3ecea162162e0d94803
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / onap / sdnc / dg / loader / DGXMLLoad.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.dg.loader;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
38
39 public class DGXMLLoad {
40     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoad.class);
41     private final SvcLogicStore store;
42     public static String STRING_ENCODING = "utf-8";
43
44     public DGXMLLoad(String propfile) throws Exception {
45         if (StringUtils.isBlank(propfile)) {
46             throw new Exception(propfile + " Profile file is not defined");
47         }
48         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
49     }
50
51     protected DGXMLLoad(SvcLogicStore store) throws Exception {
52         this.store = store;
53     }
54
55     public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
56         if (dgXMLpath != null) {
57             SvcLogicParser.load(dgXMLpath, this.store);
58         }
59     }
60
61     private void loadDGXMLDir(String xmlPath) throws Exception {
62         try {
63             logger.info(
64                     "******************** Loading DG into Database *****************************");
65             List<String> errors = new ArrayList<String>();
66             if (this.store != null) {
67                 File xmlDir = new File(xmlPath);
68                 if (xmlDir != null && xmlDir.isDirectory()) {
69                     String[] extensions = new String[] {"xml", "XML"};
70                     List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
71                     for (File file : files) {
72                         logger.info("Loading DG XML file :" + file.getCanonicalPath());
73                         try {
74                             SvcLogicParser.load(file.getCanonicalPath(), this.store);
75                         } catch (Exception e) {
76                             errors.add("Failed to load XML " + file.getCanonicalPath()
77                                     + ", Exception : " + e.getMessage());
78                         }
79                     }
80                 } else {
81                     throw new Exception(xmlPath + " is not a valid XML Directory");
82                 }
83             } else {
84                 throw new Exception("Failed to initialise SvcLogicStore");
85             }
86
87             if (errors.size() > 0) {
88                 throw new Exception(errors.toString());
89             }
90         } catch (Exception e) {
91             logger.error(e.getMessage());
92         }
93     }
94
95     public static void main(String[] args) {
96         try {
97             String xmlPath = null;
98             String propertyPath = null;
99
100             if (args != null && args.length >= 2) {
101                 xmlPath = args[0];
102                 propertyPath = args[1];
103             } else {
104                 throw new Exception(
105                         "Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <dbPropertyfile>");
106             }
107             DGXMLLoad dgXMLLoadDB = new DGXMLLoad(propertyPath);
108             dgXMLLoadDB.loadDGXMLDir(xmlPath);
109         } catch (Exception e) {
110             e.printStackTrace();
111         } finally {
112             System.exit(1);
113         }
114     }
115
116 }