X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Ftest%2Futil%2FBindingTestContext.java;h=d6d87bac84b307d388147cc3bd44c0346b5afa77;hb=5ffd4b46ca00fc8f3d801050670c890117dc0811;hp=c03d851f5c9ef740a14a9097feceaf8909833184;hpb=15105ea803f97e89ae274148abd3208776319d2c;p=controller.git diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/util/BindingTestContext.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/util/BindingTestContext.java index c03d851f5c..deb4a8aeca 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/util/BindingTestContext.java +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/util/BindingTestContext.java @@ -1,27 +1,36 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.test.util; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; +import static com.google.common.base.Preconditions.checkState; + import java.util.Set; import java.util.concurrent.Future; import javassist.ClassPool; -import org.eclipse.xtext.xbase.lib.Pure; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.impl.ForwardedBackwardsCompatibleDataBroker; +import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; +import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl; +import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker; +import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; import org.opendaylight.controller.sal.binding.api.data.DataProviderService; import org.opendaylight.controller.sal.binding.api.mount.MountProviderService; -import org.opendaylight.controller.sal.binding.dom.serializer.impl.RuntimeGeneratedMappingServiceImpl; import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl; import org.opendaylight.controller.sal.binding.impl.NotificationBrokerImpl; -import org.opendaylight.controller.sal.binding.impl.RootBindingAwareBroker; import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl; import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingDomConnectorDeployer; import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector; -import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentMappingService; import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedBindingBrokerImpl; -import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest; import org.opendaylight.controller.sal.core.api.Broker.ProviderSession; import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration; import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration; @@ -29,38 +38,36 @@ import org.opendaylight.controller.sal.core.api.BrokerService; import org.opendaylight.controller.sal.core.api.RpcImplementation; import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry; import org.opendaylight.controller.sal.core.api.RpcRegistrationListener; -import org.opendaylight.controller.sal.core.api.data.DataStore; import org.opendaylight.controller.sal.core.api.mount.MountProvisionService; -import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService; +import org.opendaylight.controller.sal.core.spi.data.DOMStore; import org.opendaylight.controller.sal.dom.broker.BrokerImpl; import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl; -import org.opendaylight.controller.sal.dom.broker.impl.DataStoreStatsWrapper; -import org.opendaylight.controller.sal.dom.broker.impl.HashMapDataStore; -import org.opendaylight.controller.sal.dom.broker.impl.RpcRouterImpl; -import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareDataStoreAdapter; +import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker; import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext; +import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl; +import org.opendaylight.yangtools.yang.binding.YangModuleInfo; +import org.opendaylight.yangtools.yang.binding.util.BindingReflections; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; -import org.reflections.Reflections; -import org.reflections.scanners.ResourcesScanner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Predicate; +import com.google.common.annotations.Beta; import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.ImmutableClassToInstanceMap; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.MutableClassToInstanceMap; import com.google.common.util.concurrent.ListeningExecutorService; -import com.google.common.util.concurrent.MoreExecutors; - -import static com.google.common.base.Preconditions.*; +@Beta public class BindingTestContext implements AutoCloseable { - public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier + public static final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier .builder().toInstance(); private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class); @@ -73,13 +80,9 @@ public class BindingTestContext implements AutoCloseable { private BindingIndependentConnector baConnectImpl; private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl; + @SuppressWarnings("deprecation") + private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataLegacyBroker; private BrokerImpl biBrokerImpl; - private HashMapDataStore rawDataStore; - private SchemaAwareDataStoreAdapter schemaAwareDataStore; - private DataStoreStatsWrapper dataStoreStats; - private DataStore dataStore; - - private boolean dataStoreStatisticsEnabled = false; private final ListeningExecutorService executor; private final ClassPool classPool; @@ -88,59 +91,88 @@ public class BindingTestContext implements AutoCloseable { private MountPointManagerImpl biMountImpl; - protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) { + + + private ImmutableMap newDatastores; + + private BackwardsCompatibleDataBroker biCompatibleBroker; + + @SuppressWarnings("deprecation") + private DataProviderService baData; + + private DOMDataBroker newDOMDataBroker; + + private final MockSchemaService mockSchemaService = new MockSchemaService(); + + private DataBroker dataBroker; + + + + public DOMDataBroker getDomAsyncDataBroker() { + return newDOMDataBroker; + } + + protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) { this.executor = executor; this.classPool = classPool; this.startWithSchema = startWithSchema; } - public void startDomDataStore() { - checkState(dataStore == null, "DataStore already started."); - checkState(biDataImpl != null, "Dom Data Broker not present"); - rawDataStore = new HashMapDataStore(); - schemaAwareDataStore = new SchemaAwareDataStoreAdapter(); - schemaAwareDataStore.changeDelegate(rawDataStore); - if (dataStoreStatisticsEnabled) { - dataStoreStats = new DataStoreStatsWrapper(schemaAwareDataStore); - dataStore = dataStoreStats; - } else { - dataStore = schemaAwareDataStore; - } - - biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore); - biDataImpl.registerOperationalReader(TREE_ROOT, dataStore); - biDataImpl.registerCommitHandler(TREE_ROOT, dataStore); - } - public void startDomDataBroker() { checkState(executor != null, "Executor needs to be set"); biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl(); biDataImpl.setExecutor(executor); + biDataLegacyBroker = biDataImpl; + } + + public void startNewDataBroker() { + checkState(executor != null, "Executor needs to be set"); + checkState(newDOMDataBroker != null, "DOM Data Broker must be set"); + dataBroker = new ForwardedBindingDataBroker(newDOMDataBroker, mappingServiceImpl, mockSchemaService); + } + + public void startNewDomDataBroker() { + checkState(executor != null, "Executor needs to be set"); + InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", executor); + InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", executor); + newDatastores = ImmutableMap.builder() + .put(LogicalDatastoreType.OPERATIONAL, operStore) + .put(LogicalDatastoreType.CONFIGURATION, configStore) + .build(); + + newDOMDataBroker = new DOMDataBrokerImpl(newDatastores, executor); + + biCompatibleBroker = new BackwardsCompatibleDataBroker(newDOMDataBroker,mockSchemaService); + + mockSchemaService.registerSchemaContextListener(configStore); + mockSchemaService.registerSchemaContextListener(operStore); + biDataLegacyBroker = biCompatibleBroker; } public void startBindingDataBroker() { checkState(executor != null, "Executor needs to be set"); baDataImpl = new DataBrokerImpl(); baDataImpl.setExecutor(executor); + baData = baDataImpl; } public void startBindingBroker() { checkState(executor != null, "Executor needs to be set"); - checkState(baDataImpl != null, "Binding Data Broker must be started"); + checkState(baData != null, "Binding Data Broker must be started"); checkState(baNotifyImpl != null, "Notification Service must be started"); baBrokerImpl = new DomForwardedBindingBrokerImpl("test"); baBrokerImpl.getMountManager().setDataCommitExecutor(executor); baBrokerImpl.getMountManager().setNotificationExecutor(executor); baBrokerImpl.setRpcBroker(new RpcProviderRegistryImpl("test")); - baBrokerImpl.setDataBroker(baDataImpl); + baBrokerImpl.setLegacyDataBroker(baData); baBrokerImpl.setNotificationBroker(baNotifyImpl); baBrokerImpl.start(); } public void startForwarding() { - checkState(baDataImpl != null, "Binding Data Broker needs to be started"); - checkState(biDataImpl != null, "DOM Data Broker needs to be started."); + checkState(baData != null, "Binding Data Broker needs to be started"); + checkState(biDataLegacyBroker != null, "DOM Data Broker needs to be started."); checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started."); baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService()); @@ -151,11 +183,12 @@ public class BindingTestContext implements AutoCloseable { } private ProviderSession createMockContext() { - // TODO Auto-generated method stub + + @SuppressWarnings("deprecation") final ClassToInstanceMap domBrokerServices = ImmutableClassToInstanceMap . builder() // - .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) // + .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataLegacyBroker) // .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) // .put(MountProvisionService.class, biMountImpl) // .build(); @@ -163,12 +196,12 @@ public class BindingTestContext implements AutoCloseable { return new ProviderSession() { @Override - public Future> rpc(QName rpc, CompositeNode input) { + public Future> rpc(final QName rpc, final CompositeNode input) { throw new UnsupportedOperationException(); } @Override - public T getService(Class service) { + public T getService(final Class service) { return domBrokerServices.getInstance(service); } @@ -188,23 +221,23 @@ public class BindingTestContext implements AutoCloseable { @Override public ListenerRegistration addRpcRegistrationListener( - RpcRegistrationListener listener) { + final RpcRegistrationListener listener) { return null; } @Override - public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation) + public RpcRegistration addRpcImplementation(final QName rpcType, final RpcImplementation implementation) throws IllegalArgumentException { return null; } @Override - public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) { + public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) { return null; } @Override - public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) { + public RoutedRpcRegistration addMountedRpcImplementation(final QName rpcType, final RpcImplementation implementation) { return null; } }; @@ -212,60 +245,42 @@ public class BindingTestContext implements AutoCloseable { public void startBindingToDomMappingService() { checkState(classPool != null, "ClassPool needs to be present"); - mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl(); - mappingServiceImpl.setPool(classPool); - mappingServiceImpl.start(null); + mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl(classPool); + mockSchemaService.registerSchemaContextListener(mappingServiceImpl); } - public void updateYangSchema(String[] files) { - SchemaContext context = getContext(files); - if (schemaAwareDataStore != null) { - schemaAwareDataStore.onGlobalContextUpdated(context); - } - if (mappingServiceImpl != null) { - mappingServiceImpl.onGlobalContextUpdated(context); - } - } - - public static String[] getAllYangFilesOnClasspath() { - Predicate predicate = new Predicate() { - @Override - public boolean apply(String input) { - return input.endsWith(".yang"); - } - }; - Reflections reflection = new Reflections("META-INF.yang", new ResourcesScanner()); - Set result = reflection.getResources(predicate); - return (String[]) result.toArray(new String[result.size()]); + private void updateYangSchema(final ImmutableSet moduleInfos) { + mockSchemaService.changeSchema(getContext(moduleInfos)); } - private static SchemaContext getContext(String[] yangFiles) { - ClassLoader loader = BindingTestContext.class.getClassLoader(); - List streams = new ArrayList<>(); - for (String string : yangFiles) { - InputStream stream = loader.getResourceAsStream(string); - streams.add(stream); - } - YangParserImpl parser = new YangParserImpl(); - Set modules = parser.parseYangModelsFromStreams(streams); - return parser.resolveSchemaContext(modules); + private SchemaContext getContext(final ImmutableSet moduleInfos) { + ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create(); + ctx.addModuleInfos(moduleInfos); + return ctx.tryToCreateSchemaContext().get(); } public void start() { - startBindingDataBroker(); - startBindingNotificationBroker(); - startBindingBroker(); - startDomDataBroker(); - startDomDataStore(); + startNewDomDataBroker(); + startDomBroker(); startDomMountPoint(); startBindingToDomMappingService(); + startNewDataBroker(); + startNewBindingDataBroker(); + startBindingNotificationBroker(); + startBindingBroker(); + startForwarding(); if (startWithSchema) { loadYangSchemaFromClasspath(); } } + public void startNewBindingDataBroker() { + ForwardedBackwardsCompatibleDataBroker forwarded = new ForwardedBackwardsCompatibleDataBroker(newDOMDataBroker, mappingServiceImpl,mockSchemaService, executor); + baData = forwarded; + } + private void startDomMountPoint() { biMountImpl = new MountPointManagerImpl(); biMountImpl.setDataBroker(getDomDataBroker()); @@ -273,9 +288,11 @@ public class BindingTestContext implements AutoCloseable { private void startDomBroker() { checkState(executor != null); - biBrokerImpl = new BrokerImpl(); - biBrokerImpl.setExecutor(executor); - biBrokerImpl.setRouter(new RpcRouterImpl("test")); + + SchemaAwareRpcBroker router = new SchemaAwareRpcBroker("/", mockSchemaService); + ClassToInstanceMap services = MutableClassToInstanceMap.create(); + biBrokerImpl = new BrokerImpl(router,services); + } public void startBindingNotificationBroker() { @@ -285,44 +302,24 @@ public class BindingTestContext implements AutoCloseable { } public void loadYangSchemaFromClasspath() { - String[] files = getAllYangFilesOnClasspath(); - updateYangSchema(files); + ImmutableSet moduleInfos = BindingReflections.loadModuleInfos(); + updateYangSchema(moduleInfos); } + @SuppressWarnings("deprecation") public DataProviderService getBindingDataBroker() { - return baDataImpl; + return baData; } + @SuppressWarnings("deprecation") public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() { - return biDataImpl; - } - - public DataStore getDomDataStore() { - return dataStore; + return biDataLegacyBroker; } public BindingIndependentMappingService getBindingToDomMappingService() { return mappingServiceImpl; } - public void logDataStoreStatistics() { - if (dataStoreStats == null) { - return; - } - - LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms", - dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(), - dataStoreStats.getConfigurationReadAverageTime()); - - LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms", - dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(), - dataStoreStats.getOperationalReadAverageTime()); - - LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms", - dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(), - dataStoreStats.getRequestCommitAverageTime()); - } - public RpcProviderRegistry getBindingRpcRegistry() { return baBrokerImpl.getRoot(); } @@ -350,4 +347,10 @@ public class BindingTestContext implements AutoCloseable { public MountProvisionService getDomMountProviderService() { return biMountImpl; } + + public DataBroker getDataBroker() { + return dataBroker; + } + + }