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
9 package org.opendaylight.controller.cluster.datastore.utils;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ExecutorService;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 public class NormalizedNodeAggregator {
28 private static final ExecutorService executorService = MoreExecutors.newDirectExecutorService();
30 private final YangInstanceIdentifier rootIdentifier;
31 private final List<Optional<NormalizedNode<?, ?>>> nodes;
32 private final InMemoryDOMDataStore dataStore;
34 NormalizedNodeAggregator(YangInstanceIdentifier rootIdentifier, List<Optional<NormalizedNode<?, ?>>> nodes,
35 SchemaContext schemaContext){
37 this.rootIdentifier = rootIdentifier;
39 this.dataStore = new InMemoryDOMDataStore("aggregator", executorService);
40 this.dataStore.onGlobalContextUpdated(schemaContext);
44 * Combine data from all the nodes in the list into a tree with root as rootIdentifier
47 * @param schemaContext
49 * @throws ExecutionException
50 * @throws InterruptedException
52 public static Optional<NormalizedNode<?,?>> aggregate(YangInstanceIdentifier rootIdentifier,
53 List<Optional<NormalizedNode<?, ?>>> nodes,
54 SchemaContext schemaContext)
55 throws ExecutionException, InterruptedException {
56 return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext).aggregate();
59 private Optional<NormalizedNode<?,?>> aggregate() throws ExecutionException, InterruptedException {
60 return combine().getRootNode();
63 private NormalizedNodeAggregator combine() throws InterruptedException, ExecutionException {
64 DOMStoreWriteTransaction domStoreWriteTransaction = dataStore.newWriteOnlyTransaction();
66 for(Optional<NormalizedNode<?,?>> node : nodes) {
67 if(node.isPresent()) {
68 domStoreWriteTransaction.merge(rootIdentifier, node.get());
71 DOMStoreThreePhaseCommitCohort ready = domStoreWriteTransaction.ready();
72 ready.canCommit().get();
73 ready.preCommit().get();
79 private Optional<NormalizedNode<?, ?>> getRootNode() throws InterruptedException, ExecutionException {
80 DOMStoreReadTransaction readTransaction = dataStore.newReadOnlyTransaction();
82 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
83 readTransaction.read(rootIdentifier);