f644bef9709e514a8da505b167ac6f6ad4bd9969
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.schemaservice.nodeschema;
22
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import java.util.zip.CRC32;
27 import java.util.zip.Checksum;
28
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;
36
37 import org.springframework.web.bind.annotation.RestController;
38
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;
44
45 @Path("/v1")
46 @RestController
47 @Tag(name = "ONAP Schema Service - Checksums", description = "Provides checksum values for different schema versions used in ONAP.")
48 public class NodeSchemaChecksumResource {
49
50     private final NodeSchemaService nodeSchemaService;
51     private final ChecksumResponse checksumResponse;
52
53     public NodeSchemaChecksumResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
54         this.nodeSchemaService = nodeSchemaService;
55         Map<SchemaVersion, Long> checksumMap = schemaVersions.getVersions().stream()
56                 .collect(Collectors.toMap(
57                         version -> version,
58                         version -> getChecksumForSchemaVersion(version)));
59         checksumResponse = new ChecksumResponse(checksumMap);
60     }
61
62     @GET
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"))
68     })
69     public Response getChecksumByVersion(@Context HttpHeaders headers, @Context UriInfo info) {
70         return Response.ok(checksumResponse).build();
71     }
72
73     private Long getChecksumForSchemaVersion(SchemaVersion version) {
74         Optional<String> optionalSchema = nodeSchemaService.fetch(version.toString());
75         if (optionalSchema.isPresent()) {
76             return getCRC32Checksum(optionalSchema.get().getBytes());
77         }
78         return 0L;
79     }
80
81     private long getCRC32Checksum(byte[] bytes) {
82         Checksum crc32 = new CRC32();
83         crc32.update(bytes, 0, bytes.length);
84         return crc32.getValue();
85     }
86
87 }