diff options
author | David Brilla <david.brilla@tieto.com> | 2020-02-12 11:00:39 +0100 |
---|---|---|
committer | James Forsyth <jf2512@att.com> | 2020-02-21 19:55:35 +0000 |
commit | e654645a50a0d028d8e67ea997f84efe8d28a6a0 (patch) | |
tree | ce460c6da06fc6e33a204b8a68e1bcd471c956df /aai-schema-abstraction/src/main | |
parent | 4f436e1378f7ee4533701f5b1766fe04d123cd1c (diff) |
AAI-common sonar fixes
Fixing bugs, code smells of aai-common repo found on sonar.
Issue-ID: AAI-2786
Change-Id: I421f8b46f385c7334b0f3cd80c07cb2f833f127f
Signed-off-by: David Brilla <david.brilla@tieto.com>
Diffstat (limited to 'aai-schema-abstraction/src/main')
3 files changed, 59 insertions, 68 deletions
diff --git a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java index 07dd65d1..5d259128 100644 --- a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java +++ b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/definitions/EdgeSchema.java @@ -27,25 +27,25 @@ public class EdgeSchema { protected String name; protected String source; protected String target; - protected Multiplicity multiplicity; + protected Multiplicity multiplicity; protected Map<String,String> annotations; protected Map<String,PropertySchema> properties; - + public enum Multiplicity { - MANY_2_MANY, + MANY_2_MANY, MANY_2_ONE, ONE_2_MANY, ONE_2_ONE } - + public String getName() { return name; } - + public String getSource() { return source; } - + public String getTarget() { return target; } @@ -61,55 +61,53 @@ public class EdgeSchema { public Map<String,PropertySchema> getPropertySchemaList() { return properties; } - + public String getAnnotationValue(String annotation) { return annotations.get(annotation.toLowerCase()); } - + public Map<String,String> getAnnotations() { return annotations; } - + @Override public int hashCode() { String key = source + target + name; return key.hashCode(); } - + @Override public boolean equals(Object obj) { if (this == obj) { return true; } - - if (getClass() != obj.getClass()) { + + if (obj == null || getClass() != obj.getClass()) { return false; } - + EdgeSchema other = (EdgeSchema) obj; - - if ( (!source.equals(other.getSource())) || (!target.equals(other.getTarget())) || (!name.equals(other.getName())) ) { - return false; - } - - return true; + + return (source.equals(other.getSource())) + && (target.equals(other.getTarget())) + && (name.equals(other.getName())); } - + public String toString() { StringBuilder sb = new StringBuilder(); sb.append("edge: " + getSource() + " -> " + getTarget() + "\n"); sb.append(" type: " + getName() + "\n"); sb.append(" multiplicity: " + getMultiplicity() + "\n"); - + sb.append(" annotations: " + "\n"); - for (String annotation : annotations.keySet()) { - sb.append(" " + annotation + ": " + annotations.get(annotation) + "\n"); + for (Map.Entry<String, String> entry : annotations.entrySet()) { + sb.append(" " + entry.getKey() + ": " + entry.getValue() + "\n"); } sb.append(" properties: " + "\n"); for (PropertySchema attrSchema : getPropertySchemaList().values()) { sb.append(attrSchema.toString()); } - + return sb.toString(); } diff --git a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java index a947eac4..47cb2727 100644 --- a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java +++ b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/JsonSchemaProvider.java @@ -30,7 +30,7 @@ import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.zip.ZipEntry; +import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.onap.aai.cl.api.Logger; @@ -53,25 +53,25 @@ import org.springframework.web.client.RestTemplate; public class JsonSchemaProvider implements SchemaProvider { Logger logger = LoggerFactory.getInstance().getLogger(JsonSchemaProvider.class.getName()); - + private JsonSchemaProviderConfig config; private Map<String,SchemaInstance> schemaCache = new ConcurrentHashMap<>(); private RestTemplate restTemplate = null; - + public JsonSchemaProvider(JsonSchemaProviderConfig config) { this.config = config; - SecureClientHttpRequestFactory fac = new SecureClientHttpRequestFactory(config); + SecureClientHttpRequestFactory fac = new SecureClientHttpRequestFactory(config); fac.setBufferRequestBody(false); this.restTemplate = new RestTemplate(fac); } - + @Override public void loadSchema() throws SchemaProviderException { // Load the latest schema version fetchSchemaVersion(getLatestSchemaVersion()); } - + @Override public String getLatestSchemaVersion() throws SchemaProviderException { return "v0"; @@ -79,7 +79,7 @@ public class JsonSchemaProvider implements SchemaProvider { @Override public VertexSchema getVertexSchema(String vertexName, String schemaVersion) throws SchemaProviderException { - SchemaInstance inst = getSchemaVersion(schemaVersion); + SchemaInstance inst = getSchemaVersion(schemaVersion); return inst.getVertexSchema(vertexName); } @@ -89,16 +89,16 @@ public class JsonSchemaProvider implements SchemaProvider { SchemaInstance inst = getSchemaVersion(version); return inst.getEdgeSchema(sourceVertex, targetVertex, edgeType); } - + @Override public Set<EdgeSchema> getAdjacentEdgeSchema(String vertexType, String version) throws SchemaProviderException { SchemaInstance inst = getSchemaVersion(version); - + Set<EdgeSchema> edgeList = inst.getEdgeSchema(vertexType); if (edgeList == null) { - edgeList = new HashSet<EdgeSchema>(); + edgeList = new HashSet<>(); } - + return edgeList; } @@ -107,55 +107,51 @@ public class JsonSchemaProvider implements SchemaProvider { throws SchemaProviderException { SchemaInstance inst = getSchemaVersion(version); - if (inst == null) { - throw new SchemaProviderException("Unable to find schema version " + version); - } - Set<EdgeSchema> edgeList = inst.getEdgeSchemas(sourceType, targetType); if (edgeList == null) { - edgeList = new HashSet<EdgeSchema>(); + edgeList = new HashSet<>(); } - + return edgeList; } - + public void loadSchema(String payload, String version) throws SchemaProviderException { JsonSchema jsonSchema = JsonSchema.fromJson(payload); SchemaInstance schemaInst = new SchemaInstance(); - + for (JsonVertexSchema jsonVertex : jsonSchema.getNodeTypes()) { FromJsonVertexSchema vSchema = new FromJsonVertexSchema(); vSchema.fromJson(jsonVertex, jsonSchema.getDataTypes(), jsonSchema.getCommonProperties()); schemaInst.addVertex(vSchema); } - + for (JsonEdgeSchema jsonEdge : jsonSchema.getRelationshipTypes()) { FromJsonEdgeSchema eSchema = new FromJsonEdgeSchema(); eSchema.fromJson(jsonEdge); schemaInst.addEdge(eSchema); } - + schemaCache.put(version, schemaInst); } - + private synchronized void fetchSchemaVersion(String version) throws SchemaProviderException { if (schemaCache.get(version) != null) { return; } - + String url = config.getSchemaServiceBaseUrl() + "/" + version; - + HttpHeaders headers = new HttpHeaders(); headers.put("X-FromAppId", Arrays.asList(config.getServiceName())); headers.put("X-TransactionId", Arrays.asList(java.util.UUID.randomUUID().toString())); headers.setAccept(Arrays.asList(org.springframework.http.MediaType.APPLICATION_OCTET_STREAM)); - - HttpEntity <String> entity = new HttpEntity<String>(headers); - + + HttpEntity <String> entity = new HttpEntity<>(headers); + ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class); - - + + if (response.getStatusCodeValue() == HttpStatus.NOT_FOUND.value()) { logger.warn(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "version " + version + " not found"); throw new SchemaProviderException("Schema version " + version + " not found"); @@ -173,7 +169,7 @@ public class JsonSchemaProvider implements SchemaProvider { StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); ex.printStackTrace(printWriter); - logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "failed to load version " + version + ": " + logger.error(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, "failed to load version " + version + ": " + response.getBody() + "\n" + writer.toString()); throw new SchemaProviderException("Error loading schema version " + version + ":" + ex.getMessage()); @@ -181,7 +177,7 @@ public class JsonSchemaProvider implements SchemaProvider { logger.info(SchemaProviderMsgs.LOADED_SCHEMA_FILE, version); } - + private String unzipAndGetJSONString(ResponseEntity<byte[]> response) throws IOException { StringBuffer sb = new StringBuffer(""); @@ -199,8 +195,10 @@ public class JsonSchemaProvider implements SchemaProvider { } } finally { try { - zipStream.closeEntry(); - zipStream.close(); + if (zipStream != null) { + zipStream.closeEntry(); + zipStream.close(); + } } catch (Exception e) { logger.warn(SchemaProviderMsgs.SCHEMA_LOAD_ERROR, e.toString()); @@ -223,7 +221,7 @@ public class JsonSchemaProvider implements SchemaProvider { throw new SchemaProviderException("Unable to find schema version " + versionToLoad); } } - + return inst; } @@ -231,5 +229,5 @@ public class JsonSchemaProvider implements SchemaProvider { public Map<String, VertexSchema> getVertexMap(String schemaVersion) throws SchemaProviderException { return getSchemaVersion(schemaVersion).getVertexMap(); } - + } diff --git a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java index 0b48a36c..5be74c39 100644 --- a/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java +++ b/aai-schema-abstraction/src/main/java/org/onap/aai/schemaif/json/SecureClientHttpRequestFactory.java @@ -45,7 +45,7 @@ public class SecureClientHttpRequestFactory extends SimpleClientHttpRequestFacto private static final String KEYSTORE_ALGORITHM = "SunX509"; private static final String KEYSTORE_TYPE = "PKCS12"; private JsonSchemaProviderConfig config; - + public SecureClientHttpRequestFactory(JsonSchemaProviderConfig config) { super(); @@ -100,18 +100,13 @@ public class SecureClientHttpRequestFactory extends SimpleClientHttpRequestFacto } if (config.getSchemaServiceCertFile() != null) { - FileInputStream fin =null; - try { - fin = new FileInputStream(config.getSchemaServiceCertFile()); - + try (FileInputStream fin = new FileInputStream(config.getSchemaServiceCertFile())) { // Load the keystore and initialize the key manager factory. ks.load(fin, pwd); kmf.init(ks, pwd); - + ctx.init(kmf.getKeyManagers(), trustAllCerts, null); - }finally { - fin.close(); - } + } } else { ctx.init(null, trustAllCerts, null); } |