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.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;
41 public class DGXMLLoadNActivate {
43 private static final Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
44 private static final String STRING_ENCODING = "utf-8";
46 private final SvcLogicStore store;
48 public DGXMLLoadNActivate(String propfile) throws DGXMLException, SvcLogicException {
49 if (StringUtils.isBlank(propfile)) {
50 throw new DGXMLException(propfile + " Profile file is not defined");
52 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
55 protected DGXMLLoadNActivate(SvcLogicStore store) {
59 public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
60 if (dgXMLpath != null) {
61 SvcLogicParser.load(dgXMLpath, this.store);
65 private void loadDGXMLDir(String xmlPath) {
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);
77 throw new DGXMLException(xmlPath + " is not a valid XML Directory");
80 throw new DGXMLException("Failed to initialise SvcLogicStore");
83 if (!errors.isEmpty()) {
84 throw new DGXMLException(errors.toString());
86 } catch (Exception e) {
87 logger.error("Failed to load DGXML directories", e);
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());
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());
104 public void activateDg(String activateFilePath) {
106 "******************** Activating DG into Database *****************************");
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);
115 throw new DGXMLException(activateFile + " is not a valid Activate file Path");
118 throw new DGXMLException("Failed to initialise SvcLogicStore");
121 if (!errors.isEmpty()) {
122 throw new DGXMLException(errors.toString());
124 } catch (Exception e) {
125 logger.error("Failed to activade DG", e);
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);
140 private void doActivateDG(List<String> errors, String line, String[] lineArray) {
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);
157 } catch (Exception e) {
158 logger.error("Failed to Activate " + line, e);
159 errors.add("Failed to Activate " + line + ", " + e.getMessage());
163 private void tryActivateStore(String line, SvcLogicGraph graph) throws SvcLogicException, DGXMLException {
165 logger.info("Found Graph :" + line + " Activating ...");
166 store.activate(graph);
168 throw new DGXMLException("Failed to fetch from Database");
173 public static void main(String[] args) {
179 if (args != null && args.length >= 3) {
181 activateFile = args[1];
182 propertyPath = args[2];
184 throw new DGXMLException(
185 "Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <activatefile> <dbPropertyfile>");
188 DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath);
189 dgXMLLoadDB.loadDGXMLDir(xmlPath);
190 dgXMLLoadDB.activateDg(activateFile);
191 } catch (Exception e) {
192 logger.error("Arguments missing", e);