Proxy MD-SAL interfaces in DOMMountPointServiceImpl
[controller.git] / opendaylight / md-sal / sal-dom-compat / src / main / java / org / opendaylight / controller / sal / core / compat / DOMTransactionChainAdapter.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.sal.core.compat;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.collect.ForwardingObject;
15 import java.util.function.Function;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
28
29 @Deprecated
30 public class DOMTransactionChainAdapter extends ForwardingObject
31         implements org.opendaylight.mdsal.dom.api.DOMTransactionChain, TransactionChainListener {
32     private final Cache<AsyncTransaction<?, ?>, DOMDataTreeTransaction> transactions = CacheBuilder.newBuilder()
33             .weakKeys().weakValues().build();
34
35     private final DOMTransactionChainListener listener;
36     private final DOMTransactionChain delegate;
37
38     public DOMTransactionChainAdapter(final DOMTransactionChainListener listener,
39             final Function<TransactionChainListener, DOMTransactionChain> function) {
40         this.listener = requireNonNull(listener);
41         this.delegate = function.apply(this);
42     }
43
44     @Override
45     public void close() {
46         delegate().close();
47         transactions.invalidateAll();
48     }
49
50     @Override
51     public DOMDataTreeReadTransaction newReadOnlyTransaction() {
52         final DOMDataReadOnlyTransaction tx = delegate.newReadOnlyTransaction();
53         return track(tx, new DOMDataTreeReadTransactionAdapter(tx) {
54             @Override
55             public void close() {
56                 untrack(delegate());
57                 super.close();
58             }
59         });
60     }
61
62     @Override
63     public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
64         final DOMDataWriteTransaction tx = delegate.newWriteOnlyTransaction();
65         return track(tx, new DOMDataTreeWriteTransactionAdapter(tx) {
66             @Override
67             public boolean cancel() {
68                 untrack(delegate());
69                 return super.cancel();
70             }
71         });
72     }
73
74     @Override
75     public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
76         final DOMDataReadWriteTransaction tx = delegate.newReadWriteTransaction();
77         return track(tx, new DOMDataTreeReadWriteTransactionAdapter(tx) {
78             @Override
79             public boolean cancel() {
80                 untrack(delegate());
81                 return super.cancel();
82             }
83
84             @Override
85             public void close() {
86                 untrack(delegate());
87                 super.close();
88             }
89         });
90     }
91
92     @Override
93     public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction,
94             final Throwable cause) {
95         listener.onTransactionChainFailed(this, null, cause);
96     }
97
98     @Override
99     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
100         listener.onTransactionChainSuccessful(this);
101     }
102
103     @Override
104     protected DOMTransactionChain delegate() {
105         return delegate;
106     }
107
108     private <T extends DOMDataTreeTransaction> T track(final AsyncTransaction<?, ?> controllerTx, final T mdsalTx) {
109         transactions.put(controllerTx, mdsalTx);
110         return mdsalTx;
111     }
112
113     void untrack(final AsyncTransaction<?, ?> controllerTx) {
114         transactions.invalidate(controllerTx);
115     }
116 }

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