2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2024 Deutsche Telekom. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.onap.aai.schemaservice.nodeschema;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import java.util.zip.CRC32;
28 import java.util.zip.Checksum;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.HttpHeaders;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriInfo;
38 import org.springframework.web.bind.annotation.RestController;
42 public class NodeSchemaChecksumResource {
44 private final NodeSchemaService nodeSchemaService;
45 private final ChecksumResponse checksumResponse;
47 public NodeSchemaChecksumResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
48 this.nodeSchemaService = nodeSchemaService;
49 Map<SchemaVersion, Long> checksumMap = schemaVersions.getVersions().stream()
50 .collect(Collectors.toMap(
52 version -> getChecksumForSchemaVersion(version))
54 checksumResponse = new ChecksumResponse(checksumMap);
58 @Path("/nodes/checksums")
59 @Produces({"application/json"})
60 public Response getChecksumByVersion(@Context HttpHeaders headers, @Context UriInfo info) {
61 return Response.ok(checksumResponse).build();
64 private Long getChecksumForSchemaVersion(SchemaVersion version) {
65 Optional<String> optionalSchema = nodeSchemaService.fetch(version.toString());
66 if (optionalSchema.isPresent()) {
67 return getCRC32Checksum(optionalSchema.get().getBytes());
72 private long getCRC32Checksum(byte[] bytes) {
73 Checksum crc32 = new CRC32();
74 crc32.update(bytes, 0, bytes.length);
75 return crc32.getValue();