e3b57df85d743748a38d8e025b05b39ffc85a40b
[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
22 package org.onap.aai.schemaservice.nodeschema;
23
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import java.util.zip.CRC32;
28 import java.util.zip.Checksum;
29
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;
37
38 import org.springframework.web.bind.annotation.RestController;
39
40 @Path("/v1")
41 @RestController
42 public class NodeSchemaChecksumResource {
43
44     private final NodeSchemaService nodeSchemaService;
45     private final ChecksumResponse checksumResponse;
46
47     public NodeSchemaChecksumResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
48         this.nodeSchemaService = nodeSchemaService;
49         Map<SchemaVersion, Long> checksumMap = schemaVersions.getVersions().stream()
50             .collect(Collectors.toMap(
51                 version -> version,
52                 version -> getChecksumForSchemaVersion(version))
53             );
54         checksumResponse = new ChecksumResponse(checksumMap);
55     }
56
57     @GET
58     @Path("/nodes/checksums")
59     @Produces({"application/json"})
60     public Response getChecksumByVersion(@Context HttpHeaders headers, @Context UriInfo info) {
61         return Response.ok(checksumResponse).build();
62     }
63
64     private Long getChecksumForSchemaVersion(SchemaVersion version) {
65         Optional<String> optionalSchema = nodeSchemaService.fetch(version.toString());
66         if (optionalSchema.isPresent()) {
67             return getCRC32Checksum(optionalSchema.get().getBytes());
68         }
69         return 0L;
70     }
71
72     private long getCRC32Checksum(byte[] bytes) {
73         Checksum crc32 = new CRC32();
74         crc32.update(bytes, 0, bytes.length);
75         return crc32.getValue();
76     }
77
78 }