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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/**
* ============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.migration;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.activemq.broker.BrokerService;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.dbmap.AAIGraph;
import org.onap.aai.dbmap.DBConnectionType;
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.introspection.Version;
import org.onap.aai.logging.LoggingContext;
import org.onap.aai.logging.LoggingContext.StatusCode;
import org.onap.aai.serialization.engines.QueryStyle;
import org.onap.aai.serialization.engines.JanusGraphDBEngine;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.util.AAIConstants;
import org.onap.aai.util.FormatDate;
import org.reflections.Reflections;
import org.slf4j.MDC;
import com.att.eelf.configuration.Configuration;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
/**
* Runs a series of migrations from a defined directory based on the presence of
* the {@link org.onap.aai.migration.Enabled Enabled} annotation
*
* It will also write a record of the migrations run to the database.
*/
public class MigrationControllerInternal {
private EELFLogger logger;
private final int DANGER_ZONE = 10;
private static final String VERTEX_TYPE = "migration-list-" + Version.getLatest().toString();
private final List<String> resultsSummary = new ArrayList<>();
private BrokerService broker;
private final List<NotificationHelper> notifications = new ArrayList<>();
private static final String SNAPSHOT_LOCATION = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "logs" + AAIConstants.AAI_FILESEP + "data" + AAIConstants.AAI_FILESEP + "migrationSnapshots";
/**
* The main method.
*
* @param args
* the arguments
*/
public void run(String[] args) {
// Set the logging file properties to be used by EELFManager
System.setProperty("aai.service.name", MigrationController.class.getSimpleName());
Properties props = System.getProperties();
props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, "migration-logback.xml");
props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES);
logger = EELFManager.getInstance().getLogger(MigrationControllerInternal.class.getSimpleName());
MDC.put("logFilenameAppender", MigrationController.class.getSimpleName());
boolean loadSnapshot = false;
CommandLineArgs cArgs = new CommandLineArgs();
JCommander jCommander = new JCommander(cArgs, args);
jCommander.setProgramName(MigrationController.class.getSimpleName());
// Set flag to load from snapshot based on the presence of snapshot and
// graph storage backend of inmemory
if (cArgs.dataSnapshot != null && !cArgs.dataSnapshot.isEmpty()) {
try {
PropertiesConfiguration config = new PropertiesConfiguration(cArgs.config);
if (config.getString("storage.backend").equals("inmemory")) {
loadSnapshot = true;
System.setProperty("load.snapshot.file", "true");
System.setProperty("snapshot.location", cArgs.dataSnapshot);
}
} catch (ConfigurationException e) {
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.DATA_ERROR);
logAndPrint("ERROR: Could not load janusgraph configuration.\n" + ExceptionUtils.getFullStackTrace(e));
return;
}
}
System.setProperty("realtime.db.config", cArgs.config);
logAndPrint("\n\n---------- Connecting to Graph ----------");
AAIGraph.getInstance();
logAndPrint("---------- Connection Established ----------");
Version version = AAIProperties.LATEST;
QueryStyle queryStyle = QueryStyle.TRAVERSAL;
ModelType introspectorFactoryType = ModelType.MOXY;
Loader loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
TransactionalGraphEngine engine = new JanusGraphDBEngine(queryStyle, DBConnectionType.REALTIME, loader);
if (cArgs.help) {
jCommander.usage();
engine.rollback();
return;
}
Reflections reflections = new Reflections("org.onap.aai.migration");
List<Class<? extends Migrator>> migratorClasses = new ArrayList<>(findClasses(reflections));
//Displays list of migration classes which needs to be executed.Pass flag "-l" following by the class names
if (cArgs.list) {
listMigrationWithStatus(cArgs, migratorClasses, engine);
return;
}
logAndPrint("---------- Looking for migration scripts to be executed. ----------");
//Excluding any migration class when run migration from script.Pass flag "-e" following by the class names
if (!cArgs.excludeClasses.isEmpty()) {
migratorClasses = filterMigrationClasses(cArgs.excludeClasses, migratorClasses);
listMigrationWithStatus(cArgs, migratorClasses, engine);
}
List<Class<? extends Migrator>> migratorClassesToRun = createMigratorList(cArgs, migratorClasses);
sortList(migratorClassesToRun);
if (!cArgs.scripts.isEmpty() && migratorClassesToRun.isEmpty()) {
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR);
logAndPrint("\tERROR: Failed to find migrations " + cArgs.scripts + ".");
logAndPrint("---------- Done ----------");
LoggingContext.successStatusFields();
}
logAndPrint("\tFound " + migratorClassesToRun.size() + " migration scripts.");
logAndPrint("---------- Executing Migration Scripts ----------");
if (!cArgs.skipPreMigrationSnapShot) {
takePreSnapshotIfRequired(engine, cArgs, migratorClassesToRun);
}
for (Class<? extends Migrator> migratorClass : migratorClassesToRun) {
String name = migratorClass.getSimpleName();
Migrator migrator;
if (migratorClass.isAnnotationPresent(Enabled.class)) {
try {
engine.startTransaction();
if (!cArgs.forced && hasAlreadyRun(name, engine)) {
logAndPrint("Migration " + name + " has already been run on this database and will not be executed again. Use -f to force execution");
continue;
}
migrator = migratorClass.getConstructor(TransactionalGraphEngine.class).newInstance(engine);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.DATA_ERROR);
logAndPrint("EXCEPTION caught initalizing migration class " + migratorClass.getSimpleName() + ".\n" + ExceptionUtils.getFullStackTrace(e));
LoggingContext.successStatusFields();
engine.rollback();
continue;
}
logAndPrint("\tRunning " + migratorClass.getSimpleName() + " migration script.");
logAndPrint("\t\t See " + System.getProperty("AJSC_HOME") + "/logs/migration/" + migratorClass.getSimpleName() + "/* for logs.");
MDC.put("logFilenameAppender", migratorClass.getSimpleName() + "/" + migratorClass.getSimpleName());
migrator.run();
commitChanges(engine, migrator, cArgs);
} else {
logAndPrint("\tSkipping " + migratorClass.getSimpleName() + " migration script because it has been disabled.");
}
}
MDC.put("logFilenameAppender", MigrationController.class.getSimpleName());
for (NotificationHelper notificationHelper : notifications) {
try {
notificationHelper.triggerEvents();
} catch (AAIException e) {
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR);
logAndPrint("\tcould not event");
logger.error("could not event", e);
LoggingContext.successStatusFields();
}
}
logAndPrint("---------- Done ----------");
// Save post migration snapshot if snapshot was loaded
if (!cArgs.skipPostMigrationSnapShot) {
generateSnapshot(engine, "post");
}
outputResultsSummary();
}
/**
* This method is used to remove excluded classes from migration from the
* script command.
*
* @param excludeClasses
* : Classes to be removed from Migration
* @param migratorClasses
* : Classes to execute migration.
* @return
*/
private List<Class<? extends Migrator>> filterMigrationClasses(
List<String> excludeClasses,
List<Class<? extends Migrator>> migratorClasses) {
List<Class<? extends Migrator>> filteredMigratorClasses = migratorClasses
.stream()
.filter(migratorClass -> !excludeClasses.contains(migratorClass
.getSimpleName())).collect(Collectors.toList());
return filteredMigratorClasses;
}
private void listMigrationWithStatus(CommandLineArgs cArgs,
List<Class<? extends Migrator>> migratorClasses, TransactionalGraphEngine engine) {
sortList(migratorClasses);
engine.startTransaction();
System.out.println("---------- List of all migrations ----------");
migratorClasses.forEach(migratorClass -> {
boolean enabledAnnotation = migratorClass.isAnnotationPresent(Enabled.class);
String enabled = enabledAnnotation ? "Enabled" : "Disabled";
StringBuilder sb = new StringBuilder();
sb.append(migratorClass.getSimpleName());
sb.append(" in package ");
sb.append(migratorClass.getPackage().getName().substring(migratorClass.getPackage().getName().lastIndexOf('.')+1));
sb.append(" is ");
sb.append(enabled);
sb.append(" ");
sb.append("[" + getDbStatus(migratorClass.getSimpleName(), engine) + "]");
System.out.println(sb.toString());
});
engine.rollback();
System.out.println("---------- Done ----------");
}
private String getDbStatus(String name, TransactionalGraphEngine engine) {
if (hasAlreadyRun(name, engine)) {
return "Already executed in this env";
}
return "Will be run on next execution if Enabled";
}
private boolean hasAlreadyRun(String name, TransactionalGraphEngine engine) {
return engine.asAdmin().getReadOnlyTraversalSource().V().has(AAIProperties.NODE_TYPE, VERTEX_TYPE).has(name, true).hasNext();
}
private Set<Class<? extends Migrator>> findClasses(Reflections reflections) {
Set<Class<? extends Migrator>> migratorClasses = reflections.getSubTypesOf(Migrator.class);
/*
* TODO- Change this to make sure only classes in the specific $release are added in the runList
* Or add a annotation like exclude which folks again need to remember to add ??
*/
migratorClasses.remove(PropertyMigrator.class);
migratorClasses.remove(EdgeMigrator.class);
return migratorClasses;
}
private void takePreSnapshotIfRequired(TransactionalGraphEngine engine, CommandLineArgs cArgs, List<Class<? extends Migrator>> migratorClassesToRun) {
/*int sum = 0;
for (Class<? extends Migrator> migratorClass : migratorClassesToRun) {
if (migratorClass.isAnnotationPresent(Enabled.class)) {
sum += migratorClass.getAnnotation(MigrationPriority.class).value();
}
}
if (sum >= DANGER_ZONE) {
logAndPrint("Entered Danger Zone. Taking snapshot.");
}*/
//always take snapshot for now
generateSnapshot(engine, "pre");
}
private List<Class<? extends Migrator>> createMigratorList(CommandLineArgs cArgs,
List<Class<? extends Migrator>> migratorClasses) {
List<Class<? extends Migrator>> migratorClassesToRun = new ArrayList<>();
for (Class<? extends Migrator> migratorClass : migratorClasses) {
if (!cArgs.scripts.isEmpty() && !cArgs.scripts.contains(migratorClass.getSimpleName())) {
continue;
} else {
migratorClassesToRun.add(migratorClass);
}
}
return migratorClassesToRun;
}
private void sortList(List<Class<? extends Migrator>> migratorClasses) {
Collections.sort(migratorClasses, (m1, m2) -> {
try {
if (m1.getAnnotation(MigrationPriority.class).value() > m2.getAnnotation(MigrationPriority.class).value()) {
return 1;
} else if (m1.getAnnotation(MigrationPriority.class).value() < m2.getAnnotation(MigrationPriority.class).value()) {
return -1;
} else {
return m1.getSimpleName().compareTo(m2.getSimpleName());
}
} catch (Exception e) {
return 0;
}
});
}
private void generateSnapshot(TransactionalGraphEngine engine, String phase) {
FormatDate fd = new FormatDate("yyyyMMddHHmm", "GMT");
String dateStr= fd.getDateTime();
String fileName = SNAPSHOT_LOCATION + File.separator + phase + "Migration." + dateStr + ".graphson";
logAndPrint("Saving snapshot of inmemory graph " + phase + " migration to " + fileName);
Graph transaction = null;
try {
Path pathToFile = Paths.get(fileName);
if (!pathToFile.toFile().exists()) {
Files.createDirectories(pathToFile.getParent());
}
transaction = engine.startTransaction();
transaction.io(IoCore.graphson()).writeGraph(fileName);
engine.rollback();
} catch (IOException e) {
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR);
logAndPrint("ERROR: Could not write in memory graph to " + phase + "Migration file. \n" + ExceptionUtils.getFullStackTrace(e));
LoggingContext.successStatusFields();
engine.rollback();
}
logAndPrint( phase + " migration snapshot saved to " + fileName);
}
/**
* Log and print.
*
* @param msg
* the msg
*/
protected void logAndPrint(String msg) {
System.out.println(msg);
logger.info(msg);
}
/**
* Commit changes.
*
* @param engine
* the graph transaction
* @param migrator
* the migrator
* @param cArgs
*/
protected void commitChanges(TransactionalGraphEngine engine, Migrator migrator, CommandLineArgs cArgs) {
String simpleName = migrator.getClass().getSimpleName();
String message;
if (migrator.getStatus().equals(Status.FAILURE)) {
message = "Migration " + simpleName + " Failed. Rolling back.";
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.DATA_ERROR);
logAndPrint("\t" + message);
LoggingContext.successStatusFields();
migrator.rollback();
} else if (migrator.getStatus().equals(Status.CHECK_LOGS)) {
message = "Migration " + simpleName + " encountered an anomaly, check logs. Rolling back.";
LoggingContext.statusCode(StatusCode.ERROR);
LoggingContext.responseCode(LoggingContext.DATA_ERROR);
logAndPrint("\t" + message);
LoggingContext.successStatusFields();
migrator.rollback();
} else {
MDC.put("logFilenameAppender", simpleName + "/" + simpleName);
if (cArgs.commit) {
if (!engine.asAdmin().getTraversalSource().V().has(AAIProperties.NODE_TYPE, VERTEX_TYPE).hasNext()) {
engine.asAdmin().getTraversalSource().addV(AAIProperties.NODE_TYPE, VERTEX_TYPE).iterate();
}
engine.asAdmin().getTraversalSource().V().has(AAIProperties.NODE_TYPE, VERTEX_TYPE)
.property(simpleName, true).iterate();
MDC.put("logFilenameAppender", MigrationController.class.getSimpleName());
notifications.add(migrator.getNotificationHelper());
migrator.commit();
message = "Migration " + simpleName + " Succeeded. Changes Committed.";
logAndPrint("\t"+ message +"\t");
} else {
message = "--commit not specified. Not committing changes for " + simpleName + " to database.";
logAndPrint("\t" + message);
migrator.rollback();
}
}
resultsSummary.add(message);
}
private void outputResultsSummary() {
logAndPrint("---------------------------------");
logAndPrint("-------------Summary-------------");
for (String result : resultsSummary) {
logAndPrint(result);
}
logAndPrint("---------------------------------");
logAndPrint("---------------------------------");
}
}
class CommandLineArgs {
@Parameter(names = "--help", help = true)
public boolean help;
@Parameter(names = "-c", description = "location of configuration file")
public String config;
@Parameter(names = "-m", description = "names of migration scripts")
public List<String> scripts = new ArrayList<>();
@Parameter(names = "-l", description = "list the status of migrations")
public boolean list = false;
@Parameter(names = "-d", description = "location of data snapshot", hidden = true)
public String dataSnapshot;
@Parameter(names = "-f", description = "force migrations to be rerun")
public boolean forced = false;
@Parameter(names = "--commit", description = "commit changes to graph")
public boolean commit = false;
@Parameter(names = "-e", description = "exclude list of migrator classes")
public List<String> excludeClasses = new ArrayList<>();
@Parameter(names = "--skipPreMigrationSnapShot", description = "skips taking the PRE migration snapshot")
public boolean skipPreMigrationSnapShot = false;
@Parameter(names = "--skipPostMigrationSnapShot", description = "skips taking the POST migration snapshot")
public boolean skipPostMigrationSnapShot = false;
}
|