Merge "Stop swallowing exceptions in XmlElement optional methods."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / NormalizedNodeAggregator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.utils;
10
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;
25
26 public class NormalizedNodeAggregator {
27
28     private static final ExecutorService executorService = MoreExecutors.newDirectExecutorService();
29
30     private final YangInstanceIdentifier rootIdentifier;
31     private final List<Optional<NormalizedNode<?, ?>>> nodes;
32     private final InMemoryDOMDataStore dataStore;
33
34     NormalizedNodeAggregator(YangInstanceIdentifier rootIdentifier, List<Optional<NormalizedNode<?, ?>>> nodes,
35                              SchemaContext schemaContext){
36
37         this.rootIdentifier = rootIdentifier;
38         this.nodes = nodes;
39         this.dataStore = new InMemoryDOMDataStore("aggregator", executorService);
40         this.dataStore.onGlobalContextUpdated(schemaContext);
41     }
42
43     /**
44      * Combine data from all the nodes in the list into a tree with root as rootIdentifier
45      *
46      * @param nodes
47      * @param schemaContext
48      * @return
49      * @throws ExecutionException
50      * @throws InterruptedException
51      */
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();
57     }
58
59     private Optional<NormalizedNode<?,?>> aggregate() throws ExecutionException, InterruptedException {
60         return combine().getRootNode();
61     }
62
63     private NormalizedNodeAggregator combine() throws InterruptedException, ExecutionException {
64         DOMStoreWriteTransaction domStoreWriteTransaction = dataStore.newWriteOnlyTransaction();
65
66         for(Optional<NormalizedNode<?,?>> node : nodes) {
67             if(node.isPresent()) {
68                 domStoreWriteTransaction.merge(rootIdentifier, node.get());
69             }
70         }
71         DOMStoreThreePhaseCommitCohort ready = domStoreWriteTransaction.ready();
72         ready.canCommit().get();
73         ready.preCommit().get();
74         ready.commit().get();
75
76         return this;
77     }
78
79     private Optional<NormalizedNode<?, ?>> getRootNode() throws InterruptedException, ExecutionException {
80         DOMStoreReadTransaction readTransaction = dataStore.newReadOnlyTransaction();
81
82         CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
83                 readTransaction.read(rootIdentifier);
84
85         return read.get();
86     }
87
88
89 }