Fix compiler error due to removal of InMemoryDataTreeFactory.create
[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.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
20 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22
23 public class NormalizedNodeAggregator {
24     private final YangInstanceIdentifier rootIdentifier;
25     private final List<Optional<NormalizedNode<?, ?>>> nodes;
26     private final DataTree dataTree;
27
28     private NormalizedNodeAggregator(final YangInstanceIdentifier rootIdentifier, final List<Optional<NormalizedNode<?, ?>>> nodes,
29                              final SchemaContext schemaContext, LogicalDatastoreType logicalDatastoreType) {
30         this.rootIdentifier = rootIdentifier;
31         this.nodes = nodes;
32         this.dataTree = InMemoryDataTreeFactory.getInstance().create(
33                 logicalDatastoreType == LogicalDatastoreType.CONFIGURATION ? TreeType.CONFIGURATION :
34                     TreeType.OPERATIONAL);
35         this.dataTree.setSchemaContext(schemaContext);
36     }
37
38     /**
39      * Combine data from all the nodes in the list into a tree with root as rootIdentifier
40      *
41      * @param nodes
42      * @param schemaContext
43      * @param logicalDatastoreType
44      * @return
45      * @throws DataValidationFailedException
46      */
47     public static Optional<NormalizedNode<?,?>> aggregate(final YangInstanceIdentifier rootIdentifier,
48                                                           final List<Optional<NormalizedNode<?, ?>>> nodes,
49                                                           final SchemaContext schemaContext,
50                                                           LogicalDatastoreType logicalDatastoreType) throws DataValidationFailedException {
51         return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext, logicalDatastoreType).aggregate();
52     }
53
54     private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
55         return combine().getRootNode();
56     }
57
58     private NormalizedNodeAggregator combine() throws DataValidationFailedException {
59         final DataTreeModification mod = dataTree.takeSnapshot().newModification();
60
61         for (final Optional<NormalizedNode<?,?>> node : nodes) {
62             if (node.isPresent()) {
63                 mod.merge(rootIdentifier, node.get());
64             }
65         }
66         mod.ready();
67         dataTree.validate(mod);
68         final DataTreeCandidate candidate = dataTree.prepare(mod);
69         dataTree.commit(candidate);
70
71         return this;
72     }
73
74     private Optional<NormalizedNode<?, ?>> getRootNode() {
75         return dataTree.takeSnapshot().readNode(rootIdentifier);
76     }
77 }