2 * Copyright (c) 2014 Cisco 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.md.sal.binding.impl;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableSet;
14 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeService;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
26 import org.opendaylight.controller.md.sal.dom.api.DOMService;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 * The DataBrokerImpl simply defers to the DOMDataBroker for all its operations.
31 * All transactions and listener registrations are wrapped by the DataBrokerImpl
32 * to allow binding aware components to use the DataBroker transparently.
34 * Besides this the DataBrokerImpl and it's collaborators also cache data that
35 * is already transformed from the binding independent to binding aware format
39 public class BindingDOMDataBrokerAdapter extends AbstractForwardedDataBroker implements DataBroker {
42 static final Factory<DataBroker> BUILDER_FACTORY = new BindingDOMAdapterBuilder.Factory<DataBroker>() {
45 public BindingDOMAdapterBuilder<DataBroker> newBuilder() {
50 private final DataTreeChangeService treeChangeService;
52 public BindingDOMDataBrokerAdapter(final DOMDataBroker domDataBroker, final BindingToNormalizedNodeCodec codec) {
53 super(domDataBroker, codec);
54 final DOMDataTreeChangeService domTreeChange = (DOMDataTreeChangeService) domDataBroker.getSupportedExtensions().get(DOMDataTreeChangeService.class);
55 if(domTreeChange != null) {
56 treeChangeService = BindingDOMDataTreeChangeServiceAdapter.create(codec, domTreeChange);
58 treeChangeService = null;
64 public ReadOnlyTransaction newReadOnlyTransaction() {
65 return new BindingDOMReadTransactionAdapter(getDelegate().newReadOnlyTransaction(),getCodec());
69 public ReadWriteTransaction newReadWriteTransaction() {
70 return new BindingDOMReadWriteTransactionAdapter(getDelegate().newReadWriteTransaction(),getCodec());
74 public WriteTransaction newWriteOnlyTransaction() {
75 return new BindingDOMWriteTransactionAdapter<>(getDelegate().newWriteOnlyTransaction(),getCodec());
79 public BindingTransactionChain createTransactionChain(final TransactionChainListener listener) {
80 return new BindingDOMTransactionChainAdapter(getDelegate(), getCodec(), listener);
83 private static class Builder extends BindingDOMAdapterBuilder<DataBroker> {
86 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
87 return ImmutableSet.of(DOMDataBroker.class);
91 protected DataBroker createInstance(final BindingToNormalizedNodeCodec codec,
92 final ClassToInstanceMap<DOMService> delegates) {
93 final DOMDataBroker domDataBroker = delegates.getInstance(DOMDataBroker.class);
94 return new BindingDOMDataBrokerAdapter(domDataBroker, codec);
99 public <L extends DataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(
100 final DataTreeIdentifier treeId, final L listener) {
101 if(treeChangeService == null) {
102 throw new UnsupportedOperationException("Underlying data broker does not expose DOMDataTreeChangeService.");
104 return treeChangeService.registerDataTreeChangeListener(treeId, listener);