2 * Copyright (c) 2018 AT&T and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.fd.honeycomb.transportpce.test.common;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Optional;
21 import java.util.ServiceLoader;
23 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
24 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
25 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
26 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
27 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
28 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
29 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
30 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.util.ListenerRegistry;
33 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
34 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import javassist.ClassPool;
43 public class DataStoreContextImpl implements DataStoreContext {
45 private static final Logger LOG = LoggerFactory.getLogger(DataStoreContextImpl.class);
47 // private final Map<LogicalDatastoreType, DOMStore> datastores;
48 private final SchemaContextHolder mockedSchemaContext;
49 // private final DOMNotificationRouter domNotificationRouter;
50 // private final DOMDataBroker domDataBroker;
51 // private final DataBroker dataBroker;
52 // private final NotificationService notificationService;
53 // private final NotificationPublishService notificationPublishService;
55 public DataStoreContextImpl() {
59 public DataStoreContextImpl(boolean fromClasspath) {
60 this.mockedSchemaContext = new SchemaContextHolder(fromClasspath);
61 // this.datastores = createDatastores();
62 // this.domNotificationRouter = DOMNotificationRouter.create(16);
63 // this.domDataBroker = createDOMDataBroker();
64 // this.dataBroker = createDataBroker();
65 // this.notificationService = createNotificationService();
66 // this.notificationPublishService = createNotificationPublishService();
67 // for (ListenerRegistration<SchemaContextListener> listener : this.mockedSchemaContext.listeners) {
68 // listener.getInstance().onGlobalContextUpdated(this.mockedSchemaContext.schemaContext);
73 // public DataBroker getDataBroker() {
74 // return this.dataBroker;
78 // public DOMDataBroker getDOMDataBroker() {
79 // return this.domDataBroker;
83 // public NotificationService createNotificationService() {
84 // return new BindingDOMNotificationServiceAdapter(this.mockedSchemaContext.bindingStreamCodecs,
85 // this.domNotificationRouter);
89 // public NotificationPublishService createNotificationPublishService() {
90 // return new BindingDOMNotificationPublishServiceAdapter(this.mockedSchemaContext.bindingToNormalized,
91 // this.domNotificationRouter);
95 public SchemaContext getSchemaContext() {
96 return this.mockedSchemaContext.schemaContext;
100 public BindingNormalizedNodeCodecRegistry getBindingToNormalizedNodeCodec() {
101 return this.mockedSchemaContext.bindingStreamCodecs;
105 // public NotificationService getNotificationService() {
106 // return this.notificationService;
110 // public NotificationPublishService getNotificationPublishService() {
111 // return this.notificationPublishService;
114 // private DOMDataBroker createDOMDataBroker() {
115 // return new SerializedDOMDataBroker(this.datastores,
116 // MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
119 // private ListeningExecutorService getDataTreeChangeListenerExecutor() {
120 // return MoreExecutors.newDirectExecutorService();
123 // private DataBroker createDataBroker() {
124 // return new BindingDOMDataBrokerAdapter(getDOMDataBroker(), this.mockedSchemaContext.bindingToNormalized);
127 // private Map<LogicalDatastoreType, DOMStore> createDatastores() {
128 // return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
129 // .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
130 // .put(LogicalDatastoreType.CONFIGURATION, createConfigurationDatastore()).build();
133 // private DOMStore createConfigurationDatastore() {
134 // final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
135 // this.mockedSchemaContext.registerSchemaContextListener(store);
139 // private DOMStore createOperationalDatastore() {
140 // final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
141 // this.mockedSchemaContext.registerSchemaContextListener(store);
145 private class SchemaContextHolder implements DOMSchemaService, SchemaContextProvider {
147 private final SchemaContext schemaContext;
148 private final ListenerRegistry<SchemaContextListener> listeners;
149 private final BindingNormalizedNodeCodecRegistry bindingStreamCodecs;
150 private final BindingToNormalizedNodeCodec bindingToNormalized;
151 private final ModuleInfoBackedContext moduleInfoBackedCntxt;
153 private SchemaContextHolder(boolean fromClasspath) {
154 List<YangModuleInfo> moduleInfos = loadModuleInfos();
155 this.moduleInfoBackedCntxt = ModuleInfoBackedContext.create();
156 this.schemaContext = getSchemaContext(moduleInfos);
157 this.listeners = ListenerRegistry.create();
158 this.bindingStreamCodecs = createBindingRegistry();
159 GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
160 this.bindingToNormalized = new BindingToNormalizedNodeCodec(loading, this.bindingStreamCodecs);
161 registerSchemaContextListener(this.bindingToNormalized);
165 public SchemaContext getSchemaContext() {
166 return this.schemaContext;
170 * Get the schemacontext from loaded modules on classpath.
172 * @param moduleInfos a list of Yang module Infos
173 * @return SchemaContext a schema context
175 private SchemaContext getSchemaContext(List<YangModuleInfo> moduleInfos) {
176 this.moduleInfoBackedCntxt.addModuleInfos(moduleInfos);
177 Optional<SchemaContext> tryToCreateSchemaContext =
178 this.moduleInfoBackedCntxt.tryToCreateSchemaContext().toJavaUtil();
179 if (!tryToCreateSchemaContext.isPresent()) {
180 LOG.error("Could not create the initial schema context. Schema context is empty");
181 throw new IllegalStateException();
183 return tryToCreateSchemaContext.get();
187 public SchemaContext getGlobalContext() {
188 return this.schemaContext;
192 public SchemaContext getSessionContext() {
193 return this.schemaContext;
197 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
198 SchemaContextListener listener) {
199 return this.listeners.register(listener);
203 * Loads all {@link YangModelBindingProvider} on the classpath.
205 * @return list of known {@link YangModuleInfo}
207 private List<YangModuleInfo> loadModuleInfos() {
208 List<YangModuleInfo> moduleInfos = new LinkedList<>();
209 ServiceLoader<YangModelBindingProvider> yangProviderLoader =
210 ServiceLoader.load(YangModelBindingProvider.class);
211 for (YangModelBindingProvider yangModelBindingProvider : yangProviderLoader) {
212 moduleInfos.add(yangModelBindingProvider.getModuleInfo());
213 LOG.debug("Adding [{}] module into known modules", yangModelBindingProvider.getModuleInfo());
219 * Creates binding registry.
221 * @return BindingNormalizedNodeCodecRegistry the resulting binding registry
223 private BindingNormalizedNodeCodecRegistry createBindingRegistry() {
224 BindingRuntimeContext bindingContext = BindingRuntimeContext.create(this.moduleInfoBackedCntxt, this.schemaContext);
225 BindingNormalizedNodeCodecRegistry bindingNormalizedNodeCodecRegistry =
226 new BindingNormalizedNodeCodecRegistry(
227 StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault())));
228 bindingNormalizedNodeCodecRegistry.onBindingRuntimeContextUpdated(bindingContext);
229 return bindingNormalizedNodeCodecRegistry;