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