From 2e6edbd56d6644d84fe24b061be06b952ceb773a Mon Sep 17 00:00:00 2001 From: "Kajur, Harish (vk250x)" Date: Tue, 15 Jan 2019 22:48:58 -0500 Subject: [PATCH] Add the edges endpoint to the schema service Issue-ID: AAI-1862 Change-Id: I0c540b4ecf4e69411a51453e86bf805e2146d027 Signed-off-by: Kajur, Harish (vk250x) --- .../onap/aai/schemaservice/edges/EdgeResource.java | 95 +++++++++++++ .../org/onap/aai/schemaservice/edges/EdgeRule.java | 153 +++++++++++++++++++++ .../onap/aai/schemaservice/edges/EdgeRules.java | 72 ++++++++++ .../onap/aai/schemaservice/edges/EdgeService.java | 106 ++++++++++++++ .../aai/schemaservice/web/JerseyConfiguration.java | 2 + 5 files changed, 428 insertions(+) create mode 100644 aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeResource.java create mode 100644 aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRule.java create mode 100644 aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRules.java create mode 100644 aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeService.java diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeResource.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeResource.java new file mode 100644 index 0000000..cb045ea --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeResource.java @@ -0,0 +1,95 @@ +/** + * ============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.schemaservice.edges; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.restcore.HttpMethod; +import org.onap.aai.restcore.RESTAPI; +import org.onap.aai.schemaservice.nodeschema.SchemaVersion; +import org.onap.aai.schemaservice.nodeschema.SchemaVersions; +import org.onap.aai.schemaservice.nodeschema.validation.AAISchemaValidationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import java.util.Optional; + +@Path("/v1") +public class EdgeResource extends RESTAPI { + + private final EdgeService edgeService; + private final SchemaVersions schemaVersions; + private final Gson gson; + + @Autowired + public EdgeResource(EdgeService edgeService, SchemaVersions schemaVersions){ + this.edgeService = edgeService; + this.schemaVersions = schemaVersions; + gson = new GsonBuilder().create(); + } + + @GET + @Path("/edgerules") + @Produces({ "application/json"}) + public Response retrieveSchema(@QueryParam("version") String version, + @Context HttpHeaders headers, + @Context UriInfo info) + { + Response response = null; + + try { + + if(StringUtils.isEmpty(version)){ + throw new AAIException("AAI_3050"); + } + + SchemaVersion schemaVersion = new SchemaVersion(version); + + if(!schemaVersions.getVersions().contains(schemaVersion)){ + throw new AAIException("AAI_3018", version); + } + + Optional edgeRulesOptional = edgeService.findRules(version); + + if(!edgeRulesOptional.isPresent()){ + throw new AAIException("AAI_3001"); + } + + response = Response.ok(gson.toJson(edgeRulesOptional.get())).build(); + } catch(AAIException ex){ + response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, ex); + } catch(AAISchemaValidationException ex){ + response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, new AAIException("AAI_3051", version)); + } catch(Exception ex){ + response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, new AAIException("AAI_4000")); + } + + return response; + } +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRule.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRule.java new file mode 100644 index 0000000..ab0f311 --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRule.java @@ -0,0 +1,153 @@ +/** + * ============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.schemaservice.edges; + +import com.google.gson.annotations.SerializedName; + +public class EdgeRule { + + private String from; + private String to; + private String label; + + private String direction; + private String multiplicity; + private String description; + + @SerializedName("contains-other-v") + private String containsOtherV; + + @Override + public String toString() { + return "EdgeRule{" + + "from='" + from + '\'' + + ", to='" + to + '\'' + + ", label='" + label + '\'' + + ", direction='" + direction + '\'' + + ", multiplicity='" + multiplicity + '\'' + + ", description='" + description + '\'' + + ", containsOtherV='" + containsOtherV + '\'' + + ", deleteOtherV='" + deleteOtherV + '\'' + + ", preventDelete='" + preventDelete + '\'' + + ", privateEdge=" + privateEdge + + ", isDefaultEdge=" + defaultEdge + + '}'; + } + + @SerializedName("delete-other-v") + private String deleteOtherV; + @SerializedName("prevent-delete") + private String preventDelete; + + @SerializedName("private") + private String privateEdge; + + @SerializedName("default") + private String defaultEdge; + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getDirection() { + return direction; + } + + public void setDirection(String direction) { + this.direction = direction; + } + + public String getMultiplicity() { + return multiplicity; + } + + public void setMultiplicity(String multiplicity) { + this.multiplicity = multiplicity; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getContainsOtherV() { + return containsOtherV; + } + + public void setContainsOtherV(String containsOtherV) { + this.containsOtherV = containsOtherV; + } + + public String getDeleteOtherV() { + return deleteOtherV; + } + + public void setDeleteOtherV(String deleteOtherV) { + this.deleteOtherV = deleteOtherV; + } + + public String getPreventDelete() { + return preventDelete; + } + + public void setPreventDelete(String preventDelete) { + this.preventDelete = preventDelete; + } + + public String getPrivateEdge() { + return privateEdge; + } + + public void setPrivateEdge(String privateEdge) { + this.privateEdge = privateEdge; + } + + public String getDefaultEdge() { + return defaultEdge; + } + + public void setDefaultEdge(String defaultEdge) { + this.defaultEdge = defaultEdge; + } + +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRules.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRules.java new file mode 100644 index 0000000..ec68d30 --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeRules.java @@ -0,0 +1,72 @@ +/** + * ============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.schemaservice.edges; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; +import java.util.Objects; + +public class EdgeRules { + + @SerializedName("rules") + private List rules; + + public EdgeRules(List rules){ + this.rules = rules; + } + + public List getRules() { + return rules; + } + + public void setRules(List rules) { + this.rules = rules; + } + + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + EdgeRules rules1 = (EdgeRules) o; + + return Objects.equals(rules, rules1.rules); + } + + @Override + public int hashCode() { + return Objects.hash(rules); + } + + @Override + public String toString() { + return "EdgeRules{" + + "rules=" + rules + + '}'; + } + +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeService.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeService.java new file mode 100644 index 0000000..ca953fd --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/edges/EdgeService.java @@ -0,0 +1,106 @@ +/** + * ============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.schemaservice.edges; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.stream.JsonReader; +import org.onap.aai.schemaservice.nodeschema.SchemaVersion; +import org.onap.aai.schemaservice.nodeschema.SchemaVersions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Service +public class EdgeService { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(EdgeService.class); + + private static final String FILESEP = System.getProperty("file.separator"); + + private SchemaVersions schemaVersions; + private String edgesLocation; + + private Map rulesMap; + + @Autowired + public EdgeService(SchemaVersions schemaVersions, + @Value("${schema.edges.location}") String edgesLocation){ + this.schemaVersions = schemaVersions; + this.edgesLocation = edgesLocation; + this.rulesMap = new HashMap<>(); + } + + @PostConstruct + public void initialize() throws IOException { + + Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create(); + + for (SchemaVersion schemaVersion : schemaVersions.getVersions()) { + + String edgeRuleVersionPath = edgesLocation + FILESEP + schemaVersion.toString(); + + LOGGER.debug("For the version {} looking for edge rules in folder {}", schemaVersion, edgeRuleVersionPath); + + try (Stream pathStream = Files.walk(Paths.get(edgeRuleVersionPath))){ + + List jsonFiles = pathStream + .filter((path) -> path.toString().endsWith(".json")) + .collect(Collectors.toList()); + + if(jsonFiles.isEmpty()){ + LOGGER.error("Unable to find any edge rules json files in folder {}", edgeRuleVersionPath); + } else { + LOGGER.trace("Found the following edge rules {}", jsonFiles); + } + + List rules = new ArrayList<>(); + for(Path path : jsonFiles){ + File edgeRuleFile = path.toFile(); + try (JsonReader jsonReader = new JsonReader(new FileReader(edgeRuleFile))){ + EdgeRules edgeRules = gson.fromJson(jsonReader, EdgeRules.class); + rules.addAll(edgeRules.getRules()); + } + } + + rulesMap.put(schemaVersion.toString(), new EdgeRules(rules)); + } + } + + } + + public Optional findRules(String version){ + return Optional.ofNullable(rulesMap.get(version)); + } +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java index 606d24b..bdd7946 100644 --- a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java @@ -20,6 +20,7 @@ package org.onap.aai.schemaservice.web; import org.glassfish.jersey.server.ResourceConfig; +import org.onap.aai.schemaservice.edges.EdgeResource; import org.onap.aai.schemaservice.healthcheck.EchoResource; import org.onap.aai.schemaservice.nodeschema.NodeSchemaResource; import org.onap.aai.schemaservice.versions.VersionResource; @@ -52,6 +53,7 @@ public class JerseyConfiguration extends ResourceConfig { register(VersionResource.class); register(EchoResource.class); register(NodeSchemaResource.class); + register(EdgeResource.class); //Request Filters registerFiltersForRequests(); -- 2.16.6