f8bd3636a1cc69ebbaefceadb7aa7c5d3ddef890
[transportpce.git] / tests / honeynode / 2.2.1 / honeynode-common / src / main / java / io / fd / honeycomb / transportpce / test / common / DataStoreContextImpl.java
1 /*
2  * Copyright (c) 2018 AT&T and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.fd.honeycomb.transportpce.test.common;
17
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Optional;
21 import java.util.ServiceLoader;
22
23 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
24 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
25 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
26 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
27 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
28 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
29 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.util.ListenerRegistry;
34 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
35 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.collect.ClassToInstanceMap;
43
44 import javassist.ClassPool;
45
46 public class DataStoreContextImpl implements DataStoreContext {
47
48     private static final Logger LOG = LoggerFactory.getLogger(DataStoreContextImpl.class);
49
50 //    private final Map<LogicalDatastoreType, DOMStore> datastores;
51     private final SchemaContextHolder mockedSchemaContext;
52 //    private final DOMNotificationRouter domNotificationRouter;
53 //    private final DOMDataBroker domDataBroker;
54 //    private final DataBroker dataBroker;
55 //    private final NotificationService notificationService;
56 //    private final NotificationPublishService notificationPublishService;
57
58     public DataStoreContextImpl() {
59         this(false);
60     }
61
62     public DataStoreContextImpl(boolean fromClasspath) {
63         this.mockedSchemaContext = new SchemaContextHolder(fromClasspath);
64 //        this.datastores = createDatastores();
65 //        this.domNotificationRouter = DOMNotificationRouter.create(16);
66 //        this.domDataBroker = createDOMDataBroker();
67 //        this.dataBroker = createDataBroker();
68 //        this.notificationService = createNotificationService();
69 //        this.notificationPublishService = createNotificationPublishService();
70 //        for (ListenerRegistration<SchemaContextListener> listener : this.mockedSchemaContext.listeners) {
71 //            listener.getInstance().onGlobalContextUpdated(this.mockedSchemaContext.schemaContext);
72 //        }
73     }
74
75 //    @Override
76 //    public DataBroker getDataBroker() {
77 //        return this.dataBroker;
78 //    }
79 //
80 //    @Override
81 //    public DOMDataBroker getDOMDataBroker() {
82 //        return this.domDataBroker;
83 //    }
84 //
85 //    @Override
86 //    public NotificationService createNotificationService() {
87 //        return new BindingDOMNotificationServiceAdapter(this.mockedSchemaContext.bindingStreamCodecs,
88 //                this.domNotificationRouter);
89 //    }
90 //
91 //    @Override
92 //    public NotificationPublishService createNotificationPublishService() {
93 //        return new BindingDOMNotificationPublishServiceAdapter(this.mockedSchemaContext.bindingToNormalized,
94 //                this.domNotificationRouter);
95 //    }
96
97     @Override
98     public SchemaContext getSchemaContext() {
99         return this.mockedSchemaContext.schemaContext;
100     }
101
102     @Override
103     public BindingNormalizedNodeCodecRegistry getBindingToNormalizedNodeCodec() {
104         return this.mockedSchemaContext.bindingStreamCodecs;
105     }
106
107 //    @Override
108 //    public NotificationService getNotificationService() {
109 //        return this.notificationService;
110 //    }
111 //
112 //    @Override
113 //    public NotificationPublishService getNotificationPublishService() {
114 //        return this.notificationPublishService;
115 //    }
116 //
117 //    private DOMDataBroker createDOMDataBroker() {
118 //        return new SerializedDOMDataBroker(this.datastores,
119 //                MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
120 //    }
121 //
122 //    private ListeningExecutorService getDataTreeChangeListenerExecutor() {
123 //        return MoreExecutors.newDirectExecutorService();
124 //    }
125 //
126 //    private DataBroker createDataBroker() {
127 //        return new BindingDOMDataBrokerAdapter(getDOMDataBroker(), this.mockedSchemaContext.bindingToNormalized);
128 //    }
129 //
130 //    private Map<LogicalDatastoreType, DOMStore> createDatastores() {
131 //        return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
132 //                .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
133 //                .put(LogicalDatastoreType.CONFIGURATION, createConfigurationDatastore()).build();
134 //    }
135 //
136 //    private DOMStore createConfigurationDatastore() {
137 //        final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
138 //        this.mockedSchemaContext.registerSchemaContextListener(store);
139 //        return store;
140 //    }
141 //
142 //    private DOMStore createOperationalDatastore() {
143 //        final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
144 //        this.mockedSchemaContext.registerSchemaContextListener(store);
145 //        return store;
146 //    }
147
148     private final class SchemaContextHolder implements DOMSchemaService, SchemaContextProvider {
149
150         private final SchemaContext schemaContext;
151         private final ListenerRegistry<SchemaContextListener> listeners;
152         private final BindingNormalizedNodeCodecRegistry bindingStreamCodecs;
153         private final BindingToNormalizedNodeCodec bindingToNormalized;
154         private final ModuleInfoBackedContext moduleInfoBackedCntxt;
155
156         private SchemaContextHolder(boolean fromClasspath) {
157             List<YangModuleInfo> moduleInfos = loadModuleInfos();
158             this.moduleInfoBackedCntxt = ModuleInfoBackedContext.create();
159             this.schemaContext = getSchemaContext(moduleInfos);
160             this.listeners = ListenerRegistry.create();
161             this.bindingStreamCodecs = createBindingRegistry();
162             GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
163             this.bindingToNormalized = new BindingToNormalizedNodeCodec(loading, this.bindingStreamCodecs);
164             registerSchemaContextListener(this.bindingToNormalized);
165         }
166
167         @Override
168         public SchemaContext getSchemaContext() {
169             return this.schemaContext;
170         }
171
172         /**
173          * Get the schemacontext from loaded modules on classpath.
174          *
175          * @param moduleInfos a list of Yang module Infos
176          * @return SchemaContext a schema context
177          */
178         private SchemaContext getSchemaContext(List<YangModuleInfo> moduleInfos) {
179             this.moduleInfoBackedCntxt.addModuleInfos(moduleInfos);
180             Optional<SchemaContext> tryToCreateSchemaContext = this.moduleInfoBackedCntxt.tryToCreateSchemaContext();
181             if (!tryToCreateSchemaContext.isPresent()) {
182                 LOG.error("Could not create the initial schema context. Schema context is empty");
183                 throw new IllegalStateException();
184             }
185             return tryToCreateSchemaContext.get();
186         }
187
188         @Override
189         public SchemaContext getGlobalContext() {
190             return this.schemaContext;
191         }
192
193         @Override
194         public SchemaContext getSessionContext() {
195             return this.schemaContext;
196         }
197
198         @Override
199         public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
200                 SchemaContextListener listener) {
201             return this.listeners.register(listener);
202         }
203
204         /**
205          * Loads all {@link YangModelBindingProvider} on the classpath.
206          *
207          * @return list of known {@link YangModuleInfo}
208          */
209         private List<YangModuleInfo> loadModuleInfos() {
210             List<YangModuleInfo> moduleInfos = new LinkedList<>();
211             ServiceLoader<YangModelBindingProvider> yangProviderLoader = ServiceLoader
212                     .load(YangModelBindingProvider.class);
213             for (YangModelBindingProvider yangModelBindingProvider : yangProviderLoader) {
214                 moduleInfos.add(yangModelBindingProvider.getModuleInfo());
215                 LOG.debug("Adding [{}] module into known modules", yangModelBindingProvider.getModuleInfo());
216             }
217             return moduleInfos;
218         }
219
220         /**
221          * Creates binding registry.
222          *
223          * @return BindingNormalizedNodeCodecRegistry the resulting binding registry
224          */
225         private BindingNormalizedNodeCodecRegistry createBindingRegistry() {
226             BindingRuntimeContext bindingContext = BindingRuntimeContext.create(this.moduleInfoBackedCntxt,
227                     this.schemaContext);
228             BindingNormalizedNodeCodecRegistry bindingNormalizedNodeCodecRegistry = new BindingNormalizedNodeCodecRegistry(
229                     StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault())));
230             bindingNormalizedNodeCodecRegistry.onBindingRuntimeContextUpdated(bindingContext);
231             return bindingNormalizedNodeCodecRegistry;
232         }
233
234         /*
235          * (non-Javadoc)
236          *
237          * @see org.opendaylight.mdsal.dom.api.DOMExtensibleService#getExtensions()
238          */
239         @Override
240         public ClassToInstanceMap<DOMSchemaServiceExtension> getExtensions() {
241             // TODO Auto-generated method stub
242             return null;
243         }
244     }
245 }