Merge "Avoiding hash collisions of a match with its reverse"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / util / BindingTestContext.java
1 package org.opendaylight.controller.sal.binding.test.util;
2
3 import java.io.InputStream;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.concurrent.Future;
8
9 import javassist.ClassPool;
10
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.SchemaAwareDataStoreAdapter;
40 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
41 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
49 import org.reflections.Reflections;
50 import org.reflections.scanners.ResourcesScanner;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 import com.google.common.base.Predicate;
55 import com.google.common.collect.ClassToInstanceMap;
56 import com.google.common.collect.ImmutableClassToInstanceMap;
57 import com.google.common.util.concurrent.ListeningExecutorService;
58 import com.google.common.util.concurrent.MoreExecutors;
59
60 import static com.google.common.base.Preconditions.*;
61
62 public class BindingTestContext implements AutoCloseable, SchemaContextProvider {
63
64     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
65             .builder().toInstance();
66
67     private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
68
69     private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
70
71     private DomForwardedBindingBrokerImpl baBrokerImpl;
72     private DataBrokerImpl baDataImpl;
73     private NotificationBrokerImpl baNotifyImpl;
74     private BindingIndependentConnector baConnectImpl;
75
76     private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
77     private BrokerImpl biBrokerImpl;
78     private HashMapDataStore rawDataStore;
79     private SchemaAwareDataStoreAdapter schemaAwareDataStore;
80     private DataStoreStatsWrapper dataStoreStats;
81     private DataStore dataStore;
82
83     private boolean dataStoreStatisticsEnabled = false;
84
85     private final ListeningExecutorService executor;
86     private final ClassPool classPool;
87
88     private final boolean startWithSchema;
89
90     private MountPointManagerImpl biMountImpl;
91
92     private SchemaContext schemaContext;
93
94     public SchemaContext getSchemaContext() {
95         return schemaContext;
96     }
97
98     protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
99         this.executor = executor;
100         this.classPool = classPool;
101         this.startWithSchema = startWithSchema;
102     }
103
104     public void startDomDataStore() {
105         checkState(dataStore == null, "DataStore already started.");
106         checkState(biDataImpl != null, "Dom Data Broker not present");
107         rawDataStore = new HashMapDataStore();
108         schemaAwareDataStore = new SchemaAwareDataStoreAdapter();
109         schemaAwareDataStore.changeDelegate(rawDataStore);
110         if (dataStoreStatisticsEnabled) {
111             dataStoreStats = new DataStoreStatsWrapper(schemaAwareDataStore);
112             dataStore = dataStoreStats;
113         } else {
114             dataStore = schemaAwareDataStore;
115         }
116
117         biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore);
118         biDataImpl.registerOperationalReader(TREE_ROOT, dataStore);
119         biDataImpl.registerCommitHandler(TREE_ROOT, dataStore);
120     }
121
122     public void startDomDataBroker() {
123         checkState(executor != null, "Executor needs to be set");
124         biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
125         biDataImpl.setExecutor(executor);
126     }
127
128     public void startBindingDataBroker() {
129         checkState(executor != null, "Executor needs to be set");
130         baDataImpl = new DataBrokerImpl();
131         baDataImpl.setExecutor(executor);
132     }
133
134     public void startBindingBroker() {
135         checkState(executor != null, "Executor needs to be set");
136         checkState(baDataImpl != null, "Binding Data Broker must be started");
137         checkState(baNotifyImpl != null, "Notification Service must be started");
138         baBrokerImpl = new DomForwardedBindingBrokerImpl("test");
139
140         baBrokerImpl.getMountManager().setDataCommitExecutor(executor);
141         baBrokerImpl.getMountManager().setNotificationExecutor(executor);
142         baBrokerImpl.setRpcBroker(new RpcProviderRegistryImpl("test"));
143         baBrokerImpl.setDataBroker(baDataImpl);
144         baBrokerImpl.setNotificationBroker(baNotifyImpl);
145         baBrokerImpl.start();
146     }
147
148     public void startForwarding() {
149         checkState(baDataImpl != null, "Binding Data Broker needs to be started");
150         checkState(biDataImpl != null, "DOM Data Broker needs to be started.");
151         checkState(mappingServiceImpl != null, "DOM Mapping Service needs to be started.");
152
153         baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
154         baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
155         baBrokerImpl.setConnector(baConnectImpl);
156         baBrokerImpl.setDomProviderContext(createMockContext());
157         baBrokerImpl.startForwarding();
158     }
159
160     private ProviderSession createMockContext() {
161         // TODO Auto-generated method stub
162         final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
163                 .<BrokerService> builder()
164                 //
165                 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) //
166                 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
167                 .put(MountProvisionService.class, biMountImpl) //
168                 .build();
169
170         return new ProviderSession() {
171
172             @Override
173             public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {
174                 throw new UnsupportedOperationException();
175             }
176
177             @Override
178             public <T extends BrokerService> T getService(Class<T> service) {
179                 return domBrokerServices.getInstance(service);
180             }
181
182             @Override
183             public boolean isClosed() {
184                 return false;
185             }
186
187             @Override
188             public Set<QName> getSupportedRpcs() {
189                 return null;
190             }
191
192             @Override
193             public void close() {
194             }
195
196             @Override
197             public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
198                     RpcRegistrationListener listener) {
199                 return null;
200             }
201
202             @Override
203             public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
204                     throws IllegalArgumentException {
205                 return null;
206             }
207
208             @Override
209             public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
210                 return null;
211             }
212
213             @Override
214             public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
215                 return null;
216             }
217         };
218     }
219
220     public void startBindingToDomMappingService() {
221         checkState(classPool != null, "ClassPool needs to be present");
222         mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl();
223         mappingServiceImpl.setPool(classPool);
224         mappingServiceImpl.start(null);
225     }
226
227     public void updateYangSchema(String[] files) {
228         schemaContext = getContext(files);
229         if (schemaAwareDataStore != null) {
230             schemaAwareDataStore.onGlobalContextUpdated(schemaContext);
231         }
232         if (mappingServiceImpl != null) {
233             mappingServiceImpl.onGlobalContextUpdated(schemaContext);
234         }
235     }
236
237     public static String[] getAllYangFilesOnClasspath() {
238         Predicate<String> predicate = new Predicate<String>() {
239             @Override
240             public boolean apply(String input) {
241                 return input.endsWith(".yang");
242             }
243         };
244         Reflections reflection = new Reflections("META-INF.yang", new ResourcesScanner());
245         Set<String> result = reflection.getResources(predicate);
246         return (String[]) result.toArray(new String[result.size()]);
247     }
248
249     private static SchemaContext getContext(String[] yangFiles) {
250         ClassLoader loader = BindingTestContext.class.getClassLoader();
251         List<InputStream> streams = new ArrayList<>();
252         for (String string : yangFiles) {
253             InputStream stream = loader.getResourceAsStream(string);
254             streams.add(stream);
255         }
256         YangParserImpl parser = new YangParserImpl();
257         Set<Module> modules = parser.parseYangModelsFromStreams(streams);
258         return parser.resolveSchemaContext(modules);
259     }
260
261     public void start() {
262         startBindingDataBroker();
263         startBindingNotificationBroker();
264         startBindingBroker();
265         startDomDataBroker();
266         startDomDataStore();
267         startDomBroker();
268         startDomMountPoint();
269         startBindingToDomMappingService();
270         startForwarding();
271         if (startWithSchema) {
272             loadYangSchemaFromClasspath();
273         }
274     }
275
276     private void startDomMountPoint() {
277         biMountImpl = new MountPointManagerImpl();
278         biMountImpl.setDataBroker(getDomDataBroker());
279     }
280
281     private void startDomBroker() {
282         checkState(executor != null);
283         biBrokerImpl = new BrokerImpl();
284         biBrokerImpl.setExecutor(executor);
285         biBrokerImpl.setRouter(new SchemaAwareRpcBroker("/", this));
286     }
287
288     public void startBindingNotificationBroker() {
289         checkState(executor != null);
290         baNotifyImpl = new NotificationBrokerImpl(executor);
291
292     }
293
294     public void loadYangSchemaFromClasspath() {
295         String[] files = getAllYangFilesOnClasspath();
296         updateYangSchema(files);
297     }
298
299     public DataProviderService getBindingDataBroker() {
300         return baDataImpl;
301     }
302
303     public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
304         return biDataImpl;
305     }
306
307     public DataStore getDomDataStore() {
308         return dataStore;
309     }
310
311     public BindingIndependentMappingService getBindingToDomMappingService() {
312         return mappingServiceImpl;
313     }
314
315     public void logDataStoreStatistics() {
316         if (dataStoreStats == null) {
317             return;
318         }
319
320         LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
321                 dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
322                 dataStoreStats.getConfigurationReadAverageTime());
323
324         LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
325                 dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(),
326                 dataStoreStats.getOperationalReadAverageTime());
327
328         LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
329                 dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
330                 dataStoreStats.getRequestCommitAverageTime());
331     }
332
333     public RpcProviderRegistry getBindingRpcRegistry() {
334         return baBrokerImpl.getRoot();
335     }
336
337     public RpcProvisionRegistry getDomRpcRegistry() {
338         if (biBrokerImpl == null) {
339             return null;
340         }
341         return biBrokerImpl.getRouter();
342     }
343
344     public RpcImplementation getDomRpcInvoker() {
345         return biBrokerImpl.getRouter();
346     }
347
348     @Override
349     public void close() throws Exception {
350
351     }
352
353     public MountProviderService getBindingMountProviderService() {
354         return baBrokerImpl.getMountManager();
355     }
356
357     public MountProvisionService getDomMountProviderService() {
358         return biMountImpl;
359     }
360 }