b37400ba726271ff79bb6f6905e05730112dc5b7
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / openecomp / sdnc / dg / loader / DGXMLActivate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.dg.loader;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.openecomp.sdnc.sli.SvcLogicGraph;
30 import org.openecomp.sdnc.sli.SvcLogicStore;
31 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37 public class DGXMLActivate {
38
39     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
40     private final SvcLogicStore store;
41     public static String STRING_ENCODING = "utf-8";
42
43     public DGXMLActivate(String propfile) throws Exception{
44         if(StringUtils.isBlank(propfile)){
45             throw new Exception(propfile + " Profile file is not defined");
46         }
47         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
48     }
49
50
51     public void activateDg(String activateFilePath) throws Exception {
52         logger.info("******************** Activating DG into Database *****************************");
53         try {
54             List<String> errors = new ArrayList<String>();
55             if(this.store != null){
56                 File activateFile = new File(activateFilePath);
57                 if(activateFile != null && activateFile.isFile()){
58                     List<String> fileLines = FileUtils.readLines(activateFile,STRING_ENCODING);
59                     if(fileLines != null ){
60                         for (String line : fileLines) {
61                             if(line != null && ! line.trim().startsWith("#")){
62                                 String lineArray[] = line.trim().split(":");
63                                 try {
64                                     if(lineArray != null && lineArray.length >= 4){
65                                         String module = lineArray[0];
66                                         String rpc = lineArray[1];
67                                         String version = lineArray[2];
68                                         String mode = lineArray[3];
69                                         if(StringUtils.isNotBlank(module) && StringUtils.isNotBlank(rpc)
70                                                 && StringUtils.isNotBlank(version) && StringUtils.isNotBlank(mode)){
71                                             logger.info("Activating DG :" + line);
72                                             SvcLogicGraph graph = this.store.fetch(module, rpc, version, mode);
73                                             if(graph != null){
74                                                 logger.info("Found Graph :" + line + " Activating ...");
75                                                 this.store.activate(graph);
76                                             }else{
77                                                 throw new Exception("Failed to fetch from Database");
78                                             }
79                                         }
80                                     }
81                                 } catch (Exception e) {
82                                     e.printStackTrace();
83                                     errors.add("Failed to Activate "+line + ", "+e.getMessage());
84                                 }
85                             }
86                         }
87                     }
88                 }else{
89                     throw new Exception(activateFile + " is not a valid Activate file Path");
90                 }
91             }else{
92                 throw new Exception("Failed to initialise SvcLogicStore");
93             }
94
95             if(errors.size() > 0){
96                 throw new Exception(errors.toString());
97             }
98         } catch (Exception e) {
99             logger.error(e.getMessage());
100         }
101     }
102
103
104     public static void main(String[] args) {
105         try {
106             String activateFile = null;
107             String propertyPath = null;
108
109             if(args != null && args.length >= 2){
110                 activateFile = args[0];
111                 propertyPath = args[1];
112             }else{
113                 throw new Exception("Sufficient inputs for DGXMLActivate are missing <activatefile> <dbPropertyfile>");
114             }
115
116             DGXMLActivate dgXmlActivate = new DGXMLActivate(propertyPath);
117             dgXmlActivate.activateDg(activateFile);
118         } catch (Exception e) {
119             e.printStackTrace();
120         }finally {
121             System.exit(1);
122         }
123     }
124
125 }