ETags on resources
[aai/gizmo.git] / src / main / java / org / onap / crud / util / etag / EtagGenerator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.util.etag;
22
23 import java.io.IOException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import org.onap.crud.event.GraphEventEdge;
30 import org.onap.crud.event.GraphEventVertex;
31 import org.onap.crud.util.HashGenerator;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34
35 /**
36  * Computes hash for GraphEventVertex and GraphEventEdge
37  */
38 public class EtagGenerator {
39
40     private static final String AAI_LAST_MOD_TS = "aai-last-mod-ts";
41     private final HashGenerator hashGenerator;
42
43     public EtagGenerator() throws NoSuchAlgorithmException {
44         this.hashGenerator = new HashGenerator();
45     }
46
47     /**
48      * Takes in the GraphEventVertex for which the hash is to be computed.
49      * @param GraphEventVertex
50      * @return hash for the GraphEventVertex
51      * @throws IOException
52      */
53     public String computeHashForVertex(GraphEventVertex graphEventVertex) throws IOException {
54         return hashGenerator.generateSHA256AsHex(graphEventVertex.getId(), graphEventVertex.getType(), convertPropertiesToMap(graphEventVertex.getProperties()));
55     }
56
57     /**
58      * Takes in the GraphEventEdge for which the hash is to be computed.
59      * @param GraphEventEdge
60      * @return hash for the GraphEventEdge
61      * @throws IOException
62      */
63     public String computeHashForEdge(GraphEventEdge graphEventEdge) throws IOException {
64         return hashGenerator.generateSHA256AsHex(graphEventEdge.getId(), graphEventEdge.getType(),
65                 convertPropertiesToMap(graphEventEdge.getProperties()),
66                 computeHashForVertex(graphEventEdge.getSource()), computeHashForVertex(graphEventEdge.getTarget()));
67     }
68
69     private Map<String, Object> convertPropertiesToMap(JsonElement properties) {
70         Map<String, Object> propertiesMap = new HashMap<>();
71         if (null != properties) {
72             JsonObject propsObject = properties.getAsJsonObject();
73             for (Entry<String, JsonElement> props : propsObject.entrySet()) {
74                 String key = props.getKey();
75                 String value = props.getValue().getAsString();
76                 propertiesMap.put(key, value);
77             }
78         }
79         return filterAndSortProperties(propertiesMap);
80     }
81
82     private Map<String, Object> filterAndSortProperties(Map<String, Object> properties) {
83         return properties
84                 .entrySet()
85                 .stream()
86                 .filter(x -> !x.getKey().equals(AAI_LAST_MOD_TS))
87                 .sorted((x, y) -> x.getKey().compareTo(y.getKey()))
88                 .collect(LinkedHashMap::new,
89                         (m, e) -> m.put(e.getKey(), e.getValue()),
90                         Map::putAll);
91     }
92 }