diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-12-08 20:41:19 +0100 |
---|---|---|
committer | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-12-09 08:39:06 +0100 |
commit | 31bb930755b5b350bedb2ab0bc18f0ec820c17f8 (patch) | |
tree | 577b8fb8ce2960a35b5b07632a8584c60fd610db /aai-schema-service/src/main | |
parent | 8b018d4f1c8a2dbc7bf68aa3275d6756b814cd0a (diff) |
Add checksum endpoint to schema-service
- provide endpoint that allows consumers to check if schema creation is necessary
- this can be used for the schema creation job to only run when the schema changed
Issue-ID: AAI-4084
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Change-Id: I831f8c134b548f460eda26570d45f99f28e048a4
Diffstat (limited to 'aai-schema-service/src/main')
3 files changed, 114 insertions, 0 deletions
diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/ChecksumResponse.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/ChecksumResponse.java new file mode 100644 index 0000000..bf61eb8 --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/ChecksumResponse.java @@ -0,0 +1,34 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.nodeschema; + +import java.util.Map; + +import lombok.Data; +import lombok.Builder; +import lombok.extern.jackson.Jacksonized; + +@Data +@Builder +@Jacksonized +public class ChecksumResponse { + private final Map<SchemaVersion, Long> checksumMap; +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResource.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResource.java new file mode 100644 index 0000000..e3b57df --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResource.java @@ -0,0 +1,78 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.nodeschema; + +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.zip.CRC32; +import java.util.zip.Checksum; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +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 org.springframework.web.bind.annotation.RestController; + +@Path("/v1") +@RestController +public class NodeSchemaChecksumResource { + + private final NodeSchemaService nodeSchemaService; + private final ChecksumResponse checksumResponse; + + public NodeSchemaChecksumResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) { + this.nodeSchemaService = nodeSchemaService; + Map<SchemaVersion, Long> checksumMap = schemaVersions.getVersions().stream() + .collect(Collectors.toMap( + version -> version, + version -> getChecksumForSchemaVersion(version)) + ); + checksumResponse = new ChecksumResponse(checksumMap); + } + + @GET + @Path("/nodes/checksums") + @Produces({"application/json"}) + public Response getChecksumByVersion(@Context HttpHeaders headers, @Context UriInfo info) { + return Response.ok(checksumResponse).build(); + } + + private Long getChecksumForSchemaVersion(SchemaVersion version) { + Optional<String> optionalSchema = nodeSchemaService.fetch(version.toString()); + if (optionalSchema.isPresent()) { + return getCRC32Checksum(optionalSchema.get().getBytes()); + } + return 0L; + } + + private long getCRC32Checksum(byte[] bytes) { + Checksum crc32 = new CRC32(); + crc32.update(bytes, 0, bytes.length); + return crc32.getValue(); + } + +} 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 299c813..c0740f1 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 @@ -32,6 +32,7 @@ import javax.ws.rs.container.ContainerResponseFilter; 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.NodeSchemaChecksumResource; import org.onap.aai.schemaservice.nodeschema.NodeSchemaResource; import org.onap.aai.schemaservice.query.QueryResource; import org.onap.aai.schemaservice.versions.VersionResource; @@ -58,6 +59,7 @@ public class JerseyConfiguration extends ResourceConfig { register(NodeSchemaResource.class); register(QueryResource.class); register(EdgeResource.class); + register(NodeSchemaChecksumResource.class); // Request Filters registerFilters(ContainerRequestFilter.class); |