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 javax.annotation.Nonnull;
16 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.sal.core.compat.ReadFailedExceptionAdapter;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
25 import org.opendaylight.mdsal.common.api.MappingCheckedFuture;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 * Adapter between the legacy controller API-based DOMStore and the mdsal API-based DOMStore.
32 * @author Thomas Pantelis
34 public class LegacyDOMStoreAdapter extends ForwardingObject implements DOMStore, AutoCloseable {
35 private final DistributedDataStoreInterface delegate;
37 public LegacyDOMStoreAdapter(DistributedDataStoreInterface delegate) {
38 this.delegate = delegate;
42 public DOMStoreReadTransaction newReadOnlyTransaction() {
43 return new DOMStoreTransactionAdapter(delegate().newReadOnlyTransaction());
47 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
48 return new DOMStoreTransactionAdapter(delegate().newWriteOnlyTransaction());
52 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
53 return new DOMStoreTransactionAdapter(delegate().newReadWriteTransaction());
57 public DOMStoreTransactionChain createTransactionChain() {
58 final org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain delegateChain =
59 delegate().createTransactionChain();
60 return new DOMStoreTransactionChain() {
62 public DOMStoreReadTransaction newReadOnlyTransaction() {
63 return new DOMStoreTransactionAdapter(delegateChain.newReadOnlyTransaction());
67 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
68 return new DOMStoreTransactionAdapter(delegateChain.newWriteOnlyTransaction());
72 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
73 return new DOMStoreTransactionAdapter(delegateChain.newReadWriteTransaction());
78 delegateChain.close();
88 protected DistributedDataStoreInterface delegate() {
92 private static class DOMStoreTransactionAdapter implements DOMStoreReadWriteTransaction {
93 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate;
94 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate;
95 private final Object identifier;
97 DOMStoreTransactionAdapter(@Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate) {
98 this.readDelegate = Preconditions.checkNotNull(readDelegate);
99 this.identifier = readDelegate.getIdentifier();
100 this.writeDelegate = null;
103 DOMStoreTransactionAdapter(
104 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate) {
105 this.writeDelegate = Preconditions.checkNotNull(writeDelegate);
106 this.identifier = writeDelegate.getIdentifier();
107 this.readDelegate = null;
110 DOMStoreTransactionAdapter(
111 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction rwDelegate) {
112 this.readDelegate = Preconditions.checkNotNull(rwDelegate);
113 this.writeDelegate = rwDelegate;
114 this.identifier = readDelegate.getIdentifier();
118 public Object getIdentifier() {
123 public void close() {
124 if (writeDelegate != null) {
125 writeDelegate.close();
127 readDelegate.close();
132 public void write(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
133 writeDelegate.write(path, data);
137 public void merge(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
138 writeDelegate.merge(path, data);
142 public void delete(YangInstanceIdentifier path) {
143 writeDelegate.delete(path);
147 public DOMStoreThreePhaseCommitCohort ready() {
148 final org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort cohort = writeDelegate.ready();
149 return new DOMStoreThreePhaseCommitCohort() {
151 public ListenableFuture<Boolean> canCommit() {
152 return cohort.canCommit();
156 public ListenableFuture<Void> preCommit() {
157 return cohort.preCommit();
161 public ListenableFuture<Void> commit() {
162 return cohort.commit();
166 public ListenableFuture<Void> abort() {
167 return cohort.abort();
173 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(YangInstanceIdentifier path) {
174 return MappingCheckedFuture.create(readDelegate.read(path), ReadFailedExceptionAdapter.INSTANCE);
178 public CheckedFuture<Boolean, ReadFailedException> exists(YangInstanceIdentifier path) {
179 return MappingCheckedFuture.create(readDelegate.exists(path), ReadFailedExceptionAdapter.INSTANCE);