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.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class DGXMLActivate {
42 private static final Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
43 private static final String STRING_ENCODING = "utf-8";
44 private final SvcLogicStore store;
46 public DGXMLActivate(String propfile) throws DGXMLException, SvcLogicException {
47 if (StringUtils.isBlank(propfile)) {
48 throw new DGXMLException(propfile + " Profile file is not defined");
50 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
53 protected DGXMLActivate(SvcLogicStore store) {
57 public void activateDg(String activateFilePath) {
59 "******************** Activating DG into Database *****************************");
61 List<String> errors = new ArrayList<>();
62 if (this.store != null) {
63 File activateFile = new File(activateFilePath);
64 if (activateFile.isFile()) {
65 List<String> fileLines = FileUtils.readLines(activateFile, STRING_ENCODING);
66 tryActivateDG(errors, fileLines);
68 throw new DGXMLException(activateFile + " is not a valid Activate file Path");
71 throw new DGXMLException("Failed to initialise SvcLogicStore");
74 if (errors.isEmpty()) {
75 throw new DGXMLException(errors.toString());
77 } catch (Exception e) {
78 logger.error("Failed to activate DG", e);
82 private void tryActivateDG(List<String> errors, List<String> fileLines) {
83 if (fileLines != null) {
84 for (String line : fileLines) {
85 if (line != null && !line.trim().startsWith("#")) {
86 String[] lineArray = line.trim().split(":");
87 doActivateDG(errors, line, lineArray);
93 private void doActivateDG(List<String> errors, String line, String[] lineArray) {
95 if (lineArray.length >= 4) {
96 String module = lineArray[0];
97 String rpc = lineArray[1];
98 String version = lineArray[2];
99 String mode = lineArray[3];
100 if (StringUtils.isNotBlank(module)
101 && StringUtils.isNotBlank(rpc)
102 && StringUtils.isNotBlank(version)
103 && StringUtils.isNotBlank(mode)) {
104 logger.info("Activating DG :" + line);
105 SvcLogicGraph graph =
106 this.store.fetch(module, rpc, version, mode);
107 tryActivateStore(line, graph);
110 } catch (Exception e) {
111 logger.error("Failed to Activate " + line, e);
112 errors.add("Failed to Activate " + line + ", " + e.getMessage());
116 private void tryActivateStore(String line, SvcLogicGraph graph) throws SvcLogicException, DGXMLException {
119 "Found Graph :" + line + " Activating ...");
120 this.store.activate(graph);
122 throw new DGXMLException("Failed to fetch from Database");
126 public static void main(String[] args) {
131 if (args != null && args.length >= 2) {
132 activateFile = args[0];
133 propertyPath = args[1];
135 throw new DGXMLException(
136 "Sufficient inputs for DGXMLActivate are missing <activatefile> <dbPropertyfile>");
139 DGXMLActivate dgXmlActivate = new DGXMLActivate(propertyPath);
140 dgXmlActivate.activateDg(activateFile);
141 } catch (Exception e) {
142 logger.error("Arguments missing", e);