33f15e3f673d0d56e6f1376d753455391a3c3aa7
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / ShardedDOMDataWriteTransaction.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 package org.opendaylight.controller.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
20 import java.util.concurrent.atomic.AtomicLong;
21 import javax.annotation.concurrent.GuardedBy;
22 import javax.annotation.concurrent.NotThreadSafe;
23 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @NotThreadSafe
38 final class ShardedDOMDataWriteTransaction implements DOMDataWriteTransaction {
39     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataWriteTransaction.class);
40     private static final AtomicLong COUNTER = new AtomicLong();
41     private final Map<DOMDataTreeIdentifier, DOMStoreWriteTransaction> idToTransaction;
42     private final ShardedDOMDataTreeProducer producer;
43     private final String identifier;
44     @GuardedBy("this")
45     private boolean closed =  false;
46
47     ShardedDOMDataWriteTransaction(final ShardedDOMDataTreeProducer producer, final Map<DOMDataTreeIdentifier, DOMStoreWriteTransaction> idToTransaction) {
48         this.producer = Preconditions.checkNotNull(producer);
49         this.idToTransaction = Preconditions.checkNotNull(idToTransaction);
50         this.identifier = "SHARDED-DOM-" + COUNTER.getAndIncrement();
51     }
52
53     // FIXME: use atomic operations
54     @GuardedBy("this")
55     private DOMStoreWriteTransaction lookup(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
56         final DOMDataTreeIdentifier id = new DOMDataTreeIdentifier(store, path);
57
58         for (Entry<DOMDataTreeIdentifier, DOMStoreWriteTransaction> e : idToTransaction.entrySet()) {
59             if (e.getKey().contains(id)) {
60                 return e.getValue();
61             }
62         }
63
64         throw new IllegalArgumentException(String.format("Path %s is not acessible from transaction %s", id, this));
65     }
66
67     @Override
68     public String getIdentifier() {
69         return identifier;
70     }
71
72     @Override
73     public synchronized boolean cancel() {
74         if (closed) {
75             return false;
76         }
77
78         LOG.debug("Cancelling transaction {}", identifier);
79         for (DOMStoreWriteTransaction tx : ImmutableSet.copyOf(idToTransaction.values())) {
80             tx.close();
81         }
82
83         closed = true;
84         producer.cancelTransaction(this);
85         return true;
86     }
87
88     @Override
89     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
90         Preconditions.checkState(!closed, "Transaction %s is already closed", identifier);
91
92         final Set<DOMStoreWriteTransaction> txns = ImmutableSet.copyOf(idToTransaction.values());
93         final List<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
94         for (DOMStoreWriteTransaction tx : txns) {
95             cohorts.add(tx.ready());
96         }
97
98         try {
99             return Futures.immediateCheckedFuture(new CommitCoordinationTask(this, cohorts, null).call());
100         } catch (TransactionCommitFailedException e) {
101             return Futures.immediateFailedCheckedFuture(e);
102         }
103     }
104
105     @Override
106     @Deprecated
107     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
108         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
109     }
110
111     @Override
112     public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
113         lookup(store, path).delete(path);
114     }
115
116     @Override
117     public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
118         lookup(store, path).write(path, data);
119     }
120
121     @Override
122     public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
123         lookup(store, path).merge(path, data);
124     }
125 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.