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(YangInstanceIdentifier rootIdentifier, List<Optional<NormalizedNode<?, ?>>> nodes,
27 SchemaContext schemaContext) {
28 this.rootIdentifier = rootIdentifier;
30 this.dataTree = InMemoryDataTreeFactory.getInstance().create();
31 this.dataTree.setSchemaContext(schemaContext);
35 * Combine data from all the nodes in the list into a tree with root as rootIdentifier
38 * @param schemaContext
40 * @throws DataValidationFailedException
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();
48 private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
49 return combine().getRootNode();
52 private NormalizedNodeAggregator combine() throws DataValidationFailedException {
53 DataTreeModification mod = dataTree.takeSnapshot().newModification();
55 for (Optional<NormalizedNode<?,?>> node : nodes) {
56 if (node.isPresent()) {
57 mod.merge(rootIdentifier, node.get());
61 dataTree.validate(mod);
62 final DataTreeCandidate candidate = dataTree.prepare(mod);
63 dataTree.commit(candidate);
68 private Optional<NormalizedNode<?, ?>> getRootNode() {
69 return dataTree.takeSnapshot().readNode(rootIdentifier);