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.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;
23 public class NormalizedNodeAggregator {
24 private final YangInstanceIdentifier rootIdentifier;
25 private final List<Optional<NormalizedNode<?, ?>>> nodes;
26 private final DataTree dataTree;
28 private NormalizedNodeAggregator(final YangInstanceIdentifier rootIdentifier, final List<Optional<NormalizedNode<?, ?>>> nodes,
29 final SchemaContext schemaContext, LogicalDatastoreType logicalDatastoreType) {
30 this.rootIdentifier = rootIdentifier;
32 this.dataTree = InMemoryDataTreeFactory.getInstance().create(
33 logicalDatastoreType == LogicalDatastoreType.CONFIGURATION ? TreeType.CONFIGURATION :
34 TreeType.OPERATIONAL);
35 this.dataTree.setSchemaContext(schemaContext);
39 * Combine data from all the nodes in the list into a tree with root as rootIdentifier
42 * @param schemaContext
43 * @param logicalDatastoreType
45 * @throws DataValidationFailedException
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();
54 private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
55 return combine().getRootNode();
58 private NormalizedNodeAggregator combine() throws DataValidationFailedException {
59 final DataTreeModification mod = dataTree.takeSnapshot().newModification();
61 for (final Optional<NormalizedNode<?,?>> node : nodes) {
62 if (node.isPresent()) {
63 mod.merge(rootIdentifier, node.get());
67 dataTree.validate(mod);
68 final DataTreeCandidate candidate = dataTree.prepare(mod);
69 dataTree.commit(candidate);
74 private Optional<NormalizedNode<?, ?>> getRootNode() {
75 return dataTree.takeSnapshot().readNode(rootIdentifier);