BUG-1014: expose a proper ShardDataTree constructor
[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(final YangInstanceIdentifier rootIdentifier, final List<Optional<NormalizedNode<?, ?>>> nodes,
27                              final SchemaContext schemaContext) {
28         this.rootIdentifier = rootIdentifier;
29         this.nodes = nodes;
30         // FIXME: BUG-1014: pass down proper DataTree
31         this.dataTree = InMemoryDataTreeFactory.getInstance().create();
32         this.dataTree.setSchemaContext(schemaContext);
33     }
34
35     /**
36      * Combine data from all the nodes in the list into a tree with root as rootIdentifier
37      *
38      * @param nodes
39      * @param schemaContext
40      * @return
41      * @throws DataValidationFailedException
42      */
43     public static Optional<NormalizedNode<?,?>> aggregate(final YangInstanceIdentifier rootIdentifier,
44                                                           final List<Optional<NormalizedNode<?, ?>>> nodes,
45                                                           final SchemaContext schemaContext) throws DataValidationFailedException {
46         return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext).aggregate();
47     }
48
49     private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
50         return combine().getRootNode();
51     }
52
53     private NormalizedNodeAggregator combine() throws DataValidationFailedException {
54         final DataTreeModification mod = dataTree.takeSnapshot().newModification();
55
56         for (final Optional<NormalizedNode<?,?>> node : nodes) {
57             if (node.isPresent()) {
58                 mod.merge(rootIdentifier, node.get());
59             }
60         }
61         mod.ready();
62         dataTree.validate(mod);
63         final DataTreeCandidate candidate = dataTree.prepare(mod);
64         dataTree.commit(candidate);
65
66         return this;
67     }
68
69     private Optional<NormalizedNode<?, ?>> getRootNode() {
70         return dataTree.takeSnapshot().readNode(rootIdentifier);
71     }
72 }