Merge "Bug 2948: Perform create snapshot synchronously"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / NormalizedNodeAggregator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import com.google.common.base.Optional;
11 import java.util.List;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
18 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20
21 public class NormalizedNodeAggregator {
22     private final YangInstanceIdentifier rootIdentifier;
23     private final List<Optional<NormalizedNode<?, ?>>> nodes;
24     private final DataTree dataTree;
25
26     private NormalizedNodeAggregator(YangInstanceIdentifier rootIdentifier, List<Optional<NormalizedNode<?, ?>>> nodes,
27                              SchemaContext schemaContext) {
28         this.rootIdentifier = rootIdentifier;
29         this.nodes = nodes;
30         this.dataTree = InMemoryDataTreeFactory.getInstance().create();
31         this.dataTree.setSchemaContext(schemaContext);
32     }
33
34     /**
35      * Combine data from all the nodes in the list into a tree with root as rootIdentifier
36      *
37      * @param nodes
38      * @param schemaContext
39      * @return
40      * @throws DataValidationFailedException
41      */
42     public static Optional<NormalizedNode<?,?>> aggregate(YangInstanceIdentifier rootIdentifier,
43                                                           List<Optional<NormalizedNode<?, ?>>> nodes,
44                                                           SchemaContext schemaContext) throws DataValidationFailedException {
45         return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext).aggregate();
46     }
47
48     private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
49         return combine().getRootNode();
50     }
51
52     private NormalizedNodeAggregator combine() throws DataValidationFailedException {
53         DataTreeModification mod = dataTree.takeSnapshot().newModification();
54
55         for (Optional<NormalizedNode<?,?>> node : nodes) {
56             if (node.isPresent()) {
57                 mod.merge(rootIdentifier, node.get());
58             }
59         }
60
61         dataTree.validate(mod);
62         final DataTreeCandidate candidate = dataTree.prepare(mod);
63         dataTree.commit(candidate);
64
65         return this;
66     }
67
68     private Optional<NormalizedNode<?, ?>> getRootNode() {
69         return dataTree.takeSnapshot().readNode(rootIdentifier);
70     }
71 }