BUG-694: Disable DataReader registration for BA Broker 07/6007/3
authorRobert Varga <rovarga@cisco.com>
Wed, 9 Apr 2014 16:23:19 +0000 (18:23 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 10 Apr 2014 09:39:45 +0000 (11:39 +0200)
The DataReader's semantic contract with its users, data merge,
when working with Binding-Aware DTOs.

The existing DataReaders implementations need to be rewritten as
datastores, which deal with the same problem in Binding Independent
format, such as the BI Data Broker.

Change-Id: I91b4516bb04a570acb6bf2b381860e9ae3fbbd43
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/DataBrokerImpl.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/MountPointManagerImpl.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java

index 48ccbfb..9ede01b 100644 (file)
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.controller.sal.binding.impl;\r
-\r
+package org.opendaylight.controller.sal.binding.impl;
+
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
+import org.opendaylight.controller.md.sal.common.api.data.DataReader;
+import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter;
 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataBroker;
 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
-import org.opendaylight.controller.sal.binding.impl.util.BindingAwareDataReaderRouter;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.util.DataObjectReadingUtil;
 
 import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.Maps;
-\r
-\r
-public class DataBrokerImpl extends AbstractDataBroker<InstanceIdentifier<? extends DataObject>, DataObject, DataChangeListener> //\r
-       implements DataProviderService, AutoCloseable {\r
-\r
+
+public class DataBrokerImpl extends
+        AbstractDataBroker<InstanceIdentifier<? extends DataObject>, DataObject, DataChangeListener> //
+        implements DataProviderService, AutoCloseable {
+
     private final static class ContainsWildcarded implements Predicate<InstanceIdentifier<? extends DataObject>> {
 
-        private final  InstanceIdentifier<? extends DataObject> key;
+        private final InstanceIdentifier<? extends DataObject> key;
 
-        public ContainsWildcarded(InstanceIdentifier<? extends DataObject> key) {
+        public ContainsWildcarded(final InstanceIdentifier<? extends DataObject> key) {
             this.key = key;
         }
 
         @Override
-        public boolean apply(InstanceIdentifier<? extends DataObject> input) {
+        public boolean apply(final InstanceIdentifier<? extends DataObject> input) {
             return key.containsWildcarded(input);
         }
     }
 
     private final static class IsContainedWildcarded implements Predicate<InstanceIdentifier<? extends DataObject>> {
 
-        private final  InstanceIdentifier<? extends DataObject> key;
+        private final InstanceIdentifier<? extends DataObject> key;
 
-        public IsContainedWildcarded(InstanceIdentifier<? extends DataObject> key) {
+        public IsContainedWildcarded(final InstanceIdentifier<? extends DataObject> key) {
             this.key = key;
         }
 
         @Override
-        public boolean apply(InstanceIdentifier<? extends DataObject> input) {
+        public boolean apply(final InstanceIdentifier<? extends DataObject> input) {
             return input.containsWildcarded(key);
         }
     }
 
-    private final AtomicLong nextTransaction = new AtomicLong();\r
-    private final AtomicLong createdTransactionsCount = new AtomicLong();\r
-\r
-    public AtomicLong getCreatedTransactionsCount() {\r
-        return createdTransactionsCount;\r
-    }\r
-\r
-    public DataBrokerImpl() {\r
-        setDataReadRouter(new BindingAwareDataReaderRouter());\r
-    }\r
-\r
-    @Override\r
-    public DataTransactionImpl beginTransaction() {\r
-        String transactionId = "BA-" + nextTransaction.getAndIncrement();\r
-        createdTransactionsCount.getAndIncrement();\r
-        return new DataTransactionImpl(transactionId,this);\r
-    }\r
-\r
-    @Override\r
-    public void close() {\r
-\r
+    private final AtomicLong nextTransaction = new AtomicLong();
+    private final AtomicLong createdTransactionsCount = new AtomicLong();
+    private final DelegatingDataReadRouter router = new DelegatingDataReadRouter();
+    private DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject> rootCommitHandler;
+
+    public DataBrokerImpl() {
+        setDataReadRouter(router);
+    }
+
+    public void setDataReadDelegate(final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> delegate) {
+        router.setDelegate(delegate);
+    }
+
+    public AtomicLong getCreatedTransactionsCount() {
+        return createdTransactionsCount;
+    }
+
+    @Override
+    public DataTransactionImpl beginTransaction() {
+        String transactionId = "BA-" + nextTransaction.getAndIncrement();
+        createdTransactionsCount.getAndIncrement();
+        return new DataTransactionImpl(transactionId, this);
     }
 
     @Override
-    protected Predicate<InstanceIdentifier<? extends DataObject>> createContainsPredicate(final
-            InstanceIdentifier<? extends DataObject> key) {
+    public void close() {
+
+    }
+
+    @Override
+    protected Predicate<InstanceIdentifier<? extends DataObject>> createContainsPredicate(
+            final InstanceIdentifier<? extends DataObject> key) {
         return new ContainsWildcarded(key);
     }
 
     @Override
-    protected Predicate<InstanceIdentifier<? extends DataObject>> createIsContainedPredicate(final
-            InstanceIdentifier<? extends DataObject> key) {
+    protected Predicate<InstanceIdentifier<? extends DataObject>> createIsContainedPredicate(
+            final InstanceIdentifier<? extends DataObject> key) {
         return new IsContainedWildcarded(key);
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Override
     protected Map<InstanceIdentifier<? extends DataObject>, DataObject> deepGetBySubpath(
-            Map<InstanceIdentifier<? extends DataObject>, DataObject> dataSet,
-            InstanceIdentifier<? extends DataObject> path) {
+            final Map<InstanceIdentifier<? extends DataObject>, DataObject> dataSet,
+            final InstanceIdentifier<? extends DataObject> path) {
         Builder<InstanceIdentifier<? extends DataObject>, DataObject> builder = ImmutableMap.builder();
-        Map<InstanceIdentifier<? extends DataObject>, DataObject> potential = Maps.filterKeys(dataSet, createIsContainedPredicate(path));
-        for(Entry<InstanceIdentifier<? extends DataObject>, DataObject> entry : potential.entrySet()) {
+        Map<InstanceIdentifier<? extends DataObject>, DataObject> potential = Maps.filterKeys(dataSet,
+                createIsContainedPredicate(path));
+        for (Entry<InstanceIdentifier<? extends DataObject>, DataObject> entry : potential.entrySet()) {
             try {
-                builder.putAll(DataObjectReadingUtil.readData(entry.getValue(),(InstanceIdentifier)entry.getKey(),path));
+                builder.putAll(DataObjectReadingUtil.readData(entry.getValue(), (InstanceIdentifier) entry.getKey(),
+                        path));
             } catch (Exception e) {
                 // FIXME : Log exception;
             }
@@ -108,5 +121,58 @@ public class DataBrokerImpl extends AbstractDataBroker<InstanceIdentifier<? exte
         return builder.build();
 
     }
-\r
+
+    public class DelegatingDataReadRouter extends
+            AbstractDataReadRouter<InstanceIdentifier<? extends DataObject>, DataObject> {
+
+        private DataReader<InstanceIdentifier<? extends DataObject>, DataObject> delegate;
+
+        @Override
+        public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
+            return delegate.readConfigurationData(path);
+        }
+
+        public void setDelegate(final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> delegate) {
+            this.delegate = delegate;
+        }
+
+        @Override
+        public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
+            return delegate.readOperationalData(path);
+        }
+
+        @Override
+        protected DataObject merge(final InstanceIdentifier<? extends DataObject> path, final Iterable<DataObject> data) {
+            throw new UnsupportedOperationException("Not supported");
+        }
+
+        @Override
+        public Registration<DataReader<InstanceIdentifier<? extends DataObject>, DataObject>> registerConfigurationReader(
+                final InstanceIdentifier<? extends DataObject> path,
+                final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> reader) {
+            throw new UnsupportedOperationException("Not supported");
+        }
+
+        @Override
+        public Registration<DataReader<InstanceIdentifier<? extends DataObject>, DataObject>> registerOperationalReader(
+                final InstanceIdentifier<? extends DataObject> path,
+                final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> reader) {
+            throw new UnsupportedOperationException("Not supported");
+        }
+    }
+
+    @Override
+    protected ImmutableList<DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject>> affectedCommitHandlers(
+            final Set<InstanceIdentifier<? extends DataObject>> paths) {
+        ImmutableList.Builder<DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject>> handlersBuilder = ImmutableList.builder();
+        return handlersBuilder //
+                .add(rootCommitHandler) //
+                .addAll(super.affectedCommitHandlers(paths)) //
+                .build();
+    }
+
+    public void setRootCommitHandler(final DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject> commitHandler) {
+        rootCommitHandler = commitHandler;
+    }
+
 }
index 29ba57f..b999a6f 100644 (file)
@@ -118,6 +118,11 @@ public class MountPointManagerImpl implements MountProviderService {
             super(rpcRegistry, notificationBroker, dataBroker);
             this.identifier = identifier;
         }
+
+        // Needed only for BI Connector
+        public DataBrokerImpl getDataBrokerImpl() {
+            return (DataBrokerImpl) getDataBroker();
+        }
         
         @Override
         public InstanceIdentifier<?> getIdentifier() {
index 37c0dfa..9edea0c 100644 (file)
@@ -35,7 +35,6 @@ import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandlerRegistration;
 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
-import org.opendaylight.controller.md.sal.common.api.data.DataReader;
 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
@@ -46,6 +45,8 @@ import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.opendaylight.controller.sal.binding.api.data.RuntimeDataProvider;
 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
+import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl;
+import org.opendaylight.controller.sal.binding.impl.MountPointManagerImpl.BindingMountPointImpl;
 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl;
 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl.GlobalRpcRegistrationListener;
 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl.RouterInstantiationListener;
@@ -100,8 +101,6 @@ public class BindingIndependentConnector implements //
 
     private final Logger LOG = LoggerFactory.getLogger(BindingIndependentConnector.class);
 
-    @SuppressWarnings("deprecation")
-    private static final InstanceIdentifier<? extends DataObject> ROOT = InstanceIdentifier.builder().toInstance();
 
     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier ROOT_BI = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
             .builder().toInstance();
@@ -140,8 +139,6 @@ public class BindingIndependentConnector implements //
 
     };
 
-    private Registration<DataReader<InstanceIdentifier<? extends DataObject>, DataObject>> baDataReaderRegistration;
-
     private boolean rpcForwarding = false;
 
     private boolean dataForwarding = false;
@@ -306,11 +303,23 @@ public class BindingIndependentConnector implements //
             dataForwarding = true;
             return;
         }
-        checkState(!dataForwarding, "Connector is already forwarding data.");
-        baDataReaderRegistration = baDataService.registerDataReader(ROOT, this);
-        baCommitHandlerRegistration = baDataService.registerCommitHandler(ROOT, bindingToDomCommitHandler);
-        biCommitHandlerRegistration = biDataService.registerCommitHandler(ROOT_BI, domToBindingCommitHandler);
-        baDataService.registerCommitHandlerListener(domToBindingCommitHandler);
+
+        final DataProviderService baData;
+        if (baDataService instanceof BindingMountPointImpl) {
+            baData = ((BindingMountPointImpl)baDataService).getDataBrokerImpl();
+            LOG.debug("Extracted BA Data provider {} from mount point {}", baData, baDataService);
+        } else {
+            baData = baDataService;
+        }
+
+        if (baData instanceof DataBrokerImpl) {
+            checkState(!dataForwarding, "Connector is already forwarding data.");
+            ((DataBrokerImpl) baData).setDataReadDelegate(this);
+            ((DataBrokerImpl) baData).setRootCommitHandler(bindingToDomCommitHandler);
+            biCommitHandlerRegistration = biDataService.registerCommitHandler(ROOT_BI, domToBindingCommitHandler);
+            baDataService.registerCommitHandlerListener(domToBindingCommitHandler);
+        }
+
         dataForwarding = true;
     }
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.