diff options
author | Igor Dysko <igor1.dysko@orange.com> | 2021-06-22 14:23:34 +0200 |
---|---|---|
committer | Igor Dysko <igor1.dysko@orange.com> | 2021-06-22 14:23:34 +0200 |
commit | 847dd464a5ccb46dae990fd36173d50839f99977 (patch) | |
tree | 3454bef1e3894d17b86739434cc469e965a2cc42 /src/main | |
parent | 3646ed953013eab56b29abdc57a9bf29a8a8753c (diff) |
Fix bugs and vulnerabilities from sonar report
Fixed bugs and vulnerabilities.
Issue-ID: AAI-3280
Signed-off-by: Igor Dysko <igor1.dysko@orange.com>
Change-Id: I3f64538efc1a9ef2458847bfc60280ca76cd6919
Diffstat (limited to 'src/main')
8 files changed, 54 insertions, 63 deletions
diff --git a/src/main/java/org/onap/aai/graphgraph/ArgumentParser.java b/src/main/java/org/onap/aai/graphgraph/ArgumentParser.java index e156e39..142731c 100644 --- a/src/main/java/org/onap/aai/graphgraph/ArgumentParser.java +++ b/src/main/java/org/onap/aai/graphgraph/ArgumentParser.java @@ -22,10 +22,15 @@ package org.onap.aai.graphgraph; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Option; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ArgumentParser { - CmdLineParser parser; + private static final Logger LOGGER = LoggerFactory.getLogger(ArgumentParser.class); + + private CmdLineParser parser; + @Option(name = "-g", usage = "generates schema model for Papyrus and exits where XY is the version", metaVar = "vXY") private String schemaVersion; @@ -40,7 +45,7 @@ public class ArgumentParser { try { parser.parseArgument(args); } catch (CmdLineException e) { - System.err.println(e.getMessage()); + LOGGER.error("Invalid arguments", e); parser.printUsage(System.err); } diff --git a/src/main/java/org/onap/aai/graphgraph/Config.java b/src/main/java/org/onap/aai/graphgraph/Config.java index 418d16f..d9d4329 100644 --- a/src/main/java/org/onap/aai/graphgraph/Config.java +++ b/src/main/java/org/onap/aai/graphgraph/Config.java @@ -22,7 +22,6 @@ package org.onap.aai.graphgraph; import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.graphgraph.reader.BasicSchemaReader; import org.onap.aai.graphgraph.reader.SchemaRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -43,14 +42,8 @@ public class Config { @Value("${schema.version.list}") String schemaVersions; - @Autowired - private MoxyLoaderRepository moxyLoaderRepository; - - @Autowired - private EdgeIngestor edgeIngestor; - @Bean - SchemaRepository createSchemaRepository() { + SchemaRepository createSchemaRepository(MoxyLoaderRepository moxyLoaderRepository, EdgeIngestor edgeIngestor) { return new SchemaRepository( Arrays.stream(schemaVersions.split(",")) .map(version -> new BasicSchemaReader(version, moxyLoaderRepository, edgeIngestor)) diff --git a/src/main/java/org/onap/aai/graphgraph/ModelExporter.java b/src/main/java/org/onap/aai/graphgraph/ModelExporter.java index 24d0829..f05c39a 100644 --- a/src/main/java/org/onap/aai/graphgraph/ModelExporter.java +++ b/src/main/java/org/onap/aai/graphgraph/ModelExporter.java @@ -62,7 +62,8 @@ public class ModelExporter { private static final String AAIMODEL_UML_FILENAME = "aaimodel.uml"; private static final String VELOCITY_TEMPLATE_FILENAME = "model_export.vm"; private static final boolean OXM_ENABLED = false; - private static final String camelCaseRegex = "(?=[A-Z][a-z])"; + private static final String CAMEL_CASE_REGEX = "(?=[A-Z][a-z])"; + private static final String VELOCITY_ASSOCIATION_NAME_FORMAT = "%s - %s (%s)"; private static final Logger LOGGER = LoggerFactory.getLogger(ModelExporter.class); @@ -109,7 +110,7 @@ public class ModelExporter { Map<String, String> edgeRuleProps = new HashMap<>(); edgeRuleProps.put(EdgeField.FROM.toString(), child); edgeRuleProps.put(EdgeField.TO.toString(), parent); - edgeRuleProps.put(EdgeField.DIRECTION.toString(), Direction.OUT.toString()); //TODO check direction + edgeRuleProps.put(EdgeField.DIRECTION.toString(), Direction.OUT.toString()); edgeRuleProps.put(EdgeField.LABEL.toString(), "OXM Parent-Child"); edgeRuleProps.put(EdgeField.MULTIPLICITY.toString(), MultiplicityRule.MANY2ONE.toString()); edgeRuleProps.put(EdgeField.DEFAULT.toString(), Boolean.toString(false)); @@ -131,10 +132,8 @@ public class ModelExporter { } public void writeExportedModel(String result) { - try { - FileWriter fw = new FileWriter(AAIMODEL_UML_FILENAME); + try (FileWriter fw = new FileWriter(AAIMODEL_UML_FILENAME)) { fw.write(result); - fw.close(); } catch (IOException e) { LOGGER.error("Writing exported model failed", e); } @@ -246,7 +245,7 @@ public class ModelExporter { return new VelocityAssociation( fromEntity.get(), toEntity.get(), - String.format("%s - %s (%s)", from, to, shortenLabel(label)), + String.format(VELOCITY_ASSOCIATION_NAME_FORMAT, from, to, shortenLabel(label)), description, multiplicity, true); @@ -254,7 +253,7 @@ public class ModelExporter { return new VelocityAssociation( toEntity.get(), fromEntity.get(), - String.format("%s - %s (%s)", to, from, shortenLabel(label)), + String.format(VELOCITY_ASSOCIATION_NAME_FORMAT, to, from, shortenLabel(label)), description, multiplicity.equals("ONE2MANY") ? "MANY2ONE" : multiplicity, true); @@ -262,7 +261,7 @@ public class ModelExporter { return new VelocityAssociation( fromEntity.get(), toEntity.get(), - String.format("%s - %s (%s)", from, to, shortenLabel(label)), + String.format(VELOCITY_ASSOCIATION_NAME_FORMAT, from, to, shortenLabel(label)), description, multiplicity, false); @@ -291,7 +290,7 @@ public class ModelExporter { String[] split = entityName.split("\\."); String entityNameRoot = split[split.length - 1]; - final Pattern pattern = Pattern.compile(camelCaseRegex); + final Pattern pattern = Pattern.compile(CAMEL_CASE_REGEX); final Matcher matcher = pattern.matcher(entityNameRoot.substring(1)); String finalEntityNameRoot = (entityNameRoot.charAt(0) + matcher.replaceAll("-")).toLowerCase(); return entities.stream().filter(e -> e.getName().equals(finalEntityNameRoot)).findFirst().orElse(null); diff --git a/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java b/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java index d2822d3..d1a476a 100644 --- a/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java +++ b/src/main/java/org/onap/aai/graphgraph/MoxyLoaderRepository.java @@ -39,7 +39,7 @@ public class MoxyLoaderRepository { @Autowired private NodeIngestor nodeIngestor; - public Map<String, MoxyLoader> moxyLoaders = new HashMap<>(); + private Map<String, MoxyLoader> moxyLoaders = new HashMap<>(); public Map<String, MoxyLoader> getMoxyLoaders() { return moxyLoaders; diff --git a/src/main/java/org/onap/aai/graphgraph/SchemaResource.java b/src/main/java/org/onap/aai/graphgraph/SchemaResource.java index 326634d..68b26ed 100644 --- a/src/main/java/org/onap/aai/graphgraph/SchemaResource.java +++ b/src/main/java/org/onap/aai/graphgraph/SchemaResource.java @@ -41,26 +41,26 @@ public class SchemaResource { @Autowired private SchemaValidator schemaValidator; - @RequestMapping("/schemas") + @GetMapping("/schemas") public List<String> loadSchemaNames() { return schemaRepository.getAllSchemaNames(); } - @RequestMapping("/schemas/{schema}/nodes") + @GetMapping("/schemas/{schema}/nodes") public List<NodeName> loadVertexNames( @PathVariable("schema") String schemaName, @RequestParam("edgeFilter") String edgeFilter) { return schemaRepository.getSchemaReader(schemaName).getAllVertexNames(edgeFilter); } - @RequestMapping("/schemas/{schema}/nodes/{node}") + @GetMapping("/schemas/{schema}/nodes/{node}") public List<NodeProperty> loadProperties( @PathVariable("schema") String schemaName, @PathVariable("node") String node) { return schemaRepository.getSchemaReader(schemaName).getVertexProperties(node); } - @RequestMapping("/schemas/{schema}/edges") + @GetMapping("/schemas/{schema}/edges") public List<Property> loadEdgeProperties( @PathVariable("schema") String schemaName, @RequestParam("fromNode") String fromNodeName, @@ -68,7 +68,7 @@ public class SchemaResource { return schemaRepository.getSchemaReader(schemaName).getEdgeProperties(fromNodeName, toNodeName, "edgerule"); } - @RequestMapping("/schemas/{schema}/graph/basic") + @GetMapping("/schemas/{schema}/graph/basic") public Graph loadGraph( @PathVariable("schema") String schemaName, @RequestParam("node") String initialNodeName, @@ -82,7 +82,7 @@ public class SchemaResource { return graph; } - @RequestMapping("/schemas/{schema}/graph/paths") + @GetMapping("/schemas/{schema}/graph/paths") public Graph loadGraphWithPaths( @PathVariable("schema") String schemaName, @RequestParam("fromNode") String fromNode, @@ -91,13 +91,13 @@ public class SchemaResource { return schemaRepository.getSchemaReader(schemaName).getGraph(fromNode, toNode, edgeFilter); } - @RequestMapping("/schemas/{schema}/validation") + @GetMapping("/schemas/{schema}/validation") public ValidationProblems validateSchema( @PathVariable("schema") String schemaName) { return schemaValidator.validate(schemaName); } - @RequestMapping(value = "/schemas/{schema}/xmiexport", produces = MediaType.TEXT_XML_VALUE) + @GetMapping(value = "/schemas/{schema}/xmiexport", produces = MediaType.TEXT_XML_VALUE) @ResponseBody public String exportSchema( @PathVariable("schema") String schemaName) { diff --git a/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java b/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java index d60e955..203ef13 100644 --- a/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java +++ b/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java @@ -20,19 +20,6 @@ package org.onap.aai.graphgraph.reader; import com.google.common.collect.Multimap; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.function.Predicate; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.jgrapht.Graph; import org.jgrapht.GraphPath; import org.jgrapht.alg.shortestpath.FloydWarshallShortestPaths; @@ -48,9 +35,19 @@ import org.onap.aai.graphgraph.dto.Property; import org.onap.aai.introspection.Introspector; import org.onap.aai.schema.enums.PropertyMetadata; import org.onap.aai.setup.SchemaVersion; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; +import java.util.Map.Entry; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class BasicSchemaReader implements SchemaReader { + private final static Logger LOGGER = LoggerFactory.getLogger(BasicSchemaReader.class); + private Map<String, Introspector> allEntities; private Graph<String, MetadataEdge> graph = new DefaultDirectedGraph<>(MetadataEdge.class); private EdgeIngestor edgeIngestor; @@ -78,35 +75,35 @@ public class BasicSchemaReader implements SchemaReader { graph = createGraph(true, true); } catch (Exception e) { - System.out.println("Failed creation of BasicSchemaReader, version: " + getSchemaName()); - e.printStackTrace(); + LOGGER.error("Failed creation of BasicSchemaReader, version: " + getSchemaName(), e); } } private Graph<String, MetadataEdge> createGraph(boolean withParentChild, boolean withEdgeRules) { - Graph<String, MetadataEdge> graph = new DefaultDirectedGraph<>(MetadataEdge.class); + Graph<String, MetadataEdge> directedGraph = new DefaultDirectedGraph<>(MetadataEdge.class); for (Entry<String, Introspector> currentParent : allEntities.entrySet()) { - graph.addVertex(currentParent.getKey()); + directedGraph.addVertex(currentParent.getKey()); currentParent.getValue().getProperties().stream() .filter(v -> allEntities.containsKey(v)) .filter(v -> !currentParent.getKey().equals(v)) .forEach(v -> { - graph.addVertex(v); + directedGraph.addVertex(v); if (withParentChild) { - addParentChildEdge(currentParent.getKey(), v, graph); + addParentChildEdge(currentParent.getKey(), v, directedGraph); } }); } if (!withEdgeRules) { - return graph; + return directedGraph; } Multimap<String, EdgeRule> allRules = null; try { allRules = edgeIngestor.getAllRules(new SchemaVersion(getSchemaName())); + } catch (EdgeRuleNotFoundException e) { - //TODO fix + LOGGER.error("Edge rule not found", e); } Objects.requireNonNull(allRules).asMap().values().stream() @@ -114,19 +111,19 @@ public class BasicSchemaReader implements SchemaReader { .forEach(e -> { switch (e.getDirection()) { case OUT: - addEdgerule(e.getFrom(), e.getTo(), e.getLabel(), graph); + addEdgerule(e.getFrom(), e.getTo(), e.getLabel(), directedGraph); break; case IN: - addEdgerule(e.getTo(), e.getFrom(), e.getLabel(), graph); + addEdgerule(e.getTo(), e.getFrom(), e.getLabel(), directedGraph); break; case BOTH: - addEdgerule(e.getFrom(), e.getTo(), e.getLabel(), graph); - addEdgerule(e.getTo(), e.getFrom(), e.getLabel(), graph); + addEdgerule(e.getFrom(), e.getTo(), e.getLabel(), directedGraph); + addEdgerule(e.getTo(), e.getFrom(), e.getLabel(), directedGraph); break; } }); - return graph; + return directedGraph; } private void addEdgerule(String parent, String child, String label, Graph<String, MetadataEdge> graph) { @@ -210,7 +207,7 @@ public class BasicSchemaReader implements SchemaReader { return properties.orElse(Collections.emptyList()); } catch (EdgeRuleNotFoundException e) { - //TODO fix + LOGGER.error("Edge rule not found", e); } } return Collections.emptyList(); diff --git a/src/main/java/org/onap/aai/graphgraph/reader/MetadataEdge.java b/src/main/java/org/onap/aai/graphgraph/reader/MetadataEdge.java index 548d550..9a2276c 100644 --- a/src/main/java/org/onap/aai/graphgraph/reader/MetadataEdge.java +++ b/src/main/java/org/onap/aai/graphgraph/reader/MetadataEdge.java @@ -28,11 +28,7 @@ public class MetadataEdge extends DefaultEdge { private final String source; private final String label; - MetadataEdge(String type, String target, String source) { - this(type, source, target, ""); - } - - MetadataEdge(String type, String target, String source, String label) { + public MetadataEdge(String type, String target, String source, String label) { this.type = type; this.target = target; this.source = source; diff --git a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java index a6c404e..0e98da8 100644 --- a/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java +++ b/src/main/java/org/onap/aai/graphgraph/velocity/VelocityEntityProperty.java @@ -19,11 +19,11 @@ */ package org.onap.aai.graphgraph.velocity; -import java.util.UUID; - import org.eclipse.jetty.util.StringUtil; import org.onap.aai.graphgraph.dto.Property; +import java.util.UUID; + public class VelocityEntityProperty extends Property { private final VelocityEntity entity; @@ -34,7 +34,7 @@ public class VelocityEntityProperty extends Property { super(propertyName, propertyValue); this.description = propertyDescription; this.entity = entity; - propertyId = entity != null ? entity.getRandomId() : UUID.randomUUID().toString(); + propertyId = VelocityId.getRandomId(); } public String getEntityId() { @@ -69,4 +69,5 @@ public class VelocityEntityProperty extends Property { public String toString() { return "VelocityEntityProperty{" + " name=" + getPropertyName() + " description=" + getDescription() + " type=" + getPropertyValue() + '}'; } + } |