Removing old name references
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / onap / sdnc / dg / loader / DGXMLLoadNActivate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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.onap.sdnc.dg.loader;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
39
40 public class DGXMLLoadNActivate {
41     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
42     private final SvcLogicStore store;
43     public static String STRING_ENCODING = "utf-8";
44
45     public DGXMLLoadNActivate(String propfile) throws Exception {
46         if (StringUtils.isBlank(propfile)) {
47             throw new Exception(propfile + " Profile file is not defined");
48         }
49         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
50     }
51
52     protected DGXMLLoadNActivate(SvcLogicStore store) {
53         this.store = store;
54     }
55
56     public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
57         if (dgXMLpath != null) {
58             SvcLogicParser.load(dgXMLpath, this.store);
59         }
60     }
61
62     private void loadDGXMLDir(String xmlPath) throws Exception {
63         try {
64             logger.info(
65                     "******************** 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.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()
78                                     + ", 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(
98                 "******************** Activating DG into Database *****************************");
99         try {
100             List<String> errors = new ArrayList<String>();
101             if (this.store != null) {
102                 File activateFile = new File(activateFilePath);
103                 if (activateFile.isFile()) {
104                     List<String> fileLines = FileUtils.readLines(activateFile, STRING_ENCODING);
105                     if (fileLines != null) {
106                         for (String line : fileLines) {
107                             if (line != null && !line.trim().startsWith("#")) {
108                                 String lineArray[] = line.trim().split(":");
109                                 try {
110                                     if (lineArray != null && lineArray.length >= 4) {
111                                         String module = lineArray[0];
112                                         String rpc = lineArray[1];
113                                         String version = lineArray[2];
114                                         String mode = lineArray[3];
115                                         if (StringUtils.isNotBlank(module)
116                                                 && StringUtils.isNotBlank(rpc)
117                                                 && StringUtils.isNotBlank(version)
118                                                 && StringUtils.isNotBlank(mode)) {
119                                             logger.info("Activating DG :" + line);
120                                             SvcLogicGraph graph =
121                                                     this.store.fetch(module, rpc, version, mode);
122                                             if (graph != null) {
123                                                 logger.info(
124                                                         "Found Graph :" + line + " Activating ...");
125                                                 this.store.activate(graph);
126                                             } else {
127                                                 throw new Exception(
128                                                         "Failed to fetch from Database");
129                                             }
130                                         }
131                                     }
132                                 } catch (Exception e) {
133                                     e.printStackTrace();
134                                     errors.add(
135                                             "Failed to Activate " + line + ", " + e.getMessage());
136                                 }
137                             }
138                         }
139                     }
140                 } else {
141                     throw new Exception(activateFile + " is not a valid Activate file Path");
142                 }
143             } else {
144                 throw new Exception("Failed to initialise SvcLogicStore");
145             }
146
147             if (errors.size() > 0) {
148                 throw new Exception(errors.toString());
149             }
150         } catch (Exception e) {
151             logger.error(e.getMessage());
152         }
153     }
154
155
156     public static void main(String[] args) {
157         try {
158             String xmlPath = null;
159             String propertyPath = null;
160             String activateFile = null;
161
162             if (args != null && args.length >= 3) {
163                 xmlPath = args[0];
164                 activateFile = args[1];
165                 propertyPath = args[2];
166             } else {
167                 throw new Exception(
168                         "Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
169             }
170
171             DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
172             dgXMLLoadDB.loadDGXMLDir(xmlPath);
173             dgXMLLoadDB.activateDg(activateFile);
174         } catch (Exception e) {
175             e.printStackTrace();
176         } finally {
177             System.exit(1);
178         }
179     }
180 }