HONEYCOMB-130: Separate v3po plugin from HC infra
[honeycomb.git] / infra / data-impl / src / main / java / io / fd / honeycomb / v3po / data / impl / ReadWriteTransaction.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.v3po.data.impl;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35
36 /**
37  * Composite DOM transaction that delegates reads to a {@link DOMDataReadTransaction} delegate and writes to a {@link
38  * DOMDataWriteTransaction} delegate.
39  */
40 final class ReadWriteTransaction implements DOMDataReadWriteTransaction {
41
42     private final DOMDataReadOnlyTransaction delegateReadTx;
43     private final DOMDataWriteTransaction delegateWriteTx;
44
45     ReadWriteTransaction(@Nonnull final DOMDataReadOnlyTransaction delegateReadTx,
46                          @Nonnull final DOMDataWriteTransaction delegateWriteTx) {
47         this.delegateReadTx = Preconditions.checkNotNull(delegateReadTx, "delegateReadTx should not be null");
48         this.delegateWriteTx = Preconditions.checkNotNull(delegateWriteTx, "delegateWriteTx should not be null");
49     }
50
51     @Override
52     public boolean cancel() {
53         delegateReadTx.close();
54         return delegateWriteTx.cancel();
55     }
56
57     @Override
58     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
59                     final NormalizedNode<?, ?> data) {
60         delegateWriteTx.put(store, path, data);
61     }
62
63     @Override
64     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
65                       final NormalizedNode<?, ?> data) {
66         delegateWriteTx.merge(store, path, data);
67     }
68
69     @Override
70     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
71         delegateWriteTx.delete(store, path);
72     }
73
74     @Override
75     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
76         return delegateWriteTx.submit();
77     }
78
79     @Override
80     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
81         return delegateWriteTx.commit();
82     }
83
84     @Override
85     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
86                                                                                    final YangInstanceIdentifier path) {
87         return delegateReadTx.read(store, path);
88     }
89
90     @Override
91     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
92                                                               final YangInstanceIdentifier path) {
93         return delegateReadTx.exists(store, path);
94     }
95
96     @Override
97     public Object getIdentifier() {
98         return this;
99     }
100 }
101