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

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