2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.sdnc.dg.loader;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
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";
47 public DGXMLLoadNActivate(String propfile) throws Exception{
48 if(StringUtils.isBlank(propfile)){
49 throw new Exception(propfile + " Profile file is not defined");
51 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
54 protected DGXMLLoadNActivate(SvcLogicStore store) {
58 public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException{
59 if(dgXMLpath != null ){
60 SvcLogicParser.load(dgXMLpath, this.store);
64 private void loadDGXMLDir(String xmlPath) throws Exception {
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());
76 SvcLogicParser.load(file.getCanonicalPath(), this.store);
77 }catch (Exception e) {
78 errors.add("Failed to load XML "+file.getCanonicalPath() + ", Exception : "+e.getMessage());
82 throw new Exception(xmlPath + " is not a valid XML Directory");
85 throw new Exception("Failed to initialise SvcLogicStore");
88 if(errors.size() > 0){
89 throw new Exception(errors.toString());
91 } catch (Exception e) {
92 logger.error(e.getMessage());
96 public void activateDg(String activateFilePath) throws Exception {
97 logger.info("******************** Activating DG into Database *****************************");
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(":");
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);
119 logger.info("Found Graph :" + line + " Activating ...");
120 this.store.activate(graph);
122 throw new Exception("Failed to fetch from Database");
126 } catch (Exception e) {
128 errors.add("Failed to Activate "+line + ", "+e.getMessage());
134 throw new Exception(activateFile + " is not a valid Activate file Path");
137 throw new Exception("Failed to initialise SvcLogicStore");
140 if(errors.size() > 0){
141 throw new Exception(errors.toString());
143 } catch (Exception e) {
144 logger.error(e.getMessage());
149 public static void main(String[] args) {
151 String xmlPath = null;
152 String propertyPath = null;
153 String activateFile = null;
155 if(args != null && args.length >= 3){
157 activateFile = args[1];
158 propertyPath = args[2];
160 throw new Exception("Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
163 //propertyPath = "/Users/bs2796/0Source/ecomp/bvc-3.2.2/others/properties/dblib.properties";
164 //xmlPath = DGXMLLoadNActivate.class.getClassLoader().getResource(".").getPath() +"/xml" ;
166 DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
167 dgXMLLoadDB.loadDGXMLDir(xmlPath);
168 dgXMLLoadDB.activateDg(activateFile);
169 } catch (Exception e) {