aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/schema/GenTester.java
blob: ad99dd94bec7f05273b63cb59369402971fa5df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.schema;

import com.att.eelf.configuration.Configuration;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.onap.aai.dbgen.SchemaGenerator;
import org.onap.aai.dbmap.AAIGraph;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.logging.LoggingContext;
import org.onap.aai.logging.LoggingContext.StatusCode;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Properties;
import java.util.UUID;


public class GenTester {

	private static EELFLogger LOGGER;
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
	   
		JanusGraph graph = null;
		System.setProperty("aai.service.name", GenTester.class.getSimpleName());
		// Set the logging file properties to be used by EELFManager
		Properties props = System.getProperties();
		props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_LOGBACK_PROPS);
		props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_BUNDLECONFIG);
		LOGGER = EELFManager.getInstance().getLogger(GenTester.class);
		boolean addDefaultCR = true;
		
		LoggingContext.init();
		LoggingContext.component("DBGenTester");
		LoggingContext.partnerName("AAI-TOOLS");
		LoggingContext.targetEntity("AAI");
		LoggingContext.requestId(UUID.randomUUID().toString());
		LoggingContext.serviceName("AAI");
		LoggingContext.targetServiceName("main");
		LoggingContext.statusCode(StatusCode.COMPLETE);
		LoggingContext.responseCode(LoggingContext.SUCCESS);

		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
				"org.onap.aai.config",
				"org.onap.aai.setup"
		);

		try {
			AAIConfig.init();
	    	if (args != null && args.length > 0 ){
	    		if( "genDbRulesOnly".equals(args[0]) ){
	    			ErrorLogHelper.logError("AAI_3100", 
	    					" This option is no longer supported. What was in DbRules is now derived from the OXM files. ");
	    			return;
	    		}
	    		else if ( "GEN_DB_WITH_NO_SCHEMA".equals(args[0]) ){
		    		// Note this is done to create an empty DB with no Schema so that
					// an HBase copyTable can be used to set up a copy of the db.
					String imsg = "    ---- NOTE --- about to load a graph without doing any schema processing (takes a little while) --------   ";
	            	System.out.println(imsg);
	            	LOGGER.info(imsg);
					graph = AAIGraph.getInstance().getGraph();
			    	
			       if( graph == null ){
					   ErrorLogHelper.logError("AAI_5102", "Error creating JanusGraph graph.");
			           return;
			       }
			       else {
			    	   String amsg = "Successfully loaded a JanusGraph graph without doing any schema work.  ";
			    	   System.out.println(amsg);
			    	   LOGGER.auditEvent(amsg);
			           return;
			       }
	    		} else if ("GEN_DB_WITH_NO_DEFAULT_CR".equals(args[0])) {
	    			addDefaultCR = false;
	    		}
	    		else {
	    			ErrorLogHelper.logError("AAI_3000", "Unrecognized argument passed to GenTester.java: [" + args[0] + "]. ");
	    			
	    			String emsg = "Unrecognized argument passed to GenTester.java: [" + args[0] + "]. ";
	    			System.out.println(emsg);
	    			LoggingContext.statusCode(StatusCode.ERROR);
	    			LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR);
	    			LOGGER.error(emsg);
	    			
	    			emsg = "Either pass no argument for normal processing, or use 'GEN_DB_WITH_NO_SCHEMA'.";
	    			System.out.println(emsg);
	    			LOGGER.error(emsg);
	    			
	    			return;
	    		}
	    	}
	    	
			//AAIConfig.init();
			ErrorLogHelper.loadProperties();
			String imsg = "    ---- NOTE --- about to open graph (takes a little while)--------;";
        	System.out.println(imsg);
        	LOGGER.info(imsg);
			graph = AAIGraph.getInstance().getGraph();
	    	
			if( graph == null ){
				ErrorLogHelper.logError("AAI_5102", "Error creating JanusGraph graph. ");
				return;
			}

			// Load the propertyKeys, indexes and edge-Labels into the DB
			JanusGraphManagement graphMgt = graph.openManagement();

            imsg = "-- Loading new schema elements into JanusGraph --";
            System.out.println(imsg);
            LOGGER.info(imsg);
            SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, null);
			if( graph != null ){
				imsg = "-- graph commit";
				System.out.println(imsg);
				LOGGER.info(imsg);
				graph.tx().commit();

				imsg = "-- graph shutdown ";
				System.out.println(imsg);
				LOGGER.info(imsg);
				graph.close();
			}
	    } catch(Exception ex) {
            ErrorLogHelper.logError("AAI_4000", ex.getMessage());
            System.exit(1);
	    }

	    LOGGER.auditEvent("-- all done, if program does not exit, please kill.");
	    System.exit(0);
    }

}