import java.util.ArrayList;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.Future;
import javassist.ClassPool;
+import org.eclipse.xtext.xbase.lib.Pure;
+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.connect.dom.BindingIndependentDataServiceConnector;
+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;
+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.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.yangtools.concepts.ListenerRegistration;
+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.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.slf4j.LoggerFactory;
import com.google.common.base.Predicate;
+import com.google.common.collect.ClassToInstanceMap;
+import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import static com.google.common.base.Preconditions.*;
-public class BindingTestContext {
-
-
+public class BindingTestContext implements AutoCloseable {
+
public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
.builder().toInstance();
private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
-
+
private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
+
+ private DomForwardedBindingBrokerImpl baBrokerImpl;
private DataBrokerImpl baDataImpl;
+ private NotificationBrokerImpl baNotifyImpl;
+ private BindingIndependentConnector baConnectImpl;
+
private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
-
- private BindingIndependentDataServiceConnector connectorServiceImpl;
+ 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;
private final boolean startWithSchema;
-
+
+ private MountPointManagerImpl biMountImpl;
+
protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
this.executor = executor;
this.classPool = classPool;
rawDataStore = new HashMapDataStore();
schemaAwareDataStore = new SchemaAwareDataStoreAdapter();
schemaAwareDataStore.changeDelegate(rawDataStore);
- if(dataStoreStatisticsEnabled) {
- dataStoreStats = new DataStoreStatsWrapper(schemaAwareDataStore);
- dataStore = dataStoreStats;
+ 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");
+ checkState(executor != null, "Executor needs to be set");
biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
biDataImpl.setExecutor(executor);
}
-
+
public void startBindingDataBroker() {
- checkState(executor != null,"Executor needs to be set");
+ checkState(executor != null, "Executor needs to be set");
baDataImpl = new DataBrokerImpl();
baDataImpl.setExecutor(executor);
}
-
- public void startBindingToDomDataConnector() {
- checkState(baDataImpl != null,"Binding Data Broker needs to be started");
- checkState(biDataImpl != null,"DOM Data Broker needs to be started.");
- checkState(mappingServiceImpl != null,"DOM Mapping Service needs to be started.");
- connectorServiceImpl = new BindingIndependentDataServiceConnector();
- connectorServiceImpl.setBaDataService(baDataImpl);
- connectorServiceImpl.setBiDataService(biDataImpl);
- connectorServiceImpl.setMappingService(mappingServiceImpl);
- connectorServiceImpl.start();
- }
-
+
+ public void startBindingBroker() {
+ checkState(executor != null, "Executor needs to be set");
+ checkState(baDataImpl != 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.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(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
+
+ baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
+ baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
+ baBrokerImpl.setConnector(baConnectImpl);
+ baBrokerImpl.setDomProviderContext(createMockContext());
+ baBrokerImpl.startForwarding();
+ }
+
+ private ProviderSession createMockContext() {
+ // TODO Auto-generated method stub
+ final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
+ .<BrokerService> builder()
+ //
+ .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) //
+ .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
+ .put(MountProvisionService.class, biMountImpl) //
+ .build();
+
+ return new ProviderSession() {
+
+ @Override
+ public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public <T extends BrokerService> T getService(Class<T> service) {
+ return domBrokerServices.getInstance(service);
+ }
+
+ @Override
+ public boolean isClosed() {
+ return false;
+ }
+
+ @Override
+ public Set<QName> getSupportedRpcs() {
+ return null;
+ }
+
+ @Override
+ public void close() {
+ }
+
+ @Override
+ public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
+ RpcRegistrationListener listener) {
+ return null;
+ }
+
+ @Override
+ public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
+ throws IllegalArgumentException {
+ return null;
+ }
+
+ @Override
+ public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
+ return null;
+ }
+
+ @Override
+ public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
+ return null;
+ }
+ };
+ }
+
public void startBindingToDomMappingService() {
- checkState(classPool != null,"ClassPool needs to be present");
+ checkState(classPool != null, "ClassPool needs to be present");
mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl();
mappingServiceImpl.setPool(classPool);
mappingServiceImpl.start(null);
}
-
-
+
public void updateYangSchema(String[] files) {
SchemaContext context = getContext(files);
- if(schemaAwareDataStore != null) {
+ if (schemaAwareDataStore != null) {
schemaAwareDataStore.onGlobalContextUpdated(context);
}
- if(mappingServiceImpl != null) {
+ if (mappingServiceImpl != null) {
mappingServiceImpl.onGlobalContextUpdated(context);
}
}
-
-
+
public static String[] getAllYangFilesOnClasspath() {
Predicate<String> predicate = new Predicate<String>() {
@Override
Set<String> result = reflection.getResources(predicate);
return (String[]) result.toArray(new String[result.size()]);
}
-
+
private static SchemaContext getContext(String[] yangFiles) {
ClassLoader loader = BindingTestContext.class.getClassLoader();
List<InputStream> streams = new ArrayList<>();
Set<Module> modules = parser.parseYangModelsFromStreams(streams);
return parser.resolveSchemaContext(modules);
}
-
+
public void start() {
startBindingDataBroker();
+ startBindingNotificationBroker();
+ startBindingBroker();
startDomDataBroker();
startDomDataStore();
+ startDomBroker();
+ startDomMountPoint();
startBindingToDomMappingService();
- startBindingToDomDataConnector();
- if(startWithSchema) {
+ startForwarding();
+ if (startWithSchema) {
loadYangSchemaFromClasspath();
}
}
+ private void startDomMountPoint() {
+ biMountImpl = new MountPointManagerImpl();
+ biMountImpl.setDataBroker(getDomDataBroker());
+ }
+
+ private void startDomBroker() {
+ checkState(executor != null);
+ biBrokerImpl = new BrokerImpl();
+ biBrokerImpl.setExecutor(executor);
+ biBrokerImpl.setRouter(new RpcRouterImpl("test"));
+ }
+
+ public void startBindingNotificationBroker() {
+ checkState(executor != null);
+ baNotifyImpl = new NotificationBrokerImpl(executor);
+
+ }
+
public void loadYangSchemaFromClasspath() {
String[] files = getAllYangFilesOnClasspath();
updateYangSchema(files);
}
public void logDataStoreStatistics() {
- if(dataStoreStats == null) {
+ if (dataStoreStats == null) {
return;
}
-
+
LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
dataStoreStats.getConfigurationReadAverageTime());
dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
dataStoreStats.getRequestCommitAverageTime());
}
+
+ public RpcProviderRegistry getBindingRpcRegistry() {
+ return baBrokerImpl.getRoot();
+ }
+
+ public RpcProvisionRegistry getDomRpcRegistry() {
+ if (biBrokerImpl == null) {
+ return null;
+ }
+ return biBrokerImpl.getRouter();
+ }
+
+ public RpcImplementation getDomRpcInvoker() {
+ return biBrokerImpl.getRouter();
+ }
+
+ @Override
+ public void close() throws Exception {
+
+ }
+
+ public MountProviderService getBindingMountProviderService() {
+ return baBrokerImpl.getMountManager();
+ }
+
+ public MountProvisionService getDomMountProviderService() {
+ return biMountImpl;
+ }
}