2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.compat;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ForwardingObject;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
19 import org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.sal.core.compat.ReadFailedExceptionAdapter;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 * Adapter between the legacy controller API-based DOMStore and the mdsal API-based DOMStore.
34 * @author Thomas Pantelis
36 public class LegacyDOMStoreAdapter extends ForwardingObject implements DOMStore, AutoCloseable {
37 private final DistributedDataStoreInterface delegate;
39 public LegacyDOMStoreAdapter(DistributedDataStoreInterface delegate) {
40 this.delegate = delegate;
44 public DOMStoreReadTransaction newReadOnlyTransaction() {
45 return new DOMStoreTransactionAdapter(delegate().newReadOnlyTransaction());
49 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
50 return new DOMStoreTransactionAdapter(delegate().newWriteOnlyTransaction());
54 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
55 return new DOMStoreTransactionAdapter(delegate().newReadWriteTransaction());
59 public DOMStoreTransactionChain createTransactionChain() {
60 final org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain delegateChain =
61 delegate().createTransactionChain();
62 return new DOMStoreTransactionChain() {
64 public DOMStoreReadTransaction newReadOnlyTransaction() {
65 return new DOMStoreTransactionAdapter(delegateChain.newReadOnlyTransaction());
69 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
70 return new DOMStoreTransactionAdapter(delegateChain.newWriteOnlyTransaction());
74 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
75 return new DOMStoreTransactionAdapter(delegateChain.newReadWriteTransaction());
80 delegateChain.close();
90 protected DistributedDataStoreInterface delegate() {
94 private static class DOMStoreTransactionAdapter implements DOMStoreReadWriteTransaction {
95 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate;
96 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate;
97 private final Object identifier;
99 DOMStoreTransactionAdapter(org.opendaylight.mdsal.dom.spi.store.@NonNull DOMStoreReadTransaction readDelegate) {
100 this.readDelegate = requireNonNull(readDelegate);
101 this.identifier = readDelegate.getIdentifier();
102 this.writeDelegate = null;
105 DOMStoreTransactionAdapter(
106 org.opendaylight.mdsal.dom.spi.store.@NonNull DOMStoreWriteTransaction writeDelegate) {
107 this.writeDelegate = requireNonNull(writeDelegate);
108 this.identifier = writeDelegate.getIdentifier();
109 this.readDelegate = null;
112 DOMStoreTransactionAdapter(
113 org.opendaylight.mdsal.dom.spi.store.@NonNull DOMStoreReadWriteTransaction rwDelegate) {
114 this.readDelegate = requireNonNull(rwDelegate);
115 this.writeDelegate = rwDelegate;
116 this.identifier = readDelegate.getIdentifier();
120 public Object getIdentifier() {
125 public void close() {
126 if (writeDelegate != null) {
127 writeDelegate.close();
129 readDelegate.close();
134 public void write(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
135 writeDelegate.write(path, data);
139 public void merge(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
140 writeDelegate.merge(path, data);
144 public void delete(YangInstanceIdentifier path) {
145 writeDelegate.delete(path);
149 public DOMStoreThreePhaseCommitCohort ready() {
150 final org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort cohort = writeDelegate.ready();
151 return new DOMStoreThreePhaseCommitCohort() {
153 public ListenableFuture<Boolean> canCommit() {
154 return cohort.canCommit();
158 public ListenableFuture<Void> preCommit() {
159 return cohort.preCommit();
163 public ListenableFuture<Void> commit() {
164 return cohort.commit();
168 public ListenableFuture<Void> abort() {
169 return cohort.abort();
175 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(YangInstanceIdentifier path) {
176 return MappingCheckedFuture.create(readDelegate.read(path).transform(
177 Optional::fromJavaUtil, MoreExecutors.directExecutor()), ReadFailedExceptionAdapter.INSTANCE);
181 public CheckedFuture<Boolean, ReadFailedException> exists(YangInstanceIdentifier path) {
182 return MappingCheckedFuture.create(readDelegate.exists(path), ReadFailedExceptionAdapter.INSTANCE);