2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.util.etag;
23 import java.io.IOException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
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;
36 * Computes hash for GraphEventVertex and GraphEventEdge
38 public class EtagGenerator {
40 private static final String AAI_LAST_MOD_TS = "aai-last-mod-ts";
41 private final HashGenerator hashGenerator;
43 public EtagGenerator() throws NoSuchAlgorithmException {
44 this.hashGenerator = new HashGenerator();
48 * Takes in the GraphEventVertex for which the hash is to be computed.
49 * @param GraphEventVertex
50 * @return hash for the GraphEventVertex
53 public String computeHashForVertex(GraphEventVertex graphEventVertex) throws IOException {
54 return hashGenerator.generateSHA256AsHex(graphEventVertex.getId(), graphEventVertex.getType(), convertPropertiesToMap(graphEventVertex.getProperties()));
58 * Takes in the GraphEventEdge for which the hash is to be computed.
59 * @param GraphEventEdge
60 * @return hash for the GraphEventEdge
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()));
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);
79 return filterAndSortProperties(propertiesMap);
82 private Map<String, Object> filterAndSortProperties(Map<String, Object> properties) {
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()),