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.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
26 import org.opendaylight.mdsal.common.api.MappingCheckedFuture;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 * Adapter between the legacy controller API-based DOMStore and the mdsal API-based DOMStore.
35 * @author Thomas Pantelis
37 public class LegacyDOMStoreAdapter extends ForwardingObject implements DOMStore, AutoCloseable {
38 public static final ExceptionMapper<ReadFailedException> READ_EX_MAPPER =
39 new ExceptionMapper<ReadFailedException>("read", ReadFailedException.class) {
41 protected ReadFailedException newWithCause(String message, Throwable cause) {
42 if (cause instanceof org.opendaylight.mdsal.common.api.ReadFailedException) {
43 return new ReadFailedException(cause.getMessage(), cause.getCause());
46 return new ReadFailedException(message, cause);
50 private final DistributedDataStoreInterface delegate;
52 public LegacyDOMStoreAdapter(DistributedDataStoreInterface delegate) {
53 this.delegate = delegate;
57 public DOMStoreReadTransaction newReadOnlyTransaction() {
58 return new DOMStoreTransactionAdapter(delegate().newReadOnlyTransaction());
62 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
63 return new DOMStoreTransactionAdapter(delegate().newWriteOnlyTransaction());
67 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
68 return new DOMStoreTransactionAdapter(delegate().newReadWriteTransaction());
72 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L>
73 registerChangeListener(YangInstanceIdentifier path, L listener, DataChangeScope scope) {
74 return delegate().registerChangeListener(path, listener, scope);
78 public DOMStoreTransactionChain createTransactionChain() {
79 final org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain delegateChain =
80 delegate().createTransactionChain();
81 return new DOMStoreTransactionChain() {
83 public DOMStoreReadTransaction newReadOnlyTransaction() {
84 return new DOMStoreTransactionAdapter(delegateChain.newReadOnlyTransaction());
88 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
89 return new DOMStoreTransactionAdapter(delegateChain.newWriteOnlyTransaction());
93 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
94 return new DOMStoreTransactionAdapter(delegateChain.newReadWriteTransaction());
99 delegateChain.close();
105 public void close() {
109 protected DistributedDataStoreInterface delegate() {
113 private static class DOMStoreTransactionAdapter implements DOMStoreReadWriteTransaction {
114 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate;
115 private final org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate;
116 private final Object identifier;
118 DOMStoreTransactionAdapter(@Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate) {
119 this.readDelegate = Preconditions.checkNotNull(readDelegate);
120 this.identifier = readDelegate.getIdentifier();
121 this.writeDelegate = null;
124 DOMStoreTransactionAdapter(
125 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate) {
126 this.writeDelegate = Preconditions.checkNotNull(writeDelegate);
127 this.identifier = writeDelegate.getIdentifier();
128 this.readDelegate = null;
131 DOMStoreTransactionAdapter(
132 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction rwDelegate) {
133 this.readDelegate = Preconditions.checkNotNull(rwDelegate);
134 this.writeDelegate = rwDelegate;
135 this.identifier = readDelegate.getIdentifier();
139 public Object getIdentifier() {
144 public void close() {
145 if (writeDelegate != null) {
146 writeDelegate.close();
148 readDelegate.close();
153 public void write(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
154 writeDelegate.write(path, data);
158 public void merge(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
159 writeDelegate.merge(path, data);
163 public void delete(YangInstanceIdentifier path) {
164 writeDelegate.delete(path);
168 public DOMStoreThreePhaseCommitCohort ready() {
169 final org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort cohort = writeDelegate.ready();
170 return new DOMStoreThreePhaseCommitCohort() {
172 public ListenableFuture<Boolean> canCommit() {
173 return cohort.canCommit();
177 public ListenableFuture<Void> preCommit() {
178 return cohort.preCommit();
182 public ListenableFuture<Void> commit() {
183 return cohort.commit();
187 public ListenableFuture<Void> abort() {
188 return cohort.abort();
194 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(YangInstanceIdentifier path) {
195 return MappingCheckedFuture.create(readDelegate.read(path), READ_EX_MAPPER);
199 public CheckedFuture<Boolean, ReadFailedException> exists(YangInstanceIdentifier path) {
200 return MappingCheckedFuture.create(readDelegate.exists(path), READ_EX_MAPPER);