20a786e774e58ad1ca859ba0f432c1c0f79ca52d
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / connect / dom / BindingToDomTransaction.java
1 package org.opendaylight.controller.sal.binding.impl.connect.dom;
2
3 import java.util.Collections;
4 import java.util.concurrent.ConcurrentMap;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.Future;
7 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
8 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
9 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
10 import org.opendaylight.controller.sal.common.util.Rpcs;
11 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
13 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.common.RpcError;
15 import org.opendaylight.yangtools.yang.common.RpcResult;
16
17 class BindingToDomTransaction implements
18     DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends DataObject>, DataObject> {
19
20     private final DataModificationTransaction backing;
21     private final DataModification<InstanceIdentifier<? extends DataObject>, DataObject> modification;
22     private final ConcurrentMap<Object, BindingToDomTransaction> domOpenedTransactions;
23
24     public BindingToDomTransaction(final DataModificationTransaction backing,
25         final DataModification<InstanceIdentifier<? extends DataObject>, DataObject> modification,
26         ConcurrentMap<Object, BindingToDomTransaction> domOpenedTransactions) {
27         this.backing = backing;
28         this.modification = modification;
29         this.domOpenedTransactions = domOpenedTransactions;
30         this.domOpenedTransactions.put(backing.getIdentifier(), this);
31     }
32
33     @Override
34     public DataModification<InstanceIdentifier<? extends DataObject>, DataObject> getModification() {
35         return modification;
36     }
37
38     @Override
39     public RpcResult<Void> finish() throws IllegalStateException {
40         Future<RpcResult<TransactionStatus>> result = backing.commit();
41         try {
42             RpcResult<TransactionStatus> biResult = result.get();
43             domOpenedTransactions.remove(backing.getIdentifier());
44             return Rpcs.getRpcResult(biResult.isSuccessful(), null, biResult.getErrors());
45         } catch (InterruptedException e) {
46             throw new IllegalStateException("", e);
47         } catch (ExecutionException e) {
48             throw new IllegalStateException("", e);
49         } finally {
50             domOpenedTransactions.remove(backing.getIdentifier());
51         }
52     }
53
54     @Override
55     public RpcResult<Void> rollback() throws IllegalStateException {
56         domOpenedTransactions.remove(backing.getIdentifier());
57         return Rpcs.getRpcResult(true, null, Collections.<RpcError> emptySet());
58     }
59 }

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