Initial add of appc-directed-graph bundles
[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         
88
89
90         public static void main(String[] args) {
91                 try {
92                         String xmlPath = null;
93                         String propertyPath = null;
94
95                         if(args != null && args.length >= 2){
96                                 xmlPath = args[0];
97                                 propertyPath = args[1];
98                         }else{
99                                 throw new Exception("Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <dbPropertyfile>");
100                         }
101                         
102                         //propertyPath = "/Users/bs2796/0Source/ecomp/bvc-3.2.2/others/properties/dblib.properties";
103                         //xmlPath = DGXMLLoadNActivate.class.getClassLoader().getResource(".").getPath() +"/xml" ;
104
105                         DGXMLLoad dgXMLLoadDB = new DGXMLLoad(propertyPath);
106                         dgXMLLoadDB.loadDGXMLDir(xmlPath);
107                 } catch (Exception e) {
108                         e.printStackTrace();
109                 }finally {
110                         System.exit(1);
111                 }
112         }
113
114 }