From 7ec7a06367d022df3dee7b1f8f89ba0783d6cfba Mon Sep 17 00:00:00 2001 From: "Sonsino, Ofir (os0695)" Date: Wed, 21 Nov 2018 11:22:09 +0200 Subject: asdctool code quality improvements Change-Id: I443c9bb288d123d10bcc2f1f58f540206fb947fc Issue-ID: SDC-1950 Signed-off-by: Sonsino, Ofir (os0695) --- .../main/java/org/openecomp/sdc/asdctool/App.java | 1 - .../java/org/openecomp/sdc/asdctool/Utils.java | 4 +- .../sdc/asdctool/impl/ArtifactUuidFix.java | 32 +++++++----- .../openecomp/sdc/asdctool/impl/DataMigration.java | 5 +- .../sdc/asdctool/impl/GraphMLConverter.java | 60 ++++++++++++++-------- .../sdc/asdctool/impl/GraphMLDataAnalyzer.java | 2 +- .../openecomp/sdc/asdctool/impl/ProductLogic.java | 6 +-- .../org/openecomp/sdc/asdctool/impl/RestUtils.java | 2 +- 8 files changed, 64 insertions(+), 48 deletions(-) (limited to 'asdctool/src') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/App.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/App.java index 7d1104005d..f7aaa1e0d9 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/App.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/App.java @@ -23,7 +23,6 @@ package org.openecomp.sdc.asdctool; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.openecomp.sdc.asdctool.main.ArtifactUUIDFixMenu; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java index ff0f84787d..5c7d78e3f2 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/Utils.java @@ -92,7 +92,7 @@ public class Utils { } } - if (false == leftValue.equals(rightValue)) { + if (!leftValue.equals(rightValue)) { log.trace("The value of key {} is differnet between properties. {} vs {}",key,leftValue,rightValue); return false; } @@ -105,7 +105,7 @@ public class Utils { public static void setProperties(Element element, Map properties) { - if (properties != null && false == properties.isEmpty()) { + if (properties != null && !properties.isEmpty()) { Object[] propertyKeyValues = new Object[properties.size() * 2]; int i = 0; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java index c935265ccc..8c704357ff 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ArtifactUuidFix.java @@ -63,7 +63,13 @@ import java.util.stream.Collectors; @org.springframework.stereotype.Component("artifactUuidFix") public class ArtifactUuidFix { + + private static final String MIGRATION1707_ARTIFACT_UUID_FIX = "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} "; + + private static final String FAILED_TO_FETCH_VF_RESOURCES = "Failed to fetch vf resources "; + private static final String UTF8 = "utf-8"; + @Autowired private TitanDao titanDao; @@ -102,7 +108,7 @@ public class ArtifactUuidFix { } if ("service_vf".equals(runMode) || "fix".equals(runMode)) { log.info("Mode {}. Find problem VFs", runMode); - if (fetchVf(serviceList, vfLst, time) == false) { + if (!fetchVf(serviceList, vfLst, time)) { log.info("Mode {}. Find problem VFs finished with failure", runMode); return false; } @@ -110,7 +116,7 @@ public class ArtifactUuidFix { } if ("fix".equals(runMode) || "fix_only_services".equals(runMode)) { log.info("Mode {}. Start fix", runMode); - if (fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca) == false) { + if (!fix(vfLst, serviceList, nodeToFixTosca, vfToFixTosca, serviceToFixTosca)) { log.info("Mode {}. Fix finished with failure", runMode); return false; } @@ -123,7 +129,7 @@ public class ArtifactUuidFix { private boolean fetchFaultVf(String fixComponent, List vfLst, long time) { log.info("Find fault VF "); String fileName = "fault_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { + try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { writer.write("vf name, vf id, state, version\n"); Map hasProps = new EnumMap<>(GraphPropertyEnum.class); @@ -180,7 +186,7 @@ public class ArtifactUuidFix { } } catch (Exception e) { - log.info("Failed to fetch vf resources ", e); + log.info(FAILED_TO_FETCH_VF_RESOURCES, e); return false; } finally { titanDao.commit(); @@ -195,7 +201,7 @@ public class ArtifactUuidFix { return true; } String fileName = "problemVf_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { + try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { writer.write("vf name, vf id, state, version, example service name\n"); Set vfIds = new HashSet<>(); for (Service service : serviceList) { @@ -239,7 +245,7 @@ public class ArtifactUuidFix { private boolean fetchServices(String fixServices, List serviceList, long time) { log.info("Find problem Services {}", fixServices); String fileName = "problemService_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { + try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { writer.write("service name, service id, state, version\n"); Map hasProps = new EnumMap<>(GraphPropertyEnum.class); @@ -571,7 +577,7 @@ public class ArtifactUuidFix { long time = System.currentTimeMillis(); String fileName = "FailedGenerateTosca" + "_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { + try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { writer.write("componentType, name, version, UID, UUID, invariantUUID, state\n"); List failedList = new ArrayList<>(); @@ -899,7 +905,7 @@ public class ArtifactUuidFix { } if (isAddToGroup) { log.debug( - "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", + MIGRATION1707_ARTIFACT_UUID_FIX, group.getName(), correctArtifactId, correctArtifactUUID); group.getArtifacts().add(correctArtifactId); if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { @@ -908,7 +914,7 @@ public class ArtifactUuidFix { } } else { log.debug( - "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", + MIGRATION1707_ARTIFACT_UUID_FIX, group.getName(), correctArtifactId, correctArtifactUUID); Set tmpSet = new HashSet<>(group.getGroupInstanceArtifacts()); tmpSet.add(correctArtifactId); @@ -987,7 +993,7 @@ public class ArtifactUuidFix { } if (isAddToGroup) { log.debug( - "Migration1707ArtifactUuidFix fix group: group name {} correct artifactId {} artifactUUID {} ", + MIGRATION1707_ARTIFACT_UUID_FIX, group.getName(), correctArtifactId, correctArtifactUUID); group.getArtifacts().add(correctArtifactId); if (correctArtifactUUID != null && !correctArtifactUUID.isEmpty()) { @@ -1106,7 +1112,7 @@ public class ArtifactUuidFix { }); } catch (Exception e) { - log.info("Failed to fetch vf resources ", e); + log.info(FAILED_TO_FETCH_VF_RESOURCES, e); } finally { titanDao.commit(); @@ -1120,7 +1126,7 @@ public class ArtifactUuidFix { boolean result = true; long time = System.currentTimeMillis(); String fileName = name + "_" + time + ".csv"; - try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { + try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), UTF8))) { writer.write("name, UUID, invariantUUID, state, version\n"); for (Map.Entry> entry : vertices.entrySet()) { List compList = entry.getValue(); @@ -1165,7 +1171,7 @@ public class ArtifactUuidFix { } } catch (Exception e) { - log.info("Failed to fetch vf resources ", e); + log.info(FAILED_TO_FETCH_VF_RESOURCES, e); return false; } finally { titanDao.commit(); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java index fddcab1e9f..48421639bf 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/DataMigration.java @@ -131,11 +131,8 @@ public class DataMigration { } } } - if (importToCassandra && !importToCassndra(files)) { - return false; - } - return true; + return !importToCassandra || importToCassndra(files); } private boolean initEsClient() { diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java index 2804a728f0..d43b3b8fcd 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java @@ -57,7 +57,23 @@ import java.util.Map.Entry; public class GraphMLConverter { - private static Logger log = Logger.getLogger(GraphMLConverter.class.getName()); + private static final String FROM_VERTEX = "fromVertex={}"; + + private static final String STORAGE_BACKEND = "storage.backend"; + + private static final String INMEMORY = "inmemory"; + + private static final String CLOSE_FILE_OUTPUT_STREAM_FAILED = "close FileOutputStream failed - {}"; + + private static final String EXPORT_GRAPH = "exportGraph."; + + private static final String DOT_JSON = ".json"; + + private static final String EXPORTED_FILE = "Exported file="; + + private static final String NODE_LABEL = "nodeLabel"; + + private static Logger log = Logger.getLogger(GraphMLConverter.class.getName()); private Gson gson = new Gson(); @@ -71,10 +87,10 @@ public class GraphMLConverter { List> propertiesCriteriaToDelete = new ArrayList<>(); ImmutablePair immutablePair1 = new ImmutablePair<>("healthcheckis", "GOOD"); - ImmutablePair immutablePair2 = new ImmutablePair<>("nodeLabel", "user"); - ImmutablePair immutablePair3 = new ImmutablePair<>("nodeLabel", + ImmutablePair immutablePair2 = new ImmutablePair<>(NODE_LABEL, "user"); + ImmutablePair immutablePair3 = new ImmutablePair<>(NODE_LABEL, "resourceCategory"); - ImmutablePair immutablePair4 = new ImmutablePair<>("nodeLabel", + ImmutablePair immutablePair4 = new ImmutablePair<>(NODE_LABEL, "serviceCategory"); propertiesCriteriaToDelete.add(immutablePair1); @@ -109,7 +125,7 @@ public class GraphMLConverter { return false; } - System.out.println("Exported file=" + result); + System.out.println(EXPORTED_FILE + result); } catch (Exception e) { log.info("export graph failed -{}" , e); return false; @@ -133,7 +149,7 @@ public class GraphMLConverter { result = exportGraphMl(graph, outputDirectory); - System.out.println("Exported file=" + result); + System.out.println(EXPORTED_FILE + result); } catch (Exception e) { log.info("export exportGraphMl failed - {}" , e); return null; @@ -160,7 +176,7 @@ public class GraphMLConverter { return false; } - System.out.println("Exported file=" + result); + System.out.println(EXPORTED_FILE + result); } catch (Exception e) { log.info("find Error In Json Graph failed - {}" , e); return false; @@ -183,7 +199,7 @@ public class GraphMLConverter { String result = null; - String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".json"; + String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON; OutputStream out = null; try { @@ -208,7 +224,7 @@ public class GraphMLConverter { out.close(); } } catch (IOException e) { - log.info("close FileOutputStream failed - {}" , e); + log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e); } } return result; @@ -217,7 +233,7 @@ public class GraphMLConverter { public String exportGraphMl(TitanGraph graph, String outputDirectory) { String result = null; - String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml"; + String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + ".graphml"; try { try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) { graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph); @@ -288,7 +304,7 @@ public class GraphMLConverter { is.close(); } } catch (IOException e) { - log.info("close FileOutputStream failed - {}" , e); + log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e); } } @@ -303,7 +319,7 @@ public class GraphMLConverter { String result = null; - String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".json"; + String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON; OutputStream out = null; try { @@ -327,7 +343,7 @@ public class GraphMLConverter { vertexTo = edge.inVertex(); BaseConfiguration conf = new BaseConfiguration(); - conf.setProperty("storage.backend", "inmemory"); + conf.setProperty(STORAGE_BACKEND, INMEMORY); TitanGraph openGraph = Utils.openGraph(conf); TitanVertex addVertexFrom = openGraph.addVertex(); @@ -339,7 +355,7 @@ public class GraphMLConverter { Edge addEdge = addVertexFrom.addEdge(edge.label(), addVertexTo); Utils.setProperties(addEdge, Utils.getProperties(edge)); - log.info("fromVertex={}", Utils.getProperties(vertexFrom)); + log.info(FROM_VERTEX, Utils.getProperties(vertexFrom)); log.info("toVertex={}", Utils.getProperties(vertexTo)); log.info("edge={} {} ",edge.label(),Utils.getProperties(edge)); @@ -351,7 +367,7 @@ public class GraphMLConverter { } catch (Exception e) { log.info("run Edge Scan failed - {}" , e); - log.error("fromVertex={}", Utils.getProperties(vertexFrom)); + log.error(FROM_VERTEX, Utils.getProperties(vertexFrom)); log.error("toVertex={}", Utils.getProperties(vertexTo)); log.error("edge={} {} ",edge.label(),Utils.getProperties(edge)); @@ -377,13 +393,13 @@ public class GraphMLConverter { if (!iterator2.hasNext()) { BaseConfiguration conf = new BaseConfiguration(); - conf.setProperty("storage.backend", "inmemory"); + conf.setProperty(STORAGE_BACKEND, INMEMORY); TitanGraph openGraph = Utils.openGraph(conf); TitanVertex addVertexFrom = openGraph.addVertex(); Utils.setProperties(addVertexFrom, Utils.getProperties(vertex)); - log.info("fromVertex={}", Utils.getProperties(addVertexFrom)); + log.info(FROM_VERTEX, Utils.getProperties(addVertexFrom)); GraphSONWriter create = GraphSONWriter.build().create(); create.writeGraph(out, openGraph); @@ -415,7 +431,7 @@ public class GraphMLConverter { ; BaseConfiguration conf = new BaseConfiguration(); - conf.setProperty("storage.backend", "inmemory"); + conf.setProperty(STORAGE_BACKEND, INMEMORY); for (NodeTypeEnum nodeTypeEnum : NodeTypeEnum.values()) { removeNodesByLabel(graph, nodeTypeEnum.getName()); } @@ -435,7 +451,7 @@ public class GraphMLConverter { out.close(); } } catch (IOException e) { - log.info("close FileOutputStream failed - {}" , e); + log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e); } } return result; @@ -457,7 +473,7 @@ public class GraphMLConverter { List> users = new ArrayList<>(); String result = null; - String outputFile = outputDirectory + File.separator + "users." + System.currentTimeMillis() + ".json"; + String outputFile = outputDirectory + File.separator + "users." + System.currentTimeMillis() + DOT_JSON; FileWriter fileWriter = null; try { @@ -494,7 +510,7 @@ public class GraphMLConverter { fileWriter.close(); } } catch (IOException e) { - log.info("close FileOutputStream failed - {}" , e); + log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e); } } return result; @@ -534,7 +550,7 @@ public class GraphMLConverter { return false; } - System.out.println("Exported file=" + result); + System.out.println(EXPORTED_FILE + result); } catch (Exception e) { log.info("export Users failed - {}" , e); return false; diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java index e533c31082..2aed83c823 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java @@ -65,7 +65,7 @@ public class GraphMLDataAnalyzer { // Parse ML file SAXBuilder builder = new SAXBuilder(); File xmlFile = new File(mlFileLocation); - Document document = (Document) builder.build(xmlFile); + Document document = builder.build(xmlFile); // XLS data file name String outputFile = mlFileLocation.replace(".graphml", ".xls"); diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java index 40fd6fb3d0..de23c69a3a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/ProductLogic.java @@ -88,10 +88,8 @@ public class ProductLogic { private TitanGraph openGraph(String titanFileLocation) { - TitanGraph graph = TitanFactory.open(titanFileLocation); - - return graph; - + return TitanFactory.open(titanFileLocation); + } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java index a886367561..6a35da1163 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/RestUtils.java @@ -32,7 +32,7 @@ import java.util.Properties; */ public class RestUtils { - final static String DELETE_PRODUCT = "http://%s:%s/sdc2/rest/v1/catalog/products/%s"; + static final String DELETE_PRODUCT = "http://%s:%s/sdc2/rest/v1/catalog/products/%s"; private static Logger log = Logger.getLogger(RestUtils.class.getName()); -- cgit 1.2.3-korg