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