2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdnc.dg.loader;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
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;
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";
43 public DGXMLLoadNActivate(String propfile) throws Exception{
44 if(StringUtils.isBlank(propfile)){
45 throw new Exception(propfile + " Profile file is not defined");
47 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
50 public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException{
51 if(dgXMLpath != null ){
52 SvcLogicParser.load(dgXMLpath, this.store);
56 private void loadDGXMLDir(String xmlPath) throws Exception {
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());
68 SvcLogicParser.load(file.getCanonicalPath(), this.store);
69 }catch (Exception e) {
70 errors.add("Failed to load XML "+file.getCanonicalPath() + ", Exception : "+e.getMessage());
74 throw new Exception(xmlPath + " is not a valid XML Directory");
77 throw new Exception("Failed to initialise SvcLogicStore");
80 if(errors.size() > 0){
81 throw new Exception(errors.toString());
83 } catch (Exception e) {
84 logger.error(e.getMessage());
88 public void activateDg(String activateFilePath) throws Exception {
89 logger.info("******************** Activating DG into Database *****************************");
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(":");
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);
111 logger.info("Found Graph :" + line + " Activating ...");
112 this.store.activate(graph);
114 throw new Exception("Failed to fetch from Database");
118 } catch (Exception e) {
120 errors.add("Failed to Activate "+line + ", "+e.getMessage());
126 throw new Exception(activateFile + " is not a valid Activate file Path");
129 throw new Exception("Failed to initialise SvcLogicStore");
132 if(errors.size() > 0){
133 throw new Exception(errors.toString());
135 } catch (Exception e) {
136 logger.error(e.getMessage());
141 public static void main(String[] args) {
143 String xmlPath = null;
144 String propertyPath = null;
145 String activateFile = null;
147 if(args != null && args.length >= 3){
149 activateFile = args[1];
150 propertyPath = args[2];
152 throw new Exception("Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
155 //propertyPath = "/Users/bs2796/0Source/ecomp/bvc-3.2.2/others/properties/dblib.properties";
156 //xmlPath = DGXMLLoadNActivate.class.getClassLoader().getResource(".").getPath() +"/xml" ;
158 DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
159 dgXMLLoadDB.loadDGXMLDir(xmlPath);
160 dgXMLLoadDB.activateDg(activateFile);
161 } catch (Exception e) {