HoneyNode Java 11 support for 221 devices
[transportpce.git] / tests / honeynode / 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.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;
40
41 import javassist.ClassPool;
42
43 public class DataStoreContextImpl implements DataStoreContext {
44
45     private static final Logger LOG = LoggerFactory.getLogger(DataStoreContextImpl.class);
46
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;
54
55     public DataStoreContextImpl() {
56         this(false);
57     }
58
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);
69 //        }
70     }
71
72 //    @Override
73 //    public DataBroker getDataBroker() {
74 //        return this.dataBroker;
75 //    }
76 //
77 //    @Override
78 //    public DOMDataBroker getDOMDataBroker() {
79 //        return this.domDataBroker;
80 //    }
81 //
82 //    @Override
83 //    public NotificationService createNotificationService() {
84 //        return new BindingDOMNotificationServiceAdapter(this.mockedSchemaContext.bindingStreamCodecs,
85 //                this.domNotificationRouter);
86 //    }
87 //
88 //    @Override
89 //    public NotificationPublishService createNotificationPublishService() {
90 //        return new BindingDOMNotificationPublishServiceAdapter(this.mockedSchemaContext.bindingToNormalized,
91 //                this.domNotificationRouter);
92 //    }
93
94     @Override
95     public SchemaContext getSchemaContext() {
96         return this.mockedSchemaContext.schemaContext;
97     }
98
99     @Override
100     public BindingNormalizedNodeCodecRegistry getBindingToNormalizedNodeCodec() {
101         return this.mockedSchemaContext.bindingStreamCodecs;
102     }
103
104 //    @Override
105 //    public NotificationService getNotificationService() {
106 //        return this.notificationService;
107 //    }
108 //
109 //    @Override
110 //    public NotificationPublishService getNotificationPublishService() {
111 //        return this.notificationPublishService;
112 //    }
113 //
114 //    private DOMDataBroker createDOMDataBroker() {
115 //        return new SerializedDOMDataBroker(this.datastores,
116 //                MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
117 //    }
118 //
119 //    private ListeningExecutorService getDataTreeChangeListenerExecutor() {
120 //        return MoreExecutors.newDirectExecutorService();
121 //    }
122 //
123 //    private DataBroker createDataBroker() {
124 //        return new BindingDOMDataBrokerAdapter(getDOMDataBroker(), this.mockedSchemaContext.bindingToNormalized);
125 //    }
126 //
127 //    private Map<LogicalDatastoreType, DOMStore> createDatastores() {
128 //        return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
129 //                .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
130 //                .put(LogicalDatastoreType.CONFIGURATION, createConfigurationDatastore()).build();
131 //    }
132 //
133 //    private DOMStore createConfigurationDatastore() {
134 //        final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", getDataTreeChangeListenerExecutor());
135 //        this.mockedSchemaContext.registerSchemaContextListener(store);
136 //        return store;
137 //    }
138 //
139 //    private DOMStore createOperationalDatastore() {
140 //        final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", getDataTreeChangeListenerExecutor());
141 //        this.mockedSchemaContext.registerSchemaContextListener(store);
142 //        return store;
143 //    }
144
145     private class SchemaContextHolder implements DOMSchemaService, SchemaContextProvider {
146
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;
152
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);
162         }
163
164         @Override
165         public SchemaContext getSchemaContext() {
166             return this.schemaContext;
167         }
168
169         /**
170          * Get the schemacontext from loaded modules on classpath.
171          *
172          * @param moduleInfos a list of Yang module Infos
173          * @return SchemaContext a schema context
174          */
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();
182             }
183             return tryToCreateSchemaContext.get();
184         }
185
186         @Override
187         public SchemaContext getGlobalContext() {
188             return this.schemaContext;
189         }
190
191         @Override
192         public SchemaContext getSessionContext() {
193             return this.schemaContext;
194         }
195
196         @Override
197         public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
198                 SchemaContextListener listener) {
199             return this.listeners.register(listener);
200         }
201
202         /**
203          * Loads all {@link YangModelBindingProvider} on the classpath.
204          *
205          * @return list of known {@link YangModuleInfo}
206          */
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());
214             }
215             return moduleInfos;
216         }
217
218         /**
219          * Creates binding registry.
220          *
221          * @return BindingNormalizedNodeCodecRegistry the resulting binding registry
222          */
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;
230         }
231     }
232 }