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.sal.binding.test.util;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.io.InputStream;
13 import java.util.ArrayList;
14 import java.util.List;
16 import java.util.concurrent.Future;
18 import javassist.ClassPool;
20 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBackwardsCompatibleDataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
24 import org.opendaylight.controller.md.sal.dom.broker.impl.compat.BackwardsCompatibleDataBroker;
25 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
28 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
29 import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl;
30 import org.opendaylight.controller.sal.binding.impl.NotificationBrokerImpl;
31 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl;
32 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingDomConnectorDeployer;
33 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
34 import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedBindingBrokerImpl;
35 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
36 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
37 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
38 import org.opendaylight.controller.sal.core.api.BrokerService;
39 import org.opendaylight.controller.sal.core.api.RpcImplementation;
40 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
41 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
42 import org.opendaylight.controller.sal.core.api.data.DataStore;
43 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
45 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
46 import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl;
47 import org.opendaylight.controller.sal.dom.broker.impl.DataStoreStatsWrapper;
48 import org.opendaylight.controller.sal.dom.broker.impl.HashMapDataStore;
49 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareDataStoreAdapter;
50 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
56 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
57 import org.opendaylight.yangtools.yang.model.api.Module;
58 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
59 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
60 import org.reflections.Reflections;
61 import org.reflections.scanners.ResourcesScanner;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.common.base.Predicate;
66 import com.google.common.collect.ClassToInstanceMap;
67 import com.google.common.collect.ImmutableClassToInstanceMap;
68 import com.google.common.collect.ImmutableMap;
69 import com.google.common.util.concurrent.ListeningExecutorService;
71 public class BindingTestContext implements AutoCloseable {
73 public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
74 .builder().toInstance();
76 private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
78 private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
80 private DomForwardedBindingBrokerImpl baBrokerImpl;
81 private DataBrokerImpl baDataImpl;
82 private NotificationBrokerImpl baNotifyImpl;
83 private BindingIndependentConnector baConnectImpl;
85 private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
86 private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataLegacyBroker;
87 private BrokerImpl biBrokerImpl;
88 private HashMapDataStore rawDataStore;
89 private SchemaAwareDataStoreAdapter schemaAwareDataStore;
90 private DataStoreStatsWrapper dataStoreStats;
91 private DataStore dataStore;
93 private final boolean dataStoreStatisticsEnabled = false;
95 private final ListeningExecutorService executor;
96 private final ClassPool classPool;
98 private final boolean startWithSchema;
100 private MountPointManagerImpl biMountImpl;
104 private ImmutableMap<LogicalDatastoreType, DOMStore> newDatastores;
106 private BackwardsCompatibleDataBroker biCompatibleBroker;
108 private DataProviderService baData;
110 private DOMDataBroker newDOMDataBroker;
112 private final MockSchemaService mockSchemaService = new MockSchemaService();
116 public DOMDataBroker getDomAsyncDataBroker() {
117 return newDOMDataBroker;
120 protected BindingTestContext(final ListeningExecutorService executor, final ClassPool classPool, final boolean startWithSchema) {
121 this.executor = executor;
122 this.classPool = classPool;
123 this.startWithSchema = startWithSchema;
127 public void startDomDataStore() {
128 checkState(dataStore == null, "DataStore already started.");
129 checkState(biDataImpl != null, "Dom Data Broker not present");
130 rawDataStore = new HashMapDataStore();
131 schemaAwareDataStore = new SchemaAwareDataStoreAdapter();
132 schemaAwareDataStore.changeDelegate(rawDataStore);
133 if (dataStoreStatisticsEnabled) {
134 dataStoreStats = new DataStoreStatsWrapper(schemaAwareDataStore);
135 dataStore = dataStoreStats;
137 dataStore = schemaAwareDataStore;
139 mockSchemaService.registerSchemaServiceListener(schemaAwareDataStore);
140 biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore);
141 biDataImpl.registerOperationalReader(TREE_ROOT, dataStore);
142 biDataImpl.registerCommitHandler(TREE_ROOT, dataStore);
145 public void startDomDataBroker() {
146 checkState(executor != null, "Executor needs to be set");
147 biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
148 biDataImpl.setExecutor(executor);
149 biDataLegacyBroker = biDataImpl;
152 public void startNewDomDataBroker() {
153 checkState(executor != null, "Executor needs to be set");
154 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", executor);
155 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", executor);
156 newDatastores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
157 .put(LogicalDatastoreType.OPERATIONAL, operStore)
158 .put(LogicalDatastoreType.CONFIGURATION, configStore)
161 newDOMDataBroker = new DOMDataBrokerImpl(newDatastores, executor);
163 biCompatibleBroker = new BackwardsCompatibleDataBroker(newDOMDataBroker);
165 mockSchemaService.registerSchemaServiceListener(configStore);
166 mockSchemaService.registerSchemaServiceListener(operStore);
167 mockSchemaService.registerSchemaServiceListener(biCompatibleBroker);
168 biDataLegacyBroker = biCompatibleBroker;
171 public void startBindingDataBroker() {
172 checkState(executor != null, "Executor needs to be set");
173 baDataImpl = new DataBrokerImpl();
174 baDataImpl.setExecutor(executor);
178 public void startBindingBroker() {
179 checkState(executor != null, "Executor needs to be set");
180 checkState(baData != null, "Binding Data Broker must be started");
181 checkState(baNotifyImpl != null, "Notification Service must be started");
182 baBrokerImpl = new DomForwardedBindingBrokerImpl("test");
184 baBrokerImpl.getMountManager().setDataCommitExecutor(executor);
185 baBrokerImpl.getMountManager().setNotificationExecutor(executor);
186 baBrokerImpl.setRpcBroker(new RpcProviderRegistryImpl("test"));
187 baBrokerImpl.setLegacyDataBroker(baData);
188 baBrokerImpl.setNotificationBroker(baNotifyImpl);
189 baBrokerImpl.start();
192 public void startForwarding() {
193 checkState(baData != null, "Binding Data Broker needs to be started");
194 checkState(biDataLegacyBroker != null, "DOM Data Broker needs to be started.");
195 checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
197 baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
198 baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
199 baBrokerImpl.setConnector(baConnectImpl);
200 baBrokerImpl.setDomProviderContext(createMockContext());
201 baBrokerImpl.startForwarding();
204 private ProviderSession createMockContext() {
206 final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
207 .<BrokerService> builder()
209 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataLegacyBroker) //
210 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
211 .put(MountProvisionService.class, biMountImpl) //
214 return new ProviderSession() {
217 public Future<RpcResult<CompositeNode>> rpc(final QName rpc, final CompositeNode input) {
218 throw new UnsupportedOperationException();
222 public <T extends BrokerService> T getService(final Class<T> service) {
223 return domBrokerServices.getInstance(service);
227 public boolean isClosed() {
232 public Set<QName> getSupportedRpcs() {
237 public void close() {
241 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
242 final RpcRegistrationListener listener) {
247 public RpcRegistration addRpcImplementation(final QName rpcType, final RpcImplementation implementation)
248 throws IllegalArgumentException {
253 public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
258 public RoutedRpcRegistration addMountedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
264 public void startBindingToDomMappingService() {
265 checkState(classPool != null, "ClassPool needs to be present");
266 mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl(classPool);
267 mockSchemaService.registerSchemaServiceListener(mappingServiceImpl);
270 public void updateYangSchema(final String[] files) {
271 mockSchemaService.changeSchema(getContext(files));
274 public static String[] getAllYangFilesOnClasspath() {
275 Predicate<String> predicate = new Predicate<String>() {
277 public boolean apply(final String input) {
278 return input.endsWith(".yang");
281 Reflections reflection = new Reflections("META-INF.yang", new ResourcesScanner());
282 Set<String> result = reflection.getResources(predicate);
283 return result.toArray(new String[result.size()]);
286 private static SchemaContext getContext(final String[] yangFiles) {
287 ClassLoader loader = BindingTestContext.class.getClassLoader();
288 List<InputStream> streams = new ArrayList<>();
289 for (String string : yangFiles) {
290 InputStream stream = loader.getResourceAsStream(string);
293 YangParserImpl parser = new YangParserImpl();
294 Set<Module> modules = parser.parseYangModelsFromStreams(streams);
295 return parser.resolveSchemaContext(modules);
298 public void startLegacy() {
299 startBindingDataBroker();
300 startBindingNotificationBroker();
301 startBindingBroker();
302 startDomDataBroker();
305 startDomMountPoint();
306 startBindingToDomMappingService();
308 if (startWithSchema) {
309 loadYangSchemaFromClasspath();
313 public void start() {
314 startNewDomDataBroker();
316 startDomMountPoint();
317 startBindingToDomMappingService();
318 startNewBindingDataBroker();
320 startBindingNotificationBroker();
321 startBindingBroker();
324 if (startWithSchema) {
325 loadYangSchemaFromClasspath();
329 public void startNewBindingDataBroker() {
330 ForwardedBackwardsCompatibleDataBroker forwarded = new ForwardedBackwardsCompatibleDataBroker(newDOMDataBroker, mappingServiceImpl,mockSchemaService, executor);
334 private void startDomMountPoint() {
335 biMountImpl = new MountPointManagerImpl();
336 biMountImpl.setDataBroker(getDomDataBroker());
339 private void startDomBroker() {
340 checkState(executor != null);
341 biBrokerImpl = new BrokerImpl();
342 biBrokerImpl.setRouter(new SchemaAwareRpcBroker("/", mockSchemaService));
346 public void startBindingNotificationBroker() {
347 checkState(executor != null);
348 baNotifyImpl = new NotificationBrokerImpl(executor);
352 public void loadYangSchemaFromClasspath() {
353 String[] files = getAllYangFilesOnClasspath();
354 updateYangSchema(files);
357 public DataProviderService getBindingDataBroker() {
361 public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
362 return biDataLegacyBroker;
365 public DataStore getDomDataStore() {
369 public BindingIndependentMappingService getBindingToDomMappingService() {
370 return mappingServiceImpl;
373 public void logDataStoreStatistics() {
374 if (dataStoreStats == null) {
378 LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
379 dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
380 dataStoreStats.getConfigurationReadAverageTime());
382 LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
383 dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(),
384 dataStoreStats.getOperationalReadAverageTime());
386 LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
387 dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
388 dataStoreStats.getRequestCommitAverageTime());
391 public RpcProviderRegistry getBindingRpcRegistry() {
392 return baBrokerImpl.getRoot();
395 public RpcProvisionRegistry getDomRpcRegistry() {
396 if (biBrokerImpl == null) {
399 return biBrokerImpl.getRouter();
402 public RpcImplementation getDomRpcInvoker() {
403 return biBrokerImpl.getRouter();
407 public void close() throws Exception {
411 public MountProviderService getBindingMountProviderService() {
412 return baBrokerImpl.getMountManager();
415 public MountProvisionService getDomMountProviderService() {