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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ForwardingObject;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
18 import org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.sal.core.compat.ReadFailedExceptionAdapter;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 * Adapter between the legacy controller API-based DOMStore and the mdsal API-based DOMStore.
33 * @author Thomas Pantelis
35 public class LegacyDOMStoreAdapter extends ForwardingObject implements DOMStore, AutoCloseable {
36 private final DistributedDataStoreInterface delegate;
38 public LegacyDOMStoreAdapter(DistributedDataStoreInterface delegate) {
39 this.delegate = delegate;
43 public DOMStoreReadTransaction newReadOnlyTransaction() {
44 return new DOMStoreTransactionAdapter(delegate().newReadOnlyTransaction());
48 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
49 return new DOMStoreTransactionAdapter(delegate().newWriteOnlyTransaction());
53 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
54 return new DOMStoreTransactionAdapter(delegate().newReadWriteTransaction());
58 public DOMStoreTransactionChain createTransactionChain() {
59 final org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain delegateChain =
60 delegate().createTransactionChain();
61 return new DOMStoreTransactionChain() {
63 public DOMStoreReadTransaction newReadOnlyTransaction() {
64 return new DOMStoreTransactionAdapter(delegateChain.newReadOnlyTransaction());
68 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
69 return new DOMStoreTransactionAdapter(delegateChain.newWriteOnlyTransaction());
73 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
74 return new DOMStoreTransactionAdapter(delegateChain.newReadWriteTransaction());
79 delegateChain.close();
89 protected DistributedDataStoreInterface delegate() {
93 private static class DOMStoreTransactionAdapter implements DOMStoreReadWriteTransaction {
94 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate;
95 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate;
96 private final Object identifier;
98 DOMStoreTransactionAdapter(@Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate) {
99 this.readDelegate = Preconditions.checkNotNull(readDelegate);
100 this.identifier = readDelegate.getIdentifier();
101 this.writeDelegate = null;
104 DOMStoreTransactionAdapter(
105 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate) {
106 this.writeDelegate = Preconditions.checkNotNull(writeDelegate);
107 this.identifier = writeDelegate.getIdentifier();
108 this.readDelegate = null;
111 DOMStoreTransactionAdapter(
112 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction rwDelegate) {
113 this.readDelegate = Preconditions.checkNotNull(rwDelegate);
114 this.writeDelegate = rwDelegate;
115 this.identifier = readDelegate.getIdentifier();
119 public Object getIdentifier() {
124 public void close() {
125 if (writeDelegate != null) {
126 writeDelegate.close();
128 readDelegate.close();
133 public void write(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
134 writeDelegate.write(path, data);
138 public void merge(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
139 writeDelegate.merge(path, data);
143 public void delete(YangInstanceIdentifier path) {
144 writeDelegate.delete(path);
148 public DOMStoreThreePhaseCommitCohort ready() {
149 final org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort cohort = writeDelegate.ready();
150 return new DOMStoreThreePhaseCommitCohort() {
152 public ListenableFuture<Boolean> canCommit() {
153 return cohort.canCommit();
157 public ListenableFuture<Void> preCommit() {
158 return cohort.preCommit();
162 public ListenableFuture<Void> commit() {
163 return cohort.commit();
167 public ListenableFuture<Void> abort() {
168 return cohort.abort();
174 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(YangInstanceIdentifier path) {
175 return MappingCheckedFuture.create(readDelegate.read(path).transform(
176 Optional::fromJavaUtil, MoreExecutors.directExecutor()), ReadFailedExceptionAdapter.INSTANCE);
180 public CheckedFuture<Boolean, ReadFailedException> exists(YangInstanceIdentifier path) {
181 return MappingCheckedFuture.create(readDelegate.exists(path), ReadFailedExceptionAdapter.INSTANCE);