/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.cluster.datastore.utils; import com.google.common.base.Optional; import java.util.List; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public class NormalizedNodeAggregator { private final YangInstanceIdentifier rootIdentifier; private final List>> nodes; private final DataTree dataTree; private NormalizedNodeAggregator(final YangInstanceIdentifier rootIdentifier, final List>> nodes, final SchemaContext schemaContext, LogicalDatastoreType logicalDatastoreType) { this.rootIdentifier = rootIdentifier; this.nodes = nodes; this.dataTree = InMemoryDataTreeFactory.getInstance().create( logicalDatastoreType == LogicalDatastoreType.CONFIGURATION ? TreeType.CONFIGURATION : TreeType.OPERATIONAL); this.dataTree.setSchemaContext(schemaContext); } /** * Combine data from all the nodes in the list into a tree with root as rootIdentifier. */ public static Optional> aggregate(final YangInstanceIdentifier rootIdentifier, final List>> nodes, final SchemaContext schemaContext, LogicalDatastoreType logicalDatastoreType) throws DataValidationFailedException { return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext, logicalDatastoreType).aggregate(); } private Optional> aggregate() throws DataValidationFailedException { return combine().getRootNode(); } private NormalizedNodeAggregator combine() throws DataValidationFailedException { final DataTreeModification mod = dataTree.takeSnapshot().newModification(); for (final Optional> node : nodes) { if (node.isPresent()) { mod.merge(rootIdentifier, node.get()); } } mod.ready(); dataTree.validate(mod); final DataTreeCandidate candidate = dataTree.prepare(mod); dataTree.commit(candidate); return this; } private Optional> getRootNode() { return dataTree.takeSnapshot().readNode(rootIdentifier); } }