77415478a330da6bb60a390703aa6caef1b96c6d
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / onap / sdnc / dg / loader / DGXMLActivate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.dg.loader;
26
27 import java.io.File;
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;
38
39
40 public class DGXMLActivate {
41
42     private static final Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
43     private static final String STRING_ENCODING = "utf-8";
44     private final SvcLogicStore store;
45
46     public DGXMLActivate(String propfile) throws DGXMLException, SvcLogicException {
47         if (StringUtils.isBlank(propfile)) {
48             throw new DGXMLException(propfile + " Profile file is not defined");
49         }
50         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
51     }
52
53     protected DGXMLActivate(SvcLogicStore store) {
54         this.store = store;
55     }
56
57     public void activateDg(String activateFilePath) {
58         logger.info(
59             "******************** Activating DG into Database *****************************");
60         try {
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);
67                 } else {
68                     throw new DGXMLException(activateFile + " is not a valid Activate file Path");
69                 }
70             } else {
71                 throw new DGXMLException("Failed to initialise SvcLogicStore");
72             }
73
74             if (errors.isEmpty()) {
75                 throw new DGXMLException(errors.toString());
76             }
77         } catch (Exception e) {
78             logger.error("Failed to activate DG", e);
79         }
80     }
81
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);
88                 }
89             }
90         }
91     }
92
93     private void doActivateDG(List<String> errors, String line, String[] lineArray) {
94         try {
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);
108                 }
109             }
110         } catch (Exception e) {
111             logger.error("Failed to Activate " + line, e);
112             errors.add("Failed to Activate " + line + ", " + e.getMessage());
113         }
114     }
115
116     private void tryActivateStore(String line, SvcLogicGraph graph) throws SvcLogicException, DGXMLException {
117         if (graph != null) {
118             logger.info(
119                 "Found Graph :" + line + " Activating ...");
120             this.store.activate(graph);
121         } else {
122             throw new DGXMLException("Failed to fetch from Database");
123         }
124     }
125
126     public static void main(String[] args) {
127         try {
128             String activateFile;
129             String propertyPath;
130
131             if (args != null && args.length >= 2) {
132                 activateFile = args[0];
133                 propertyPath = args[1];
134             } else {
135                 throw new DGXMLException(
136                     "Sufficient inputs for DGXMLActivate are missing <activatefile> <dbPropertyfile>");
137             }
138
139             DGXMLActivate dgXmlActivate = new DGXMLActivate(propertyPath);
140             dgXmlActivate.activateDg(activateFile);
141         } catch (Exception e) {
142             logger.error("Arguments missing", e);
143         } finally {
144             System.exit(1);
145         }
146     }
147 }