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=========================================================
21 package org.onap.aai.schemaservice.nodeschema;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import java.util.zip.CRC32;
27 import java.util.zip.Checksum;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriInfo;
37 import org.springframework.web.bind.annotation.RestController;
39 import io.swagger.v3.oas.annotations.Operation;
40 import io.swagger.v3.oas.annotations.media.Content;
41 import io.swagger.v3.oas.annotations.media.Schema;
42 import io.swagger.v3.oas.annotations.responses.ApiResponse;
43 import io.swagger.v3.oas.annotations.tags.Tag;
47 @Tag(name = "ONAP Schema Service - Checksums", description = "Provides checksum values for different schema versions used in ONAP.")
48 public class NodeSchemaChecksumResource {
50 private final NodeSchemaService nodeSchemaService;
51 private final ChecksumResponse checksumResponse;
53 public NodeSchemaChecksumResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
54 this.nodeSchemaService = nodeSchemaService;
55 Map<SchemaVersion, Long> checksumMap = schemaVersions.getVersions().stream()
56 .collect(Collectors.toMap(
58 version -> getChecksumForSchemaVersion(version)));
59 checksumResponse = new ChecksumResponse(checksumMap);
63 @Path("/nodes/checksums")
64 @Produces({ "application/json" })
65 @Operation(summary = "Get schema checksums by version", description = "Returns a map of schema versions to their CRC32 checksum values for ONAP schema service.", responses = {
66 @ApiResponse(responseCode = "200", description = "Checksum data retrieved successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ChecksumResponse.class))),
67 @ApiResponse(responseCode = "500", description = "Internal server error", content = @Content(mediaType = "application/json"))
69 public Response getChecksumByVersion(@Context HttpHeaders headers, @Context UriInfo info) {
70 return Response.ok(checksumResponse).build();
73 private Long getChecksumForSchemaVersion(SchemaVersion version) {
74 Optional<String> optionalSchema = nodeSchemaService.fetch(version.toString());
75 if (optionalSchema.isPresent()) {
76 return getCRC32Checksum(optionalSchema.get().getBytes());
81 private long getCRC32Checksum(byte[] bytes) {
82 Checksum crc32 = new CRC32();
83 crc32.update(bytes, 0, bytes.length);
84 return crc32.getValue();