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.onap.sdnc.dg.loader;
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;
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";
45 public DGXMLLoadNActivate(String propfile) throws Exception {
46 if (StringUtils.isBlank(propfile)) {
47 throw new Exception(propfile + " Profile file is not defined");
49 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
52 protected DGXMLLoadNActivate(SvcLogicStore store) {
56 public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
57 if (dgXMLpath != null) {
58 SvcLogicParser.load(dgXMLpath, this.store);
62 private void loadDGXMLDir(String xmlPath) throws Exception {
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());
75 SvcLogicParser.load(file.getCanonicalPath(), this.store);
76 } catch (Exception e) {
77 errors.add("Failed to load XML " + file.getCanonicalPath()
78 + ", 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 {
98 "******************** Activating DG into Database *****************************");
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(":");
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);
124 "Found Graph :" + line + " Activating ...");
125 this.store.activate(graph);
128 "Failed to fetch from Database");
132 } catch (Exception e) {
135 "Failed to Activate " + line + ", " + e.getMessage());
141 throw new Exception(activateFile + " is not a valid Activate file Path");
144 throw new Exception("Failed to initialise SvcLogicStore");
147 if (errors.size() > 0) {
148 throw new Exception(errors.toString());
150 } catch (Exception e) {
151 logger.error(e.getMessage());
156 public static void main(String[] args) {
158 String xmlPath = null;
159 String propertyPath = null;
160 String activateFile = null;
162 if (args != null && args.length >= 3) {
164 activateFile = args[1];
165 propertyPath = args[2];
168 "Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
171 DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
172 dgXMLLoadDB.loadDGXMLDir(xmlPath);
173 dgXMLLoadDB.activateDg(activateFile);
174 } catch (Exception e) {