f5eb2441f98cfdc4c230e582c9544612c63cdef6
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / openecomp / sdnc / dg / loader / DGXMLLoadNActivate.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.SvcLogicGraph;
34 import org.openecomp.sdnc.sli.SvcLogicParser;
35 import org.openecomp.sdnc.sli.SvcLogicStore;
36 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
37
38 public class DGXMLLoadNActivate {
39     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
40     private final SvcLogicStore store;
41     public static String STRING_ENCODING = "utf-8";
42
43     public DGXMLLoadNActivate(String propfile) throws Exception{
44         if(StringUtils.isBlank(propfile)){
45             throw new Exception(propfile + " Profile file is not defined");
46         }
47         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
48     }
49
50     public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException{
51         if(dgXMLpath != null ){
52             SvcLogicParser.load(dgXMLpath, this.store);
53         }
54     }
55
56     private void loadDGXMLDir(String xmlPath) throws Exception {
57         try {
58             logger.info("******************** Loading DG into Database *****************************");
59             List<String> errors = new ArrayList<String>();
60             if(this.store != null){
61                 File xmlDir = new File(xmlPath);
62                 if(xmlDir != null && xmlDir.isDirectory()){
63                     String[] extensions = new String[] { "xml", "XML" };
64                     List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
65                     for (File file : files) {
66                         logger.info("Loading DG XML file :" + file.getCanonicalPath());
67                         try{
68                             SvcLogicParser.load(file.getCanonicalPath(), this.store);
69                         }catch (Exception e) {
70                             errors.add("Failed to load XML "+file.getCanonicalPath() + ", Exception : "+e.getMessage());
71                         }
72                     }
73                 }else{
74                     throw new Exception(xmlPath + " is not a valid XML Directory");
75                 }
76             }else{
77                 throw new Exception("Failed to initialise SvcLogicStore");
78             }
79
80             if(errors.size() > 0){
81                 throw new Exception(errors.toString());
82             }
83         } catch (Exception e) {
84             logger.error(e.getMessage());
85         }
86     }
87
88     public void activateDg(String activateFilePath) throws Exception {
89         logger.info("******************** Activating DG into Database *****************************");
90         try {
91             List<String> errors = new ArrayList<String>();
92             if(this.store != null){
93                 File activateFile = new File(activateFilePath);
94                 if(activateFile != null && activateFile.isFile()){
95                     List<String> fileLines = FileUtils.readLines(activateFile,STRING_ENCODING);
96                     if(fileLines != null ){
97                         for (String line : fileLines) {
98                             if(line != null && ! line.trim().startsWith("#")){
99                                 String lineArray[] = line.trim().split(":");
100                                 try {
101                                     if(lineArray != null && lineArray.length >= 4){
102                                         String module = lineArray[0];
103                                         String rpc = lineArray[1];
104                                         String version = lineArray[2];
105                                         String mode = lineArray[3];
106                                         if(StringUtils.isNotBlank(module) && StringUtils.isNotBlank(rpc)
107                                                 && StringUtils.isNotBlank(version) && StringUtils.isNotBlank(mode)){
108                                             logger.info("Activating DG :" + line);
109                                             SvcLogicGraph graph = this.store.fetch(module, rpc, version, mode);
110                                             if(graph != null){
111                                                 logger.info("Found Graph :" + line + " Activating ...");
112                                                 this.store.activate(graph);
113                                             }else{
114                                                 throw new Exception("Failed to fetch from Database");
115                                             }
116                                         }
117                                     }
118                                 } catch (Exception e) {
119                                     e.printStackTrace();
120                                     errors.add("Failed to Activate "+line + ", "+e.getMessage());
121                                 }
122                             }
123                         }
124                     }
125                 }else{
126                     throw new Exception(activateFile + " is not a valid Activate file Path");
127                 }
128             }else{
129                 throw new Exception("Failed to initialise SvcLogicStore");
130             }
131
132             if(errors.size() > 0){
133                 throw new Exception(errors.toString());
134             }
135         } catch (Exception e) {
136             logger.error(e.getMessage());
137         }
138     }
139
140
141     public static void main(String[] args) {
142         try {
143             String xmlPath = null;
144             String propertyPath = null;
145             String activateFile = null;
146
147             if(args != null && args.length >= 3){
148                 xmlPath = args[0];
149                 activateFile = args[1];
150                 propertyPath = args[2];
151             }else{
152                 throw new Exception("Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
153             }
154
155             //propertyPath = "/Users/bs2796/0Source/ecomp/bvc-3.2.2/others/properties/dblib.properties";
156             //xmlPath = DGXMLLoadNActivate.class.getClassLoader().getResource(".").getPath() +"/xml" ;
157
158             DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
159             dgXMLLoadDB.loadDGXMLDir(xmlPath);
160             dgXMLLoadDB.activateDg(activateFile);
161         } catch (Exception e) {
162             e.printStackTrace();
163         }finally {
164             System.exit(1);
165         }
166     }
167
168 }