2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.utils;
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;
21 public class NormalizedNodeAggregator {
22 private final YangInstanceIdentifier rootIdentifier;
23 private final List<Optional<NormalizedNode<?, ?>>> nodes;
24 private final DataTree dataTree;
26 private NormalizedNodeAggregator(final YangInstanceIdentifier rootIdentifier, final List<Optional<NormalizedNode<?, ?>>> nodes,
27 final SchemaContext schemaContext) {
28 this.rootIdentifier = rootIdentifier;
30 // FIXME: BUG-1014: pass down proper DataTree
31 this.dataTree = InMemoryDataTreeFactory.getInstance().create();
32 this.dataTree.setSchemaContext(schemaContext);
36 * Combine data from all the nodes in the list into a tree with root as rootIdentifier
39 * @param schemaContext
41 * @throws DataValidationFailedException
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();
49 private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
50 return combine().getRootNode();
53 private NormalizedNodeAggregator combine() throws DataValidationFailedException {
54 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
56 for (final Optional<NormalizedNode<?,?>> node : nodes) {
57 if (node.isPresent()) {
58 mod.merge(rootIdentifier, node.get());
62 dataTree.validate(mod);
63 final DataTreeCandidate candidate = dataTree.prepare(mod);
64 dataTree.commit(candidate);
69 private Optional<NormalizedNode<?, ?>> getRootNode() {
70 return dataTree.takeSnapshot().readNode(rootIdentifier);