1 package org.opendaylight.controller.sal.binding.test.util;
3 import java.io.InputStream;
4 import java.util.ArrayList;
7 import java.util.concurrent.Future;
9 import javassist.ClassPool;
11 import org.eclipse.xtext.xbase.lib.Pure;
12 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
13 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
14 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
15 import org.opendaylight.controller.sal.binding.dom.serializer.impl.RuntimeGeneratedMappingServiceImpl;
16 import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl;
17 import org.opendaylight.controller.sal.binding.impl.NotificationBrokerImpl;
18 import org.opendaylight.controller.sal.binding.impl.RootBindingAwareBroker;
19 import org.opendaylight.controller.sal.binding.impl.RpcProviderRegistryImpl;
20 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingDomConnectorDeployer;
21 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
22 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentMappingService;
23 import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedBindingBrokerImpl;
24 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
25 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
26 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
27 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
28 import org.opendaylight.controller.sal.core.api.BrokerService;
29 import org.opendaylight.controller.sal.core.api.RpcImplementation;
30 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
31 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
32 import org.opendaylight.controller.sal.core.api.data.DataStore;
33 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
34 import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
35 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
36 import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl;
37 import org.opendaylight.controller.sal.dom.broker.impl.DataStoreStatsWrapper;
38 import org.opendaylight.controller.sal.dom.broker.impl.HashMapDataStore;
39 import org.opendaylight.controller.sal.dom.broker.impl.RpcRouterImpl;
40 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareDataStoreAdapter;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
48 import org.reflections.Reflections;
49 import org.reflections.scanners.ResourcesScanner;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.google.common.base.Predicate;
54 import com.google.common.collect.ClassToInstanceMap;
55 import com.google.common.collect.ImmutableClassToInstanceMap;
56 import com.google.common.util.concurrent.ListeningExecutorService;
57 import com.google.common.util.concurrent.MoreExecutors;
59 import static com.google.common.base.Preconditions.*;
61 public class BindingTestContext implements AutoCloseable {
63 public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
64 .builder().toInstance();
66 private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
68 private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
70 private DomForwardedBindingBrokerImpl baBrokerImpl;
71 private DataBrokerImpl baDataImpl;
72 private NotificationBrokerImpl baNotifyImpl;
73 private BindingIndependentConnector baConnectImpl;
75 private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
76 private BrokerImpl biBrokerImpl;
77 private HashMapDataStore rawDataStore;
78 private SchemaAwareDataStoreAdapter schemaAwareDataStore;
79 private DataStoreStatsWrapper dataStoreStats;
80 private DataStore dataStore;
82 private boolean dataStoreStatisticsEnabled = false;
84 private final ListeningExecutorService executor;
85 private final ClassPool classPool;
87 private final boolean startWithSchema;
89 private MountPointManagerImpl biMountImpl;
91 protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
92 this.executor = executor;
93 this.classPool = classPool;
94 this.startWithSchema = startWithSchema;
97 public void startDomDataStore() {
98 checkState(dataStore == null, "DataStore already started.");
99 checkState(biDataImpl != null, "Dom Data Broker not present");
100 rawDataStore = new HashMapDataStore();
101 schemaAwareDataStore = new SchemaAwareDataStoreAdapter();
102 schemaAwareDataStore.changeDelegate(rawDataStore);
103 if (dataStoreStatisticsEnabled) {
104 dataStoreStats = new DataStoreStatsWrapper(schemaAwareDataStore);
105 dataStore = dataStoreStats;
107 dataStore = schemaAwareDataStore;
110 biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore);
111 biDataImpl.registerOperationalReader(TREE_ROOT, dataStore);
112 biDataImpl.registerCommitHandler(TREE_ROOT, dataStore);
115 public void startDomDataBroker() {
116 checkState(executor != null, "Executor needs to be set");
117 biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
118 biDataImpl.setExecutor(executor);
121 public void startBindingDataBroker() {
122 checkState(executor != null, "Executor needs to be set");
123 baDataImpl = new DataBrokerImpl();
124 baDataImpl.setExecutor(executor);
127 public void startBindingBroker() {
128 checkState(executor != null, "Executor needs to be set");
129 checkState(baDataImpl != null, "Binding Data Broker must be started");
130 checkState(baNotifyImpl != null, "Notification Service must be started");
131 baBrokerImpl = new DomForwardedBindingBrokerImpl("test");
133 baBrokerImpl.getMountManager().setDataCommitExecutor(executor);
134 baBrokerImpl.getMountManager().setNotificationExecutor(executor);
135 baBrokerImpl.setRpcBroker(new RpcProviderRegistryImpl("test"));
136 baBrokerImpl.setDataBroker(baDataImpl);
137 baBrokerImpl.setNotificationBroker(baNotifyImpl);
138 baBrokerImpl.start();
141 public void startForwarding() {
142 checkState(baDataImpl != null, "Binding Data Broker needs to be started");
143 checkState(biDataImpl != null, "DOM Data Broker needs to be started.");
144 checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
146 baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
147 baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
148 baBrokerImpl.setConnector(baConnectImpl);
149 baBrokerImpl.setDomProviderContext(createMockContext());
150 baBrokerImpl.startForwarding();
153 private ProviderSession createMockContext() {
154 // TODO Auto-generated method stub
155 final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
156 .<BrokerService> builder()
158 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) //
159 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
160 .put(MountProvisionService.class, biMountImpl) //
163 return new ProviderSession() {
166 public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {
167 throw new UnsupportedOperationException();
171 public <T extends BrokerService> T getService(Class<T> service) {
172 return domBrokerServices.getInstance(service);
176 public boolean isClosed() {
181 public Set<QName> getSupportedRpcs() {
186 public void close() {
190 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
191 RpcRegistrationListener listener) {
196 public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
197 throws IllegalArgumentException {
202 public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
207 public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
213 public void startBindingToDomMappingService() {
214 checkState(classPool != null, "ClassPool needs to be present");
215 mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl();
216 mappingServiceImpl.setPool(classPool);
217 mappingServiceImpl.start(null);
220 public void updateYangSchema(String[] files) {
221 SchemaContext context = getContext(files);
222 if (schemaAwareDataStore != null) {
223 schemaAwareDataStore.onGlobalContextUpdated(context);
225 if (mappingServiceImpl != null) {
226 mappingServiceImpl.onGlobalContextUpdated(context);
230 public static String[] getAllYangFilesOnClasspath() {
231 Predicate<String> predicate = new Predicate<String>() {
233 public boolean apply(String input) {
234 return input.endsWith(".yang");
237 Reflections reflection = new Reflections("META-INF.yang", new ResourcesScanner());
238 Set<String> result = reflection.getResources(predicate);
239 return (String[]) result.toArray(new String[result.size()]);
242 private static SchemaContext getContext(String[] yangFiles) {
243 ClassLoader loader = BindingTestContext.class.getClassLoader();
244 List<InputStream> streams = new ArrayList<>();
245 for (String string : yangFiles) {
246 InputStream stream = loader.getResourceAsStream(string);
249 YangParserImpl parser = new YangParserImpl();
250 Set<Module> modules = parser.parseYangModelsFromStreams(streams);
251 return parser.resolveSchemaContext(modules);
254 public void start() {
255 startBindingDataBroker();
256 startBindingNotificationBroker();
257 startBindingBroker();
258 startDomDataBroker();
261 startDomMountPoint();
262 startBindingToDomMappingService();
264 if (startWithSchema) {
265 loadYangSchemaFromClasspath();
269 private void startDomMountPoint() {
270 biMountImpl = new MountPointManagerImpl();
271 biMountImpl.setDataBroker(getDomDataBroker());
274 private void startDomBroker() {
275 checkState(executor != null);
276 biBrokerImpl = new BrokerImpl();
277 biBrokerImpl.setExecutor(executor);
278 biBrokerImpl.setRouter(new RpcRouterImpl("test"));
281 public void startBindingNotificationBroker() {
282 checkState(executor != null);
283 baNotifyImpl = new NotificationBrokerImpl(executor);
287 public void loadYangSchemaFromClasspath() {
288 String[] files = getAllYangFilesOnClasspath();
289 updateYangSchema(files);
292 public DataProviderService getBindingDataBroker() {
296 public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
300 public DataStore getDomDataStore() {
304 public BindingIndependentMappingService getBindingToDomMappingService() {
305 return mappingServiceImpl;
308 public void logDataStoreStatistics() {
309 if (dataStoreStats == null) {
313 LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
314 dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
315 dataStoreStats.getConfigurationReadAverageTime());
317 LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
318 dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(),
319 dataStoreStats.getOperationalReadAverageTime());
321 LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
322 dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
323 dataStoreStats.getRequestCommitAverageTime());
326 public RpcProviderRegistry getBindingRpcRegistry() {
327 return baBrokerImpl.getRoot();
330 public RpcProvisionRegistry getDomRpcRegistry() {
331 if (biBrokerImpl == null) {
334 return biBrokerImpl.getRouter();
337 public RpcImplementation getDomRpcInvoker() {
338 return biBrokerImpl.getRouter();
342 public void close() throws Exception {
346 public MountProviderService getBindingMountProviderService() {
347 return baBrokerImpl.getMountManager();
350 public MountProvisionService getDomMountProviderService() {