Update the aai-common with the latest code
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / dbgen / GenTester.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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.aai.dbgen;
22
23 import java.util.Properties;
24
25 import org.openecomp.aai.dbmap.AAIGraph;
26 import org.openecomp.aai.logging.ErrorLogHelper;
27 import org.openecomp.aai.util.AAIConfig;
28 import org.openecomp.aai.util.AAIConstants;
29 import com.att.eelf.configuration.Configuration;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.thinkaurelius.titan.core.TitanGraph;
33 import com.thinkaurelius.titan.core.schema.TitanManagement;
34
35
36
37
38 public class GenTester {
39
40         private static EELFLogger LOGGER;
41         
42         /**
43          * The main method.
44          *
45          * @param args the arguments
46          */
47         public static void main(String[] args) {
48            
49                 TitanGraph graph = null;
50                 
51                 // Set the logging file properties to be used by EELFManager
52                 Properties props = System.getProperties();
53                 props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_CREATE_DB_SCHEMA_LOGBACK_PROPS);
54                 props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES);
55                 LOGGER = EELFManager.getInstance().getLogger(GenTester.class);
56                 boolean addDefaultCR = true;
57                 
58                 try {   
59                         AAIConfig.init();
60                 if (args != null && args.length > 0 ){
61                         if( "genDbRulesOnly".equals(args[0]) ){
62                                 ErrorLogHelper.logError("AAI_3100", 
63                                                 " This option is no longer supported. What was in DbRules is now derived from the OXM files. ");
64                                 return;
65                         }
66                         else if ( "GEN_DB_WITH_NO_SCHEMA".equals(args[0]) ){
67                                 // Note this is done to create an empty DB with no Schema so that
68                                         // an HBase copyTable can be used to set up a copy of the db.
69                                         String imsg = "    ---- NOTE --- about to load a graph without doing any schema processing (takes a little while) --------   ";
70                         System.out.println(imsg);
71                         LOGGER.info(imsg);
72                                         graph = AAIGraph.getInstance().getGraph();
73                                 
74                                if( graph == null ){
75                                            ErrorLogHelper.logError("AAI_5102", "Error creating Titan graph.");
76                                    return;
77                                }
78                                else {
79                                    String amsg = "Successfully loaded a Titan graph without doing any schema work.  ";
80                                    System.out.println(amsg);
81                                    LOGGER.auditEvent(amsg);
82                                    return;
83                                }
84                         } else if ("GEN_DB_WITH_NO_DEFAULT_CR".equals(args[0])) {
85                                 addDefaultCR = false;
86                         }
87                         else {
88                                 ErrorLogHelper.logError("AAI_3000", "Unrecognized argument passed to GenTester.java: [" + args[0] + "]. ");
89                                 
90                                 String emsg = "Unrecognized argument passed to GenTester.java: [" + args[0] + "]. ";
91                                 System.out.println(emsg);
92                                 LOGGER.error(emsg);
93                                 
94                                 emsg = "Either pass no argument for normal processing, or use 'GEN_DB_WITH_NO_SCHEMA'.";
95                                 System.out.println(emsg);
96                                 LOGGER.error(emsg);
97                                 
98                                 return;
99                         }
100                 }
101                 
102                         //AAIConfig.init();
103                         ErrorLogHelper.loadProperties();
104                         String imsg = "    ---- NOTE --- about to open graph (takes a little while)--------;";
105                 System.out.println(imsg);
106                 LOGGER.info(imsg);
107                         graph = AAIGraph.getInstance().getGraph();
108                 
109                         if( graph == null ){
110                                 ErrorLogHelper.logError("AAI_5102", "Error creating Titan graph. ");
111                                 return;
112                         }
113
114                         // Load the propertyKeys, indexes and edge-Labels into the DB
115                         TitanManagement graphMgt = graph.openManagement();
116
117                 imsg = "-- Loading new schema elements into Titan --";
118                 System.out.println(imsg);
119                 LOGGER.info(imsg);
120                 SchemaGenerator.loadSchemaIntoTitan( graph, graphMgt, addDefaultCR );
121
122             } catch(Exception ex) {
123                 ErrorLogHelper.logError("AAI_4000", ex.getMessage());
124             }
125             
126
127             if( graph != null ){
128                     String imsg = "-- graph commit";
129                 System.out.println(imsg);
130                 LOGGER.info(imsg);
131                 graph.tx().commit();
132
133                         imsg = "-- graph shutdown ";
134                 System.out.println(imsg);
135                 LOGGER.info(imsg);
136                 graph.close();
137             }
138             
139             LOGGER.auditEvent("-- all done, if program does not exit, please kill.");
140             System.exit(0);
141     }
142
143 }
144
145