Merge "Refactor shutdown-impl: add parameter to RPC, remove secret validation and...
[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.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;
52
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;
58
59 import static com.google.common.base.Preconditions.*;
60
61 public class BindingTestContext implements AutoCloseable {
62
63     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
64             .builder().toInstance();
65
66     private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
67
68     private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
69
70     private DomForwardedBindingBrokerImpl baBrokerImpl;
71     private DataBrokerImpl baDataImpl;
72     private NotificationBrokerImpl baNotifyImpl;
73     private BindingIndependentConnector baConnectImpl;
74
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;
81
82     private boolean dataStoreStatisticsEnabled = false;
83
84     private final ListeningExecutorService executor;
85     private final ClassPool classPool;
86
87     private final boolean startWithSchema;
88
89     private MountPointManagerImpl biMountImpl;
90
91     protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
92         this.executor = executor;
93         this.classPool = classPool;
94         this.startWithSchema = startWithSchema;
95     }
96
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;
106         } else {
107             dataStore = schemaAwareDataStore;
108         }
109
110         biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore);
111         biDataImpl.registerOperationalReader(TREE_ROOT, dataStore);
112         biDataImpl.registerCommitHandler(TREE_ROOT, dataStore);
113     }
114
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);
119     }
120
121     public void startBindingDataBroker() {
122         checkState(executor != null, "Executor needs to be set");
123         baDataImpl = new DataBrokerImpl();
124         baDataImpl.setExecutor(executor);
125     }
126
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");
132
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();
139     }
140
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.");
145
146         baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
147         baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
148         baBrokerImpl.setConnector(baConnectImpl);
149         baBrokerImpl.setDomProviderContext(createMockContext());
150         baBrokerImpl.startForwarding();
151     }
152
153     private ProviderSession createMockContext() {
154         // TODO Auto-generated method stub
155         final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
156                 .<BrokerService> builder()
157                 //
158                 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) //
159                 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
160                 .put(MountProvisionService.class, biMountImpl) //
161                 .build();
162
163         return new ProviderSession() {
164
165             @Override
166             public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {
167                 throw new UnsupportedOperationException();
168             }
169
170             @Override
171             public <T extends BrokerService> T getService(Class<T> service) {
172                 return domBrokerServices.getInstance(service);
173             }
174
175             @Override
176             public boolean isClosed() {
177                 return false;
178             }
179
180             @Override
181             public Set<QName> getSupportedRpcs() {
182                 return null;
183             }
184
185             @Override
186             public void close() {
187             }
188
189             @Override
190             public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
191                     RpcRegistrationListener listener) {
192                 return null;
193             }
194
195             @Override
196             public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
197                     throws IllegalArgumentException {
198                 return null;
199             }
200
201             @Override
202             public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
203                 return null;
204             }
205
206             @Override
207             public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
208                 return null;
209             }
210         };
211     }
212
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);
218     }
219
220     public void updateYangSchema(String[] files) {
221         SchemaContext context = getContext(files);
222         if (schemaAwareDataStore != null) {
223             schemaAwareDataStore.onGlobalContextUpdated(context);
224         }
225         if (mappingServiceImpl != null) {
226             mappingServiceImpl.onGlobalContextUpdated(context);
227         }
228     }
229
230     public static String[] getAllYangFilesOnClasspath() {
231         Predicate<String> predicate = new Predicate<String>() {
232             @Override
233             public boolean apply(String input) {
234                 return input.endsWith(".yang");
235             }
236         };
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()]);
240     }
241
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);
247             streams.add(stream);
248         }
249         YangParserImpl parser = new YangParserImpl();
250         Set<Module> modules = parser.parseYangModelsFromStreams(streams);
251         return parser.resolveSchemaContext(modules);
252     }
253
254     public void start() {
255         startBindingDataBroker();
256         startBindingNotificationBroker();
257         startBindingBroker();
258         startDomDataBroker();
259         startDomDataStore();
260         startDomBroker();
261         startDomMountPoint();
262         startBindingToDomMappingService();
263         startForwarding();
264         if (startWithSchema) {
265             loadYangSchemaFromClasspath();
266         }
267     }
268
269     private void startDomMountPoint() {
270         biMountImpl = new MountPointManagerImpl();
271         biMountImpl.setDataBroker(getDomDataBroker());
272     }
273
274     private void startDomBroker() {
275         checkState(executor != null);
276         biBrokerImpl = new BrokerImpl();
277         biBrokerImpl.setExecutor(executor);
278         biBrokerImpl.setRouter(new RpcRouterImpl("test"));
279     }
280
281     public void startBindingNotificationBroker() {
282         checkState(executor != null);
283         baNotifyImpl = new NotificationBrokerImpl(executor);
284
285     }
286
287     public void loadYangSchemaFromClasspath() {
288         String[] files = getAllYangFilesOnClasspath();
289         updateYangSchema(files);
290     }
291
292     public DataProviderService getBindingDataBroker() {
293         return baDataImpl;
294     }
295
296     public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
297         return biDataImpl;
298     }
299
300     public DataStore getDomDataStore() {
301         return dataStore;
302     }
303
304     public BindingIndependentMappingService getBindingToDomMappingService() {
305         return mappingServiceImpl;
306     }
307
308     public void logDataStoreStatistics() {
309         if (dataStoreStats == null) {
310             return;
311         }
312
313         LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
314                 dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
315                 dataStoreStats.getConfigurationReadAverageTime());
316
317         LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
318                 dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(),
319                 dataStoreStats.getOperationalReadAverageTime());
320
321         LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
322                 dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
323                 dataStoreStats.getRequestCommitAverageTime());
324     }
325
326     public RpcProviderRegistry getBindingRpcRegistry() {
327         return baBrokerImpl.getRoot();
328     }
329
330     public RpcProvisionRegistry getDomRpcRegistry() {
331         if (biBrokerImpl == null) {
332             return null;
333         }
334         return biBrokerImpl.getRouter();
335     }
336
337     public RpcImplementation getDomRpcInvoker() {
338         return biBrokerImpl.getRouter();
339     }
340
341     @Override
342     public void close() throws Exception {
343
344     }
345
346     public MountProviderService getBindingMountProviderService() {
347         return baBrokerImpl.getMountManager();
348     }
349
350     public MountProvisionService getDomMountProviderService() {
351         return biMountImpl;
352     }
353 }