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.champ.util.etag;
23 import java.io.IOException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import org.onap.aai.champcore.model.ChampObject;
30 import org.onap.aai.champcore.model.ChampRelationship;
31 import org.onap.champ.util.HashGenerator;
33 * Computes etag for ChampObjects and ChampRelationships
36 public class EtagGenerator {
38 private static final String AAI_LAST_MOD_TS = "aai-last-mod-ts";
39 private final HashGenerator hashGenerator;
41 public EtagGenerator() throws NoSuchAlgorithmException {
42 this.hashGenerator = new HashGenerator();
46 * Takes in the ChampObject for which the hash is to be computed.
48 * @return hash for the ChampObject
51 public String computeHashForChampObject(ChampObject champObject) throws IOException {
52 return hashGenerator.generateSHA256AsHex(champObject.getKey().orElse(""), champObject.getType(), filterAndSortProperties(champObject.getProperties()));
56 * Takes in the ChampRelationship for which the hash is to be computed.
57 * @param ChampRelationship
58 * @return hash for the ChampRelationship
61 public String computeHashForChampRelationship(ChampRelationship champRelationship) throws IOException {
62 return hashGenerator.generateSHA256AsHex(champRelationship.getKey().orElse(""), champRelationship.getType(), filterAndSortProperties(champRelationship.getProperties()), computeHashForChampObject(champRelationship.getSource()), computeHashForChampObject(champRelationship.getTarget()));
66 * Takes in the list of ChampObjects for which the hash is to be computed.<br>
67 * Computes the individual hash, adds them to a List. <br>
68 * Note that the order of items in the list affects the hash.
70 * @return hash for the list of ChampObjects
73 public String computeHashForChampObjects(List<ChampObject> champObjects) throws IOException {
74 List<String> champObjectHashList = new ArrayList<>();
75 for(ChampObject champObject : champObjects) {
76 champObjectHashList.add(computeHashForChampObject(champObject));
78 return hashGenerator.generateSHA256AsHex(champObjectHashList);
82 * Takes in the list of ChampRelationships for which the hash is to be computed.<br>
83 * Computes the individual hash, adds them to a List. <br>
84 * Note that the order of items in the list affects the hash.
85 * @param champRelationships
86 * @return hash for the list of ChampRelationships
89 public String computeHashForChampRelationships(List<ChampRelationship> champRelationships) throws IOException {
90 List<String> champRelationshipHashList = new ArrayList<>();
91 for(ChampRelationship champRelationship : champRelationships) {
92 champRelationshipHashList.add(computeHashForChampRelationship(champRelationship));
94 return hashGenerator.generateSHA256AsHex(champRelationshipHashList);
97 private Map<String, Object> filterAndSortProperties(Map<String, Object> properties) {
101 .filter(x -> !x.getKey().equals(AAI_LAST_MOD_TS))
102 .sorted((x, y) -> x.getKey().compareTo(y.getKey()))
103 .collect(LinkedHashMap::new,
104 (m, e) -> m.put(e.getKey(), e.getValue()),