13e5e293f3d9d67705379582e04c3a4a81750c0e
[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  * 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.openecomp.sdnc.dg.loader;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import org.openecomp.sdnc.sli.SvcLogicException;
37 import org.openecomp.sdnc.sli.SvcLogicParser;
38 import org.openecomp.sdnc.sli.SvcLogicStore;
39 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
40
41 public class DGXMLLoad {
42     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoad.class);
43     private final SvcLogicStore store;
44     public static String STRING_ENCODING = "utf-8";
45
46     public DGXMLLoad(String propfile) throws Exception{
47         if(StringUtils.isBlank(propfile)){
48             throw new Exception(propfile + " Profile file is not defined");
49         }
50         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
51     }
52
53     protected DGXMLLoad(SvcLogicStore store) throws Exception {
54         this.store=store;
55     }
56     
57     public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException{
58         if(dgXMLpath != null ){
59             SvcLogicParser.load(dgXMLpath, this.store);
60         }
61     }
62
63     private void loadDGXMLDir(String xmlPath) throws Exception {
64         try {
65             logger.info("******************** Loading DG into Database *****************************");
66             List<String> errors = new ArrayList<String>();
67             if(this.store != null){
68                 File xmlDir = new File(xmlPath);
69                 if(xmlDir != null && xmlDir.isDirectory()){
70                     String[] extensions = new String[] { "xml", "XML" };
71                     List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
72                     for (File file : files) {
73                         logger.info("Loading DG XML file :" + file.getCanonicalPath());
74                         try{
75                             SvcLogicParser.load(file.getCanonicalPath(), this.store);
76                         }catch (Exception e) {
77                             errors.add("Failed to load XML "+file.getCanonicalPath() + ", 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("Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <dbPropertyfile>");
105             }
106             DGXMLLoad dgXMLLoadDB = new DGXMLLoad(propertyPath);
107             dgXMLLoadDB.loadDGXMLDir(xmlPath);
108         } catch (Exception e) {
109             e.printStackTrace();
110         }finally {
111             System.exit(1);
112         }
113     }
114
115 }