Move dg-loader files to org.onap package
[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.SvcLogicGraph;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40 public class DGXMLActivate {
41
42     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
43     private final SvcLogicStore store;
44     public static String STRING_ENCODING = "utf-8";
45
46     public DGXMLActivate(String propfile) throws Exception {
47         if (StringUtils.isBlank(propfile)) {
48             throw new Exception(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
58     public void activateDg(String activateFilePath) throws Exception {
59         logger.info(
60                 "******************** Activating DG into Database *****************************");
61         try {
62             List<String> errors = new ArrayList<String>();
63             if (this.store != null) {
64                 File activateFile = new File(activateFilePath);
65                 if (activateFile != null && activateFile.isFile()) {
66                     List<String> fileLines = FileUtils.readLines(activateFile, STRING_ENCODING);
67                     if (fileLines != null) {
68                         for (String line : fileLines) {
69                             if (line != null && !line.trim().startsWith("#")) {
70                                 String lineArray[] = line.trim().split(":");
71                                 try {
72                                     if (lineArray != null && lineArray.length >= 4) {
73                                         String module = lineArray[0];
74                                         String rpc = lineArray[1];
75                                         String version = lineArray[2];
76                                         String mode = lineArray[3];
77                                         if (StringUtils.isNotBlank(module)
78                                                 && StringUtils.isNotBlank(rpc)
79                                                 && StringUtils.isNotBlank(version)
80                                                 && StringUtils.isNotBlank(mode)) {
81                                             logger.info("Activating DG :" + line);
82                                             SvcLogicGraph graph =
83                                                     this.store.fetch(module, rpc, version, mode);
84                                             if (graph != null) {
85                                                 logger.info(
86                                                         "Found Graph :" + line + " Activating ...");
87                                                 this.store.activate(graph);
88                                             } else {
89                                                 throw new Exception(
90                                                         "Failed to fetch from Database");
91                                             }
92                                         }
93                                     }
94                                 } catch (Exception e) {
95                                     e.printStackTrace();
96                                     errors.add(
97                                             "Failed to Activate " + line + ", " + e.getMessage());
98                                 }
99                             }
100                         }
101                     }
102                 } else {
103                     throw new Exception(activateFile + " is not a valid Activate file Path");
104                 }
105             } else {
106                 throw new Exception("Failed to initialise SvcLogicStore");
107             }
108
109             if (errors.size() > 0) {
110                 throw new Exception(errors.toString());
111             }
112         } catch (Exception e) {
113             logger.error(e.getMessage());
114         }
115     }
116
117
118     public static void main(String[] args) {
119         try {
120             String activateFile = null;
121             String propertyPath = null;
122
123             if (args != null && args.length >= 2) {
124                 activateFile = args[0];
125                 propertyPath = args[1];
126             } else {
127                 throw new Exception(
128                         "Sufficient inputs for DGXMLActivate are missing <activatefile> <dbPropertyfile>");
129             }
130
131             DGXMLActivate dgXmlActivate = new DGXMLActivate(propertyPath);
132             dgXmlActivate.activateDg(activateFile);
133         } catch (Exception e) {
134             e.printStackTrace();
135         } finally {
136             System.exit(1);
137         }
138     }
139
140 }