6e89c8e44c579d22d8d8a947cc18ae167f3a01bb
[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 /**
38  * @deprecated To be removed with {@link ShardedDOMDataTree}.
39  */
40 @Deprecated
41 @NotThreadSafe
42 final class ShardedDOMDataWriteTransaction implements DOMDataWriteTransaction {
43     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataWriteTransaction.class);
44     private static final AtomicLong COUNTER = new AtomicLong();
45     private final Map<DOMDataTreeIdentifier, DOMStoreWriteTransaction> idToTransaction;
46     private final ShardedDOMDataTreeProducer producer;
47     private final String identifier;
48     @GuardedBy("this")
49     private boolean closed =  false;
50
51     ShardedDOMDataWriteTransaction(final ShardedDOMDataTreeProducer producer, final Map<DOMDataTreeIdentifier, DOMStoreWriteTransaction> idToTransaction) {
52         this.producer = Preconditions.checkNotNull(producer);
53         this.idToTransaction = Preconditions.checkNotNull(idToTransaction);
54         this.identifier = "SHARDED-DOM-" + COUNTER.getAndIncrement();
55     }
56
57     // FIXME: use atomic operations
58     @GuardedBy("this")
59     private DOMStoreWriteTransaction lookup(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
60         final DOMDataTreeIdentifier id = new DOMDataTreeIdentifier(store, path);
61
62         for (Entry<DOMDataTreeIdentifier, DOMStoreWriteTransaction> e : idToTransaction.entrySet()) {
63             if (e.getKey().contains(id)) {
64                 return e.getValue();
65             }
66         }
67
68         throw new IllegalArgumentException(String.format("Path %s is not acessible from transaction %s", id, this));
69     }
70
71     @Override
72     public String getIdentifier() {
73         return identifier;
74     }
75
76     @Override
77     public synchronized boolean cancel() {
78         if (closed) {
79             return false;
80         }
81
82         LOG.debug("Cancelling transaction {}", identifier);
83         for (DOMStoreWriteTransaction tx : ImmutableSet.copyOf(idToTransaction.values())) {
84             tx.close();
85         }
86
87         closed = true;
88         producer.cancelTransaction(this);
89         return true;
90     }
91
92     @Override
93     public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
94         Preconditions.checkState(!closed, "Transaction %s is already closed", identifier);
95
96         final Set<DOMStoreWriteTransaction> txns = ImmutableSet.copyOf(idToTransaction.values());
97         final List<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
98         for (DOMStoreWriteTransaction tx : txns) {
99             cohorts.add(tx.ready());
100         }
101
102         try {
103             return Futures.immediateCheckedFuture(new CommitCoordinationTask(this, cohorts, null).call());
104         } catch (TransactionCommitFailedException e) {
105             return Futures.immediateFailedCheckedFuture(e);
106         }
107     }
108
109     @Override
110     @Deprecated
111     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
112         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
113     }
114
115     @Override
116     public synchronized void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
117         lookup(store, path).delete(path);
118     }
119
120     @Override
121     public synchronized void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
122         lookup(store, path).write(path, data);
123     }
124
125     @Override
126     public synchronized void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
127         lookup(store, path).merge(path, data);
128     }
129 }

©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.