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