aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/dbgen/schemamod/SchemaMod.java
blob: 6e48d4ba1e98e24b0ce73959cbd9f438909dfc87 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 * ============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.dbgen.schemamod;

import com.att.eelf.configuration.Configuration;
import org.onap.aai.restclient.PropertyPasswordConfiguration;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.Loader;
import org.onap.aai.introspection.LoaderFactory;
import org.onap.aai.introspection.ModelType;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.serialization.engines.JanusGraphDBEngine;
import org.onap.aai.serialization.engines.QueryStyle;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.setup.SchemaVersions;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.onap.aai.util.ExceptionTranslator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Properties;

public class SchemaMod {

	private final LoaderFactory loaderFactory;

	private final SchemaVersions schemaVersions;
	
	private static boolean historyEnabled;

	private Logger logger = LoggerFactory.getLogger(SchemaMod.class.getSimpleName());
	

    public SchemaMod(LoaderFactory loaderFactory, SchemaVersions schemaVersions){
        this.loaderFactory  = loaderFactory;
        this.schemaVersions = schemaVersions;
	}

	public void execute(String[] args) {

        logger = LoggerFactory.getLogger(SchemaMod.class.getSimpleName());
		
		// NOTE -- We're just working with properties that are used for NODES
		// for now.
		String propName = "";
		String targetDataType = "";
		String targetIndexInfo = "";
		String preserveDataFlag = "";
		String commitBlockSizeStr = "";
		long commitBlockSize = 120000;

		String usageString = "Usage: SchemaMod propertyName targetDataType targetIndexInfo preserveDataFlag [blockSize] \n";
		
		if (args.length == 4) {
			propName = args[0];
			targetDataType = args[1];
			targetIndexInfo = args[2];
			preserveDataFlag = args[3];
		}
		else if (args.length == 5) {
			propName = args[0];
			targetDataType = args[1];
			targetIndexInfo = args[2];
			preserveDataFlag = args[3];
			commitBlockSizeStr = args[4];
		}
		else {
			String emsg = "Incorrect number of Parameters passed.  \n" + usageString;
			logAndPrint(logger, emsg);
			System.exit(1);
		} 
		if (propName.equals("")) {
			String emsg = "Bad parameter - propertyName cannot be empty.  \n" + usageString;
			logAndPrint(logger, emsg);
			System.exit(1);
		} else if (!targetDataType.equals("String") && !targetDataType.equals("Set<String>")
				&& !targetDataType.equals("Integer") && !targetDataType.equals("Long")
				&& !targetDataType.equals("Boolean")) {
			String emsg = "Unsupported targetDataType.  We only support String, Set<String>, Integer, Long or Boolean for now.\n"
					+ usageString;
			logAndPrint(logger, emsg);
			System.exit(1);
		} else if (!targetIndexInfo.equals("uniqueIndex") && !targetIndexInfo.equals("index")
				&& !targetIndexInfo.equals("noIndex")) {
			String emsg = "Unsupported IndexInfo.  We only support: 'uniqueIndex', 'index' or 'noIndex'.\n"
					+ usageString;
			logAndPrint(logger, emsg);
			System.exit(1);
		}
		
		try {
			if( !commitBlockSizeStr.equals("")) {
				// They're over-riding the commitBlockSize
				commitBlockSize = Long.parseLong(commitBlockSizeStr);
			}
		} catch (NumberFormatException nfe) {
			String emsg = "NumberFormatException - Bad block size passed in: [" + commitBlockSizeStr + "]. ";
			logAndPrint(logger, emsg );
			System.exit(1);
		}

		try {
			AAIConfig.init();
			ErrorLogHelper.loadProperties();
		} catch (Exception ae) {
			String emsg = "Problem with either AAIConfig.init() or ErrorLogHelper.LoadProperties(). ";
			logAndPrint(logger, emsg + "[" + ae.getMessage() + "]");
			System.exit(1);
		}

		logAndPrint(logger, ">>> Processing will begin in 5 seconds (unless interrupted). <<<");
		try {
			// Give them a chance to back out of this
			Thread.sleep(5000);
		} catch (java.lang.InterruptedException ie) {
			logAndPrint(logger, " DB Schema Update has been aborted. ");
			System.exit(1);
		}

        logAndPrint(logger, "    ---- NOTE --- about to open graph (takes a little while)\n");

        SchemaVersion version = schemaVersions.getDefaultVersion();
        QueryStyle queryStyle = QueryStyle.TRAVERSAL;
        ModelType introspectorFactoryType = ModelType.MOXY;
        Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
        TransactionalGraphEngine engine = null;
        try {
            engine = new JanusGraphDBEngine(queryStyle, loader);
            SchemaModInternalBatch internal = new SchemaModInternalBatch(engine, logger, propName, targetDataType, targetIndexInfo, Boolean.parseBoolean(preserveDataFlag), commitBlockSize);
            internal.execute();
            engine.startTransaction();
            engine.tx().close();
            logAndPrint(logger, "------ Completed the SchemaMod -------- ");
        } catch (Exception e) {
            String emsg = "Not able to complete the requested SchemaMod \n";
            logAndPrint(logger, e.getMessage());
            logAndPrint(logger, emsg);
            System.exit(1);
        }
	}
	/**
	 * Log and print.
	 *
	 * @param logger the logger
	 * @param msg the msg
	 */
	protected void logAndPrint(Logger logger, String msg) {
		System.out.println(msg);
		logger.debug(msg);
	}

	public static void main(String[] args) throws AAIException {

		Properties props = System.getProperties();
		props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_SCHEMA_MOD_LOGBACK_PROPS);
		props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_BUNDLECONFIG);

		MDC.put("logFilenameAppender", SchemaMod.class.getSimpleName());

		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		PropertyPasswordConfiguration initializer = new PropertyPasswordConfiguration();
		initializer.initialize(ctx);
		try {
			ctx.scan(
					"org.onap.aai.config",
					"org.onap.aai.setup"
			);
			ctx.refresh();
		} catch (Exception e) {
			AAIException aai = ExceptionTranslator.schemaServiceExceptionTranslator(e);
			System.out.println("Problems running SchemaMod "+aai.getMessage());
			ErrorLogHelper.logError(aai.getCode(), e.getMessage() + ", resolve and retry");
			throw aai;
		}
		
		historyEnabled = Boolean.parseBoolean(ctx.getEnvironment().getProperty("history.enabled","false"));
		if( historyEnabled ) {
			String emsg = "Regular SchemaMod may not be used when history.enabled=true. ";
			System.out.println(emsg);
			throw new AAIException("AAI-4005",emsg);
		}
		
		LoaderFactory loaderFactory = ctx.getBean(LoaderFactory.class);
		SchemaVersions schemaVersions = (SchemaVersions) ctx.getBean("schemaVersions");
		SchemaMod schemaMod = new SchemaMod(loaderFactory, schemaVersions);
		schemaMod.execute(args);

		System.exit(0);
	}

}