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.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;
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;
60 import static com.google.common.base.Preconditions.*;
62 public class BindingTestContext implements AutoCloseable, SchemaContextProvider {
64 public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
65 .builder().toInstance();
67 private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
69 private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
71 private DomForwardedBindingBrokerImpl baBrokerImpl;
72 private DataBrokerImpl baDataImpl;
73 private NotificationBrokerImpl baNotifyImpl;
74 private BindingIndependentConnector baConnectImpl;
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;
83 private boolean dataStoreStatisticsEnabled = false;
85 private final ListeningExecutorService executor;
86 private final ClassPool classPool;
88 private final boolean startWithSchema;
90 private MountPointManagerImpl biMountImpl;
92 private SchemaContext schemaContext;
94 public SchemaContext getSchemaContext() {
98 protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
99 this.executor = executor;
100 this.classPool = classPool;
101 this.startWithSchema = startWithSchema;
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;
114 dataStore = schemaAwareDataStore;
117 biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore);
118 biDataImpl.registerOperationalReader(TREE_ROOT, dataStore);
119 biDataImpl.registerCommitHandler(TREE_ROOT, dataStore);
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);
128 public void startBindingDataBroker() {
129 checkState(executor != null, "Executor needs to be set");
130 baDataImpl = new DataBrokerImpl();
131 baDataImpl.setExecutor(executor);
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");
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();
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.");
153 baConnectImpl = BindingDomConnectorDeployer.createConnector(getBindingToDomMappingService());
154 baConnectImpl.setDomRpcRegistry(getDomRpcRegistry());
155 baBrokerImpl.setConnector(baConnectImpl);
156 baBrokerImpl.setDomProviderContext(createMockContext());
157 baBrokerImpl.startForwarding();
160 private ProviderSession createMockContext() {
161 // TODO Auto-generated method stub
162 final ClassToInstanceMap<BrokerService> domBrokerServices = ImmutableClassToInstanceMap
163 .<BrokerService> builder()
165 .put(org.opendaylight.controller.sal.core.api.data.DataProviderService.class, biDataImpl) //
166 .put(RpcProvisionRegistry.class, biBrokerImpl.getRouter()) //
167 .put(MountProvisionService.class, biMountImpl) //
170 return new ProviderSession() {
173 public Future<RpcResult<CompositeNode>> rpc(QName rpc, CompositeNode input) {
174 throw new UnsupportedOperationException();
178 public <T extends BrokerService> T getService(Class<T> service) {
179 return domBrokerServices.getInstance(service);
183 public boolean isClosed() {
188 public Set<QName> getSupportedRpcs() {
193 public void close() {
197 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(
198 RpcRegistrationListener listener) {
203 public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
204 throws IllegalArgumentException {
209 public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
214 public RoutedRpcRegistration addMountedRpcImplementation(QName rpcType, RpcImplementation implementation) {
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);
227 public void updateYangSchema(String[] files) {
228 schemaContext = getContext(files);
229 if (schemaAwareDataStore != null) {
230 schemaAwareDataStore.onGlobalContextUpdated(schemaContext);
232 if (mappingServiceImpl != null) {
233 mappingServiceImpl.onGlobalContextUpdated(schemaContext);
237 public static String[] getAllYangFilesOnClasspath() {
238 Predicate<String> predicate = new Predicate<String>() {
240 public boolean apply(String input) {
241 return input.endsWith(".yang");
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()]);
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);
256 YangParserImpl parser = new YangParserImpl();
257 Set<Module> modules = parser.parseYangModelsFromStreams(streams);
258 return parser.resolveSchemaContext(modules);
261 public void start() {
262 startBindingDataBroker();
263 startBindingNotificationBroker();
264 startBindingBroker();
265 startDomDataBroker();
268 startDomMountPoint();
269 startBindingToDomMappingService();
271 if (startWithSchema) {
272 loadYangSchemaFromClasspath();
276 private void startDomMountPoint() {
277 biMountImpl = new MountPointManagerImpl();
278 biMountImpl.setDataBroker(getDomDataBroker());
281 private void startDomBroker() {
282 checkState(executor != null);
283 biBrokerImpl = new BrokerImpl();
284 biBrokerImpl.setExecutor(executor);
285 biBrokerImpl.setRouter(new SchemaAwareRpcBroker("/", this));
288 public void startBindingNotificationBroker() {
289 checkState(executor != null);
290 baNotifyImpl = new NotificationBrokerImpl(executor);
294 public void loadYangSchemaFromClasspath() {
295 String[] files = getAllYangFilesOnClasspath();
296 updateYangSchema(files);
299 public DataProviderService getBindingDataBroker() {
303 public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
307 public DataStore getDomDataStore() {
311 public BindingIndependentMappingService getBindingToDomMappingService() {
312 return mappingServiceImpl;
315 public void logDataStoreStatistics() {
316 if (dataStoreStats == null) {
320 LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
321 dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
322 dataStoreStats.getConfigurationReadAverageTime());
324 LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
325 dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(),
326 dataStoreStats.getOperationalReadAverageTime());
328 LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
329 dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
330 dataStoreStats.getRequestCommitAverageTime());
333 public RpcProviderRegistry getBindingRpcRegistry() {
334 return baBrokerImpl.getRoot();
337 public RpcProvisionRegistry getDomRpcRegistry() {
338 if (biBrokerImpl == null) {
341 return biBrokerImpl.getRouter();
344 public RpcImplementation getDomRpcInvoker() {
345 return biBrokerImpl.getRouter();
349 public void close() throws Exception {
353 public MountProviderService getBindingMountProviderService() {
354 return baBrokerImpl.getMountManager();
357 public MountProvisionService getDomMountProviderService() {