From 4746fb5bc717f9a7f46ac4436c7a591deab8680d Mon Sep 17 00:00:00 2001 From: Justyna Czestochowska Date: Mon, 30 Jul 2018 16:23:44 +0200 Subject: Refactor of DataGrooming main script replaced long if else with switch extracted methods handling wrong or missing arguments replaced AAI_6101 with constant, called diamond operator Change-Id: Ia8e43df0f028617c55c12bea03efab22b7fc1f4b Issue-ID: AAI-1431 Signed-off-by: jczestochowska --- .../main/java/org/onap/aai/dbgen/DataGrooming.java | 212 ++++++++++----------- 1 file changed, 106 insertions(+), 106 deletions(-) (limited to 'aai-core/src/main/java') diff --git a/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java b/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java index 61ef8544..d46aa7ae 100644 --- a/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java +++ b/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java @@ -26,6 +26,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -72,6 +73,7 @@ import org.janusgraph.core.JanusGraph; public class DataGrooming { + public static final String AAI_EXCEPTION_NUMBER = "AAI_6101"; private static EELFLogger logger; private static final String FROMAPPID = "AAI-DB"; private static final String TRANSID = UUID.randomUUID().toString(); @@ -143,101 +145,67 @@ public class DataGrooming { if (args.length > 0) { // They passed some arguments in that will affect processing - for (int i = 0; i < args.length; i++) { - String thisArg = args[i]; - if ("-edgesOnly".equals(thisArg)) { - edgesOnlyFlag = true; - } else if (thisArg.equals("-autoFix")) { - doAutoFix = true; - } else if ("-skipHostCheck".equals(thisArg)) { - skipHostCheck = true; - } else if ("-dontFixOrphans".equals(thisArg)) { - dontFixOrphansFlag = true; - } else if ("-singleCommits".equals(thisArg)) { - singleCommits = true; - } else if ("-dupeCheckOff".equals(thisArg)) { - dupeCheckOff = true; - } else if ("-dupeFixOn".equals(thisArg)) { - dupeFixOn = true; - } else if ("-ghost2CheckOff".equals(thisArg)) { - ghost2CheckOff = true; - } else if ("-neverUseCache".equals(thisArg)) { - neverUseCache = true; - } else if ("-ghost2FixOn".equals(thisArg)) { - ghost2FixOn = true; - } else if ("-skipEdgeChecks".equals(thisArg)) { - skipEdgeCheckFlag = true; - } else if ("-maxFix".equals(thisArg)) { - i++; - if (i >= args.length) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error(" No value passed with -maxFix option. "); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - String nextArg = args[i]; - try { - maxRecordsToFix = Integer.parseInt(nextArg); - } catch (Exception e) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("Bad value passed with -maxFix option: [" - + nextArg + "]"); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - } else if (thisArg.equals("-sleepMinutes")) { - i++; - if (i >= args.length) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("No value passed with -sleepMinutes option."); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - String nextArg = args[i]; - try { - sleepMinutes = Integer.parseInt(nextArg); - } catch (Exception e) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("Bad value passed with -sleepMinutes option: [" - + nextArg + "]"); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - } else if ("-timeWindowMinutes".equals(thisArg)) { - i++; - if (i >= args.length) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("No value passed with -timeWindowMinutes option."); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - String nextArg = args[i]; - try { - timeWindowMinutes = Integer.parseInt(nextArg); - } catch (Exception e) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("Bad value passed with -timeWindowMinutes option: [" - + nextArg + "]"); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - - } else if ("-f".equals(thisArg)) { - i++; - if (i >= args.length) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error(" No value passed with -f option. "); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - prevFileName = args[i]; - } else { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error(" Unrecognized argument passed to DataGrooming: [" - + thisArg + "]. "); - logger.error(" Valid values are: -f -autoFix -maxFix -edgesOnly -skipEdgeChecks -dupeFixOn -donFixOrphans -timeWindowMinutes -sleepMinutes -neverUseCache"); - AAISystemExitUtil.systemExitCloseAAIGraph(0); + + List arguments = Arrays.asList(args); + Iterator argsIterator = arguments.iterator(); + while (argsIterator.hasNext()){ + String argument = argsIterator.next(); + String nextArg; + switch(argument){ + case "-edgesOnly": + edgesOnlyFlag = true; + break; + case "-autoFix": + doAutoFix = true; + break; + case "-skipHostCheck": + skipHostCheck = true; + break; + case "-dontFixOrphans": + dontFixOrphansFlag = true; + break; + case "-singleCommits": + singleCommits = true; + break; + case "-dupeCheckOff": + dupeCheckOff = true; + break; + case "-dupeFixOn": + dupeFixOn = true; + break; + case "-ghost2CheckOff": + ghost2CheckOff = true; + break; + case "-neverUseCache": + neverUseCache = true; + break; + case "-ghost2FixOn": + ghost2FixOn = true; + break; + case "-skipEdgeChecks": + skipEdgeCheckFlag = true; + break; + case "-maxFix": + handleNoPassedArgument(argsIterator, argument); + nextArg = argsIterator.next(); + maxRecordsToFix = handleNumericArgument(maxRecordsToFix, nextArg, argument); + break; + case "-sleepMinutes": + handleNoPassedArgument(argsIterator, argument); + nextArg = argsIterator.next(); + sleepMinutes = handleNumericArgument(sleepMinutes, nextArg, argument); + break; + case "-timeWindowMinutes": + handleNoPassedArgument(argsIterator, argument); + nextArg = argsIterator.next(); + timeWindowMinutes = handleNumericArgument(timeWindowMinutes, nextArg, argument); + break; + case "-f": + handleNoPassedArgument(argsIterator, argument); + prevFileName = argsIterator.next(); + break; + default: + handleUnrecognizedArguments(argument); } } } @@ -349,6 +317,38 @@ public class DataGrooming { }// End of main() + private static void handleUnrecognizedArguments(String argument) { + LoggingContext.statusCode(StatusCode.ERROR); + LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); + logger.error(" Unrecognized argument passed to DataGrooming: [" + + argument + "]. "); + logger.error( + " Valid values are: -f -autoFix -maxFix -edgesOnly -skipEdgeChecks -dupeFixOn -donFixOrphans -timeWindowMinutes -sleepMinutes -neverUseCache"); + AAISystemExitUtil.systemExitCloseAAIGraph(0); + } + + private static int handleNumericArgument(int numericArgumentValue, String nextArg, String argument) { + try { + numericArgumentValue = Integer.parseInt(nextArg); + } catch (Exception e) { + LoggingContext.statusCode(StatusCode.ERROR); + LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); + logger.error("Bad value passed with" + argument + "option: [" + + nextArg + "]"); + AAISystemExitUtil.systemExitCloseAAIGraph(0); + } + return numericArgumentValue; + } + + private static void handleNoPassedArgument(Iterator argsIterator, String argument) { + if (!argsIterator.hasNext()) { + LoggingContext.statusCode(StatusCode.ERROR); + LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); + logger.error("No value passed with" + argument + "option."); + AAISystemExitUtil.systemExitCloseAAIGraph(0); + } + } + /** * Do the grooming. * @@ -449,7 +449,7 @@ public class DataGrooming { } if (graph == null) { String emsg = "null graph object in DataGrooming\n"; - throw new AAIException("AAI_6101", emsg); + throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); } logger.debug(" Got the graph object. "); @@ -457,18 +457,18 @@ public class DataGrooming { g = graph.newTransaction(); if (g == null) { String emsg = "null graphTransaction object in DataGrooming\n"; - throw new AAIException("AAI_6101", emsg); + throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); } GraphTraversalSource source1 = g.traversal(); ArrayList errArr = new ArrayList<>(); int totalNodeCount = 0; - HashMap misMatchedHash = new HashMap(); - HashMap orphanNodeHash = new HashMap(); - HashMap missingDepNodeHash = new HashMap(); - HashMap oneArmedEdgeHash = new HashMap(); - HashMap emptyVertexHash = new HashMap(); - HashMap ghostNodeHash = new HashMap(); + HashMap misMatchedHash = new HashMap<>(); + HashMap orphanNodeHash = new HashMap<>(); + HashMap missingDepNodeHash = new HashMap<>(); + HashMap oneArmedEdgeHash = new HashMap<>(); + HashMap emptyVertexHash = new HashMap<>(); + HashMap ghostNodeHash = new HashMap<>(); ArrayList dupeGroups = new ArrayList<>(); Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST); @@ -494,7 +494,7 @@ public class DataGrooming { // Determine what the key fields are for this nodeType - use an arrayList so they // can be gotten out in a consistent order. Set keyPropsSet = entry.getValue().getKeys(); - ArrayList keyProps = new ArrayList (); + ArrayList keyProps = new ArrayList<>(); keyProps.addAll(keyPropsSet); // Get the types of nodes that this nodetype depends on for uniqueness (if any) @@ -772,14 +772,14 @@ public class DataGrooming { graph2 = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(DataGrooming.class.getSimpleName()).withGraphType("realtime2").buildConfiguration()); if (graph2 == null) { String emsg = "null graph2 object in DataGrooming\n"; - throw new AAIException("AAI_6101", emsg); + throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); } else { logger.debug("Got the graph2 object... \n"); } g2 = graph2.newTransaction(); if (g2 == null) { String emsg = "null graphTransaction2 object in DataGrooming\n"; - throw new AAIException("AAI_6101", emsg); + throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); } ArrayList vertList = new ArrayList<>(); -- cgit 1.2.3-korg