3217a31329939e5b8d27c67f3114bbb82b9f498a
[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
8 import javassist.ClassPool;
9
10 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
11 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
12 import org.opendaylight.controller.sal.binding.dom.serializer.impl.RuntimeGeneratedMappingServiceImpl;
13 import org.opendaylight.controller.sal.binding.impl.BindingAwareBrokerImpl;
14 import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl;
15 import org.opendaylight.controller.sal.binding.impl.NotificationBrokerImpl;
16 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
17 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentMappingService;
18 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
19 import org.opendaylight.controller.sal.core.api.RpcImplementation;
20 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
21 import org.opendaylight.controller.sal.core.api.data.DataStore;
22 import org.opendaylight.controller.sal.dom.broker.BrokerImpl;
23 import org.opendaylight.controller.sal.dom.broker.impl.DataStoreStatsWrapper;
24 import org.opendaylight.controller.sal.dom.broker.impl.HashMapDataStore;
25 import org.opendaylight.controller.sal.dom.broker.impl.RpcRouterImpl;
26 import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareDataStoreAdapter;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 import org.reflections.Reflections;
31 import org.reflections.scanners.ResourcesScanner;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Predicate;
36 import com.google.common.util.concurrent.ListeningExecutorService;
37 import com.google.common.util.concurrent.MoreExecutors;
38
39 import static com.google.common.base.Preconditions.*;
40
41 public class BindingTestContext implements AutoCloseable {
42     
43     
44     public static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier TREE_ROOT = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
45             .builder().toInstance();
46
47     private static final Logger LOG = LoggerFactory.getLogger(BindingTestContext.class);
48     
49     private RuntimeGeneratedMappingServiceImpl mappingServiceImpl;
50     
51     
52     private BindingAwareBrokerImpl baBrokerImpl;
53     private DataBrokerImpl baDataImpl;
54     private NotificationBrokerImpl baNotifyImpl;
55     private BindingIndependentConnector baConnectDataServiceImpl;
56
57     private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
58     private BrokerImpl biBrokerImpl;
59     private HashMapDataStore rawDataStore;
60     private SchemaAwareDataStoreAdapter schemaAwareDataStore;
61     private DataStoreStatsWrapper dataStoreStats;
62     private DataStore dataStore;
63
64     
65     private boolean dataStoreStatisticsEnabled = false;
66     
67     private final ListeningExecutorService executor;
68     private final ClassPool classPool;
69
70     private final boolean startWithSchema;
71
72     
73     protected BindingTestContext(ListeningExecutorService executor, ClassPool classPool, boolean startWithSchema) {
74         this.executor = executor;
75         this.classPool = classPool;
76         this.startWithSchema = startWithSchema;
77     }
78
79     public void startDomDataStore() {
80         checkState(dataStore == null, "DataStore already started.");
81         checkState(biDataImpl != null, "Dom Data Broker not present");
82         rawDataStore = new HashMapDataStore();
83         schemaAwareDataStore = new SchemaAwareDataStoreAdapter();
84         schemaAwareDataStore.changeDelegate(rawDataStore);
85         if(dataStoreStatisticsEnabled) {
86         dataStoreStats = new DataStoreStatsWrapper(schemaAwareDataStore);
87         dataStore = dataStoreStats;
88         } else {
89             dataStore = schemaAwareDataStore;
90         }
91         
92         biDataImpl.registerConfigurationReader(TREE_ROOT, dataStore);
93         biDataImpl.registerOperationalReader(TREE_ROOT, dataStore);
94         biDataImpl.registerCommitHandler(TREE_ROOT, dataStore);
95     }
96     
97     public void startDomDataBroker() {
98         checkState(executor != null,"Executor needs to be set");
99         biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
100         biDataImpl.setExecutor(executor);
101     }
102     
103     public void startBindingDataBroker() {
104         checkState(executor != null,"Executor needs to be set");
105         baDataImpl = new DataBrokerImpl();
106         baDataImpl.setExecutor(executor);
107     }
108     
109     public void startBindingBroker() {
110         checkState(executor != null,"Executor needs to be set");
111         checkState(baDataImpl != null,"Binding Data Broker must be started");
112         checkState(baNotifyImpl != null, "Notification Service must be started");
113         baBrokerImpl = new BindingAwareBrokerImpl(null);
114         
115         baBrokerImpl.setDataBroker(baDataImpl);
116         baBrokerImpl.setNotifyBroker(baNotifyImpl);
117         
118         baBrokerImpl.start();
119     }
120     
121     public void startBindingToDomDataConnector() {
122         checkState(baDataImpl != null,"Binding Data Broker needs to be started");
123         checkState(biDataImpl != null,"DOM Data Broker needs to be started.");
124         checkState(mappingServiceImpl != null,"DOM Mapping Service needs to be started.");
125         baConnectDataServiceImpl = new BindingIndependentConnector();
126         baConnectDataServiceImpl.setRpcRegistry(baBrokerImpl);
127         baConnectDataServiceImpl.setDomRpcRegistry(getDomRpcRegistry());
128         baConnectDataServiceImpl.setBaDataService(baDataImpl);
129         baConnectDataServiceImpl.setBiDataService(biDataImpl);
130         baConnectDataServiceImpl.setMappingService(mappingServiceImpl);
131         baConnectDataServiceImpl.start();
132     }
133     
134     public void startBindingToDomMappingService() {
135         checkState(classPool != null,"ClassPool needs to be present");
136         mappingServiceImpl = new RuntimeGeneratedMappingServiceImpl();
137         mappingServiceImpl.setPool(classPool);
138         mappingServiceImpl.start(null);
139     }
140     
141     
142     public void updateYangSchema(String[] files) {
143         SchemaContext context = getContext(files);
144         if(schemaAwareDataStore != null) {
145             schemaAwareDataStore.onGlobalContextUpdated(context);
146         }
147         if(mappingServiceImpl != null) {
148             mappingServiceImpl.onGlobalContextUpdated(context);
149         }
150     }
151     
152     
153     public static String[] getAllYangFilesOnClasspath() {
154         Predicate<String> predicate = new Predicate<String>() {
155             @Override
156             public boolean apply(String input) {
157                 return input.endsWith(".yang");
158             }
159         };
160         Reflections reflection = new Reflections("META-INF.yang", new ResourcesScanner());
161         Set<String> result = reflection.getResources(predicate);
162         return (String[]) result.toArray(new String[result.size()]);
163     }
164     
165     private static SchemaContext getContext(String[] yangFiles) {
166         ClassLoader loader = BindingTestContext.class.getClassLoader();
167         List<InputStream> streams = new ArrayList<>();
168         for (String string : yangFiles) {
169             InputStream stream = loader.getResourceAsStream(string);
170             streams.add(stream);
171         }
172         YangParserImpl parser = new YangParserImpl();
173         Set<Module> modules = parser.parseYangModelsFromStreams(streams);
174         return parser.resolveSchemaContext(modules);
175     }
176     
177     public void start() {
178         startBindingDataBroker();
179         startBindingNotificationBroker();
180         startBindingBroker();
181         startDomDataBroker();
182         startDomDataStore();
183         startDomBroker();
184         startBindingToDomMappingService();
185         startBindingToDomDataConnector();
186         if(startWithSchema) {
187             loadYangSchemaFromClasspath();
188         }
189     }
190
191     private void startDomBroker() {
192         checkState(executor != null);
193         biBrokerImpl = new BrokerImpl();
194         biBrokerImpl.setExecutor(executor);
195         biBrokerImpl.setRouter(new RpcRouterImpl("test"));
196     }
197
198     public void startBindingNotificationBroker() {
199         checkState(executor != null);
200         baNotifyImpl = new NotificationBrokerImpl(executor);
201         
202     }
203
204     public void loadYangSchemaFromClasspath() {
205         String[] files = getAllYangFilesOnClasspath();
206         updateYangSchema(files);
207     }
208
209     public DataProviderService getBindingDataBroker() {
210         return baDataImpl;
211     }
212
213     public org.opendaylight.controller.sal.core.api.data.DataProviderService getDomDataBroker() {
214         return biDataImpl;
215     }
216
217     public DataStore getDomDataStore() {
218         return dataStore;
219     }
220
221     public BindingIndependentMappingService getBindingToDomMappingService() {
222         return mappingServiceImpl;
223     }
224
225     public void logDataStoreStatistics() {
226         if(dataStoreStats == null) {
227             return;
228         }
229         
230         LOG.info("BIDataStore Statistics: Configuration Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
231                 dataStoreStats.getConfigurationReadCount(), dataStoreStats.getConfigurationReadTotalTime(),
232                 dataStoreStats.getConfigurationReadAverageTime());
233
234         LOG.info("BIDataStore Statistics: Operational Read Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
235                 dataStoreStats.getOperationalReadCount(), dataStoreStats.getOperationalReadTotalTime(),
236                 dataStoreStats.getOperationalReadAverageTime());
237
238         LOG.info("BIDataStore Statistics: Request Commit Count: {} TotalTime: {} ms AverageTime (ns): {} ms",
239                 dataStoreStats.getRequestCommitCount(), dataStoreStats.getRequestCommitTotalTime(),
240                 dataStoreStats.getRequestCommitAverageTime());
241     }
242
243     public RpcProviderRegistry getBindingRpcRegistry() {
244         return baBrokerImpl;
245     }
246
247     public RpcProvisionRegistry getDomRpcRegistry() {
248         if(biBrokerImpl == null) {
249             return null;
250         }
251         return biBrokerImpl.getRouter();
252     }
253     
254     public RpcImplementation getDomRpcInvoker() {
255         return biBrokerImpl.getRouter();
256     }
257     
258     @Override
259     public void close() throws Exception {
260         
261     }
262 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.