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