17fe9a57d953836404bbf7325ca230fbc90ab570
[appc.git] /
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.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class DGXMLLoadNActivate {
42
43     private static final Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
44     private static final String STRING_ENCODING = "utf-8";
45
46     private final SvcLogicStore store;
47
48     public DGXMLLoadNActivate(String propfile) throws DGXMLException, SvcLogicException {
49         if (StringUtils.isBlank(propfile)) {
50             throw new DGXMLException(propfile + " Profile file is not defined");
51         }
52         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
53     }
54
55     protected DGXMLLoadNActivate(SvcLogicStore store) {
56         this.store = store;
57     }
58
59     public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
60         if (dgXMLpath != null) {
61             SvcLogicParser.load(dgXMLpath, this.store);
62         }
63     }
64
65     private void loadDGXMLDir(String xmlPath) {
66         try {
67             logger.info(
68                 "******************** Loading DG into Database *****************************");
69             List<String> errors = new ArrayList<>();
70             if (this.store != null) {
71                 File xmlDir = new File(xmlPath);
72                 if (xmlDir.isDirectory()) {
73                     String[] extensions = new String[]{"xml", "XML"};
74                     List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
75                     tryLoadXmls(errors, files);
76                 } else {
77                     throw new DGXMLException(xmlPath + " is not a valid XML Directory");
78                 }
79             } else {
80                 throw new DGXMLException("Failed to initialise SvcLogicStore");
81             }
82
83             if (!errors.isEmpty()) {
84                 throw new DGXMLException(errors.toString());
85             }
86         } catch (Exception e) {
87             logger.error("Failed to load DGXML directories", e);
88         }
89     }
90
91     private void tryLoadXmls(List<String> errors, List<File> files) throws IOException {
92         for (File file : files) {
93             logger.info("Loading DG XML file :" + file.getCanonicalPath());
94             try {
95                 SvcLogicParser.load(file.getCanonicalPath(), this.store);
96             } catch (Exception e) {
97                 logger.error("Failed to load XML " + file.getCanonicalPath(), e);
98                 errors.add("Failed to load XML " + file.getCanonicalPath()
99                     + ", Exception : " + e.getMessage());
100             }
101         }
102     }
103
104     public void activateDg(String activateFilePath) {
105         logger.info(
106             "******************** Activating DG into Database *****************************");
107         try {
108             List<String> errors = new ArrayList<>();
109             if (this.store != null) {
110                 File activateFile = new File(activateFilePath);
111                 if (activateFile.isFile()) {
112                     List<String> fileLines = FileUtils.readLines(activateFile, STRING_ENCODING);
113                     tryActivateDG(errors, fileLines);
114                 } else {
115                     throw new DGXMLException(activateFile + " is not a valid Activate file Path");
116                 }
117             } else {
118                 throw new DGXMLException("Failed to initialise SvcLogicStore");
119             }
120
121             if (!errors.isEmpty()) {
122                 throw new DGXMLException(errors.toString());
123             }
124         } catch (Exception e) {
125             logger.error("Failed to activade DG", e);
126         }
127     }
128
129     private void tryActivateDG(List<String> errors, List<String> fileLines) {
130         if (fileLines != null) {
131             for (String line : fileLines) {
132                 if (line != null && !line.trim().startsWith("#")) {
133                     String[] lineArray = line.trim().split(":");
134                     doActivateDG(errors, line, lineArray);
135                 }
136             }
137         }
138     }
139
140     private void doActivateDG(List<String> errors, String line, String[] lineArray) {
141         try {
142             if (lineArray != null && lineArray.length >= 4) {
143                 String module = lineArray[0];
144                 String rpc = lineArray[1];
145                 String version = lineArray[2];
146                 String mode = lineArray[3];
147                 if (StringUtils.isNotBlank(module)
148                     && StringUtils.isNotBlank(rpc)
149                     && StringUtils.isNotBlank(version)
150                     && StringUtils.isNotBlank(mode)) {
151                     logger.info("Activating DG :" + line);
152                     SvcLogicGraph graph =
153                         this.store.fetch(module, rpc, version, mode);
154                     tryActivateStore(line, graph);
155                 }
156             }
157         } catch (Exception e) {
158             logger.error("Failed to Activate " + line, e);
159             errors.add("Failed to Activate " + line + ", " + e.getMessage());
160         }
161     }
162
163     private void tryActivateStore(String line, SvcLogicGraph graph) throws SvcLogicException, DGXMLException {
164         if (graph != null) {
165             logger.info("Found Graph :" + line + " Activating ...");
166             store.activate(graph);
167         } else {
168             throw new DGXMLException("Failed to fetch from Database");
169         }
170     }
171
172
173     public static void main(String[] args) {
174         try {
175             String xmlPath;
176             String propertyPath;
177             String activateFile;
178
179             if (args != null && args.length >= 3) {
180                 xmlPath = args[0];
181                 activateFile = args[1];
182                 propertyPath = args[2];
183             } else {
184                 throw new DGXMLException(
185                     "Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
186             }
187
188             DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
189             dgXMLLoadDB.loadDGXMLDir(xmlPath);
190             dgXMLLoadDB.activateDg(activateFile);
191         } catch (Exception e) {
192             logger.error("Arguments missing", e);
193         } finally {
194             System.exit(1);
195         }
196     }
197 }