Return ETag in response header
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / 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.champ.util.etag;
22
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;
28 import java.util.Map;
29 import org.onap.aai.champcore.model.ChampObject;
30 import org.onap.aai.champcore.model.ChampRelationship;
31 import org.onap.champ.util.HashGenerator;
32 /**
33  * Computes etag for ChampObjects and ChampRelationships
34  *
35  */
36 public class EtagGenerator {
37
38     private static final String AAI_LAST_MOD_TS = "aai-last-mod-ts";
39     private final HashGenerator hashGenerator;
40
41     public EtagGenerator() throws NoSuchAlgorithmException {
42         this.hashGenerator = new HashGenerator();
43     }
44
45      /**
46      * Takes in the ChampObject for which the hash is to be computed.
47      * @param champObject
48      * @return hash for the ChampObject
49      * @throws IOException
50      */
51     public String computeHashForChampObject(ChampObject champObject) throws IOException {
52         return hashGenerator.generateSHA256AsHex(champObject.getKey().orElse(""), champObject.getType(), filterAndSortProperties(champObject.getProperties()));
53     }
54
55     /**
56      * Takes in the ChampRelationship for which the hash is to be computed.
57      * @param ChampRelationship
58      * @return hash for the ChampRelationship
59      * @throws IOException
60      */
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()));
63     }
64
65     /**
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.
69      * @param champObjects
70      * @return hash for the list of ChampObjects
71      * @throws IOException
72      */
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));
77         }
78         return hashGenerator.generateSHA256AsHex(champObjectHashList);
79     }
80
81     /**
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
87      * @throws IOException
88      */
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));
93         }
94         return hashGenerator.generateSHA256AsHex(champRelationshipHashList);
95     }
96
97     private Map<String, Object> filterAndSortProperties(Map<String, Object> properties) {
98         return properties
99                 .entrySet()
100                 .stream()
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()),
105                         Map::putAll);
106     }
107 }