aboutsummaryrefslogtreecommitdiffstats
path: root/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/listener/MerkleTree.java
blob: 7f24b36ea0342fa02cb19e3fbb8793ac4c821d9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * ============LICENSE_START====================================
 * DCAEGEN2-SERVICES-SDK
 * =========================================================
 * Copyright (C) 2019 Nokia. All rights reserved.
 * =========================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=====================================
 */

package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.listener;

import io.vavr.Function1;
import io.vavr.collection.HashMap;
import io.vavr.collection.List;
import io.vavr.collection.Map;
import io.vavr.control.Option;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;

/**
 * An immutable <a href="https://en.wikipedia.org/wiki/Merkle_tree" target="_blank">Merkle Tree</a> implementation.
 *
 * Each node is labelled with a {@code String} label. A path of a node is defined as a list of labels from root to a
 * given node.
 *
 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
 * @since 1.1.2
 */
public final class MerkleTree<V> {

    private static final String DEFAULT_DIGEST_ALGORITHM = "SHA-256";
    private final ValueSerializer<V> valueSerializer;
    private final HashAlgorithm hashAlgorithm;
    private final MTNode<V> root;
    private final Function1<V, byte[]> hashForValue;

    private MerkleTree(
            @NotNull ValueSerializer<V> valueSerializer,
            @NotNull HashAlgorithm hashAlgorithm,
            @NotNull MTNode<V> root) {
        this.valueSerializer = Objects.requireNonNull(valueSerializer);
        this.hashAlgorithm = Objects.requireNonNull(hashAlgorithm);
        this.root = Objects.requireNonNull(root);
        hashForValue = valueSerializer.andThen(hashAlgorithm);
    }

    /**
     * Create an empty tree with given serializer and using default digest algorithm as a hash function.
     *
     * @param serializer a way of serializing a value to array of bytes
     * @param <V> type of values kept in a tree
     * @return empty tree
     */
    public static @NotNull <V> MerkleTree<V> emptyWithDefaultDigest(@NotNull ValueSerializer<V> serializer) {
        return emptyWithDigest(DEFAULT_DIGEST_ALGORITHM, serializer);
    }

    /**
     * Create an empty tree with given serializer and given digest algorithm used as a hash function.
     *
     * @param digestAlgorithmName name of a digest algorithm as used by {@link MessageDigest#getInstance(String)}
     * @param serializer a way of serializing a value to array of bytes
     * @param <V> type of values kept in a tree
     * @return empty tree
     */
    public static @NotNull <V> MerkleTree<V> emptyWithDigest(
            @NotNull String digestAlgorithmName,
            @NotNull ValueSerializer<V> serializer) {
        return emptyWithHashProvider(serializer, messages -> {
            final MessageDigest messageDigest = messageDigest(digestAlgorithmName);
            messages.forEach(messageDigest::update);
            return messageDigest.digest();
        });
    }

    /**
     * Create an empty tree with given hash function.
     *
     * @param serializer a function which serializes values to a byte array
     * @param hashAlgorithm a function which calculates a hash of a serialized value
     * @param <V> type of values kept in a tree
     * @return empty tree
     */
    public static <V> MerkleTree<V> emptyWithHashProvider(ValueSerializer<V> serializer, HashAlgorithm hashAlgorithm) {
        return new MerkleTree<>(serializer, hashAlgorithm, MTNode.empty(hashAlgorithm));
    }

    private static MessageDigest messageDigest(String digestAlgorithmName) {
        try {
            return MessageDigest.getInstance(digestAlgorithmName);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Unsupported hash algorithm " + digestAlgorithmName, e);
        }
    }

    /**
     * Assigns a value to a given path.
     *
     * Overrides current value if already exists.
     *
     * @param value a value to assign
     * @param path path of labels from root
     * @return an updated tree instance or <code>this</code> if hashes are the same
     */
    public MerkleTree<V> add(V value, String... path) {
        return add(List.of(path), value);
    }

    /**
     * Assigns a value to a given path.
     *
     * Overrides current value if already exists.
     *
     * @param path path of labels from root
     * @param value a value to assign
     * @return an updated tree instance or <code>this</code> if hashes are the same
     */
    public MerkleTree<V> add(List<String> path, V value) {
        final MTNode<V> result = root.addChild(path, MTNode.leaf(hashAlgorithm, hashForValue.apply(value), value));
        return Arrays.equals(result.hash(), root.hash())
                ? this
                : new MerkleTree<>(valueSerializer, hashAlgorithm, result);
    }


    /**
     * Gets a value assigned to a given path.
     *
     * @param path to search for
     * @return Some(value) if path exists and contains a value, None otherwise
     */
    public Option<V> get(String... path) {
        return get(List.of(path));
    }

    /**
     * Gets a value assigned to a given path.
     *
     * @param path to search for
     * @return Some(value) if path exists and contains a value, None otherwise
     */
    public Option<V> get(List<String> path) {
        return root.findNode(path).flatMap(MTNode::value);
    }

    /**
     * Checks if nodes under given path are the same in {@code this} and {@code other} tree.
     *
     * @param other a tree to compare with
     * @param path a path to a subtree to compare
     * @return true if hashes are the same, false otherwise
     */
    public boolean isSame(MerkleTree<V> other, String... path) {
        return isSame(List.of(path), other);
    }

    /**
     * Checks if nodes under given path are the same in {@code this} and {@code other} tree.
     *
     * @param other a tree to compare with
     * @param path a path to a subtree to compare
     * @return true if hashes are the same, false otherwise
     */
    public boolean isSame(List<String> path, MerkleTree<V> other) {
        final byte[] oldHash = other.hashOf(path);
        final byte[] curHash = hashOf(path);
        return Arrays.equals(oldHash, curHash);
    }

    /**
     * Returns a hash of a node under given path.
     *
     * @param path a path of a node to check
     * @return a hash or empty array if node does not exist
     */
    public byte[] hashOf(List<String> path) {
        return root
                .findNode(path)
                .map(node -> node.hash().clone())
                .getOrElse(() -> new byte[0]);
    }

    /**
     * Returns a hash of a node under given path.
     *
     * @param path a path of a node to check
     * @return a hash or empty array if node does not exist
     */
    public byte[] hashOf(String... path) {
        return hashOf(List.of(path));
    }

    /**
     * Returns a subtree with given node as a root.
     *
     * @param path a path of a node to be a subtree root
     * @return Some(subtree) if path exists, None otherwise
     */
    public Option<MerkleTree<V>> subtree(List<String> path) {
        return root.findNode(path).map(node -> new MerkleTree<>(valueSerializer, hashAlgorithm, node));
    }

    /**
     * Returns a subtree with given node as a root.
     *
     * @param path a path of a node to be a subtree root
     * @return Some(subtree) if path exists, None otherwise
     */
    public Option<MerkleTree<V>> subtree(String... path) {
        return subtree(List.of(path));
    }

    /**
     * Hash of a root node.
     *
     * @return a copy of root node's hash
     */
    public byte[] hash() {
        return root.hash().clone();
    }

    @Override
    public String toString() {
        return root.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MerkleTree<?> that = (MerkleTree<?>) o;
        return Objects.equals(root, that.root);
    }

    @Override
    public int hashCode() {
        return Objects.hash(root);
    }
}

final class MTNode<V> {

    private final byte[] hash;
    private final Option<V> value;
    private final Map<String, MTNode<V>> children;
    private final HashAlgorithm hashAlgorithm;

    static <V> MTNode<V> empty(HashAlgorithm hashAlgorithm) {
        return new MTNode<>(hashAlgorithm, new byte[0], Option.none(), HashMap.empty());
    }

    static <V> MTNode<V> leaf(HashAlgorithm hashAlgorithm, byte[] hash, V value) {
        return new MTNode<>(hashAlgorithm, hash, Option.of(value), HashMap.empty());
    }

    private MTNode(
            HashAlgorithm hashAlgorithm,
            byte[] hash,
            Option<V> value,
            Map<String, MTNode<V>> children) {
        this.hashAlgorithm = hashAlgorithm;
        this.hash = hash.clone();
        this.value = value;
        this.children = children;
    }

    MTNode<V> addChild(final List<String> path, final MTNode<V> child) {
        if (path.isEmpty()) {
            return child;
        } else {
            String label = path.head();
            MTNode<V> newChild = children.get(label).fold(
                    () -> MTNode.<V>empty(hashAlgorithm).addChild(path.tail(), child),
                    node -> node.addChild(path.tail(), child)
            );
            return addChild(label, newChild);
        }
    }

    Option<V> value() {
        return value;
    }

    Option<MTNode<V>> findNode(List<String> path) {
        return path.headOption().fold(
                () -> Option.of(this),
                head -> children.get(head).flatMap(child -> child.findNode(path.tail()))
        );
    }

    byte[] hash() {
        return hash;
    }

    private MTNode<V> addChild(String label, MTNode<V> child) {
        final Map<String, MTNode<V>> newChildren = children.put(label, child);
        byte[] newHash = composeHashes(newChildren.iterator(this::hashForChild));
        return Arrays.equals(newHash, hash) ? this : new MTNode<>(
                hashAlgorithm,
                newHash,
                value,
                newChildren
        );
    }

    private byte[] hashForChild(String label, MTNode<V> child) {
        return composeHashes(List.of(label.getBytes(), child.hash()));
    }

    private byte[] composeHashes(Iterable<byte[]> hashes) {
        return hashAlgorithm.apply(hashes);
    }

    @Override
    public String toString() {
        return "(\"" + value.map(Object::toString).getOrElse("") + "\" ["
                + children.map(entry -> entry._1 + "=" + entry._2).mkString(", ")
                + "])";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MTNode<?> mtNode = (MTNode<?>) o;
        return Arrays.equals(hash, mtNode.hash);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(hash);
    }
}