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