Remove version declaration
[mdsal.git] / binding / mdsal-binding-runtime-spi / src / main / java / org / opendaylight / binding / runtime / spi / ModuleInfoBackedContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.binding.runtime.spi;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import com.google.common.cache.CacheBuilder;
17 import com.google.common.cache.CacheLoader;
18 import com.google.common.cache.LoadingCache;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableList.Builder;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.ListMultimap;
23 import com.google.common.collect.MultimapBuilder;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.LinkedHashSet;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.Set;
32 import org.checkerframework.checker.lock.qual.GuardedBy;
33 import org.checkerframework.checker.lock.qual.Holding;
34 import org.eclipse.jdt.annotation.NonNull;
35 import org.opendaylight.binding.runtime.api.BindingRuntimeContext;
36 import org.opendaylight.binding.runtime.api.BindingRuntimeGenerator;
37 import org.opendaylight.binding.runtime.api.ClassLoadingStrategy;
38 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
39 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
40 import org.opendaylight.yangtools.concepts.ObjectRegistration;
41 import org.opendaylight.yangtools.util.ClassLoaderUtils;
42 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
46 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
47 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
48 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
49 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
50 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
51 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
52 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
53 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaSourceRegistration;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 @Beta
58 public final class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy
59         implements ModuleInfoRegistry, EffectiveModelContextProvider, SchemaSourceProvider<YangTextSchemaSource> {
60     private abstract static class AbstractRegisteredModuleInfo {
61         final YangTextSchemaSourceRegistration reg;
62         final YangModuleInfo info;
63         final ClassLoader loader;
64
65         AbstractRegisteredModuleInfo(final YangModuleInfo info, final YangTextSchemaSourceRegistration reg,
66             final ClassLoader loader) {
67             this.info = requireNonNull(info);
68             this.reg = requireNonNull(reg);
69             this.loader = requireNonNull(loader);
70         }
71
72         @Override
73         public final String toString() {
74             return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
75         }
76
77         ToStringHelper addToStringAttributes(final ToStringHelper helper) {
78             return helper.add("info", info).add("registration", reg).add("classLoader", loader);
79         }
80     }
81
82     private static final class ExplicitRegisteredModuleInfo extends AbstractRegisteredModuleInfo {
83         private int refcount = 1;
84
85         ExplicitRegisteredModuleInfo(final YangModuleInfo info, final YangTextSchemaSourceRegistration reg,
86                 final ClassLoader loader) {
87             super(info, reg, loader);
88         }
89
90         void incRef() {
91             ++refcount;
92         }
93
94         boolean decRef() {
95             return --refcount == 0;
96         }
97
98         @Override
99         ToStringHelper addToStringAttributes(final ToStringHelper helper) {
100             return super.addToStringAttributes(helper).add("refCount", refcount);
101         }
102     }
103
104     private static final class ImplicitRegisteredModuleInfo extends AbstractRegisteredModuleInfo {
105         ImplicitRegisteredModuleInfo(final YangModuleInfo info, final YangTextSchemaSourceRegistration reg,
106                 final ClassLoader loader) {
107             super(info, reg, loader);
108         }
109     }
110
111     private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBackedContext.class);
112
113     private static final LoadingCache<ClassLoadingStrategy,
114         LoadingCache<ImmutableSet<YangModuleInfo>, ModuleInfoBackedContext>> CONTEXT_CACHES = CacheBuilder.newBuilder()
115             .weakKeys().build(new CacheLoader<ClassLoadingStrategy,
116                 LoadingCache<ImmutableSet<YangModuleInfo>, ModuleInfoBackedContext>>() {
117                     @Override
118                     public LoadingCache<ImmutableSet<YangModuleInfo>, ModuleInfoBackedContext> load(
119                             final ClassLoadingStrategy strategy) {
120                         return CacheBuilder.newBuilder().weakValues().build(
121                             new CacheLoader<Set<YangModuleInfo>, ModuleInfoBackedContext>() {
122                                 @Override
123                                 public ModuleInfoBackedContext load(final Set<YangModuleInfo> key) {
124                                     final ModuleInfoBackedContext context = ModuleInfoBackedContext.create(strategy);
125                                     context.addModuleInfos(key);
126                                     return context;
127                                 }
128                             });
129                     }
130             });
131
132     private final YangTextSchemaContextResolver ctxResolver = YangTextSchemaContextResolver.create("binding-context");
133
134     @GuardedBy("this")
135     private final ListMultimap<String, AbstractRegisteredModuleInfo> packageToInfoReg =
136             MultimapBuilder.hashKeys().arrayListValues().build();
137     @GuardedBy("this")
138     private final ListMultimap<SourceIdentifier, AbstractRegisteredModuleInfo> sourceToInfoReg =
139             MultimapBuilder.hashKeys().arrayListValues().build();
140
141     private final ClassLoadingStrategy backingLoadingStrategy;
142
143     private ModuleInfoBackedContext(final ClassLoadingStrategy loadingStrategy) {
144         this.backingLoadingStrategy = loadingStrategy;
145     }
146
147     @Beta
148     public static ModuleInfoBackedContext cacheContext(final ClassLoadingStrategy loadingStrategy,
149             final ImmutableSet<YangModuleInfo> infos) {
150         return CONTEXT_CACHES.getUnchecked(loadingStrategy).getUnchecked(infos);
151     }
152
153     public static ModuleInfoBackedContext create(final ClassLoadingStrategy loadingStrategy) {
154         return new ModuleInfoBackedContext(loadingStrategy);
155     }
156
157     @Override
158     public EffectiveModelContext getEffectiveModelContext() {
159         final Optional<? extends EffectiveModelContext> contextOptional = tryToCreateModelContext();
160         checkState(contextOptional.isPresent(), "Unable to recreate SchemaContext, error while parsing");
161         return contextOptional.get();
162     }
163
164     @Override
165     @SuppressWarnings("checkstyle:illegalCatch")
166     public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
167         // This performs an explicit check for binding classes
168         final String modulePackageName = BindingReflections.getModelRootPackageName(fullyQualifiedName);
169
170         synchronized (this) {
171             // Try to find a loaded class loader
172             // FIXME: two-step process, try explicit registrations first
173             for (AbstractRegisteredModuleInfo reg : packageToInfoReg.get(modulePackageName)) {
174                 return ClassLoaderUtils.loadClass(reg.loader, fullyQualifiedName);
175             }
176
177             // We have not found a matching registration, consult the backing strategy
178             if (backingLoadingStrategy == null) {
179                 throw new ClassNotFoundException(fullyQualifiedName);
180             }
181
182             final Class<?> cls = backingLoadingStrategy.loadClass(fullyQualifiedName);
183             registerImplicitModuleInfo(BindingRuntimeHelpers.extractYangModuleInfo(cls));
184             return cls;
185         }
186     }
187
188     @Override
189     public synchronized ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
190         return register(requireNonNull(yangModuleInfo));
191     }
192
193     @Override
194     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
195         return ctxResolver.getSource(sourceIdentifier);
196     }
197
198     public synchronized void addModuleInfos(final Iterable<? extends YangModuleInfo> moduleInfos) {
199         for (YangModuleInfo yangModuleInfo : moduleInfos) {
200             register(requireNonNull(yangModuleInfo));
201         }
202     }
203
204     @Beta
205     public @NonNull BindingRuntimeContext createRuntimeContext(final BindingRuntimeGenerator generator) {
206         return BindingRuntimeContext.create(generator.generateTypeMapping(tryToCreateModelContext().orElseThrow()),
207             this);
208     }
209
210     // TODO finish schema parsing and expose as SchemaService
211     // Unite with current SchemaService
212
213     public Optional<? extends EffectiveModelContext> tryToCreateModelContext() {
214         return ctxResolver.getEffectiveModelContext();
215     }
216
217     @Holding("this")
218     private ObjectRegistration<YangModuleInfo> register(final @NonNull YangModuleInfo moduleInfo) {
219         final Builder<ExplicitRegisteredModuleInfo> regBuilder = ImmutableList.builder();
220         for (YangModuleInfo info : flatDependencies(moduleInfo)) {
221             regBuilder.add(registerExplicitModuleInfo(info));
222         }
223         final ImmutableList<ExplicitRegisteredModuleInfo> regInfos = regBuilder.build();
224
225         return new AbstractObjectRegistration<>(moduleInfo) {
226             @Override
227             protected void removeRegistration() {
228                 unregister(regInfos);
229             }
230         };
231     }
232
233     /*
234      * Perform implicit registration of a YangModuleInfo and any of its dependencies. If there is a registration for
235      * a particular source, we do not create a duplicate registration.
236      */
237     @Holding("this")
238     private void registerImplicitModuleInfo(final @NonNull YangModuleInfo moduleInfo) {
239         for (YangModuleInfo info : flatDependencies(moduleInfo)) {
240             final Class<?> infoClass = info.getClass();
241             final SourceIdentifier sourceId = sourceIdentifierFrom(info);
242             if (sourceToInfoReg.containsKey(sourceId)) {
243                 LOG.debug("Skipping implicit registration of {} as source {} is already registered", info, sourceId);
244                 continue;
245             }
246
247             final YangTextSchemaSourceRegistration reg;
248             try {
249                 reg = ctxResolver.registerSource(toYangTextSource(sourceId, info));
250             } catch (YangSyntaxErrorException | SchemaSourceException | IOException e) {
251                 LOG.warn("Failed to register info {} source {}, ignoring it", info, sourceId, e);
252                 continue;
253             }
254
255             final ImplicitRegisteredModuleInfo regInfo = new ImplicitRegisteredModuleInfo(info, reg,
256                 infoClass.getClassLoader());
257             sourceToInfoReg.put(sourceId, regInfo);
258             packageToInfoReg.put(BindingReflections.getModelRootPackageName(infoClass.getPackage()), regInfo);
259         }
260     }
261
262     /*
263      * Perform explicit registration of a YangModuleInfo. This always results in a new explicit registration. In case
264      * there is a pre-existing implicit registration, it is removed just after the explicit registration is made.
265      */
266     @Holding("this")
267     private ExplicitRegisteredModuleInfo registerExplicitModuleInfo(final @NonNull YangModuleInfo info) {
268         // First search for an existing explicit registration
269         final SourceIdentifier sourceId = sourceIdentifierFrom(info);
270         for (AbstractRegisteredModuleInfo reg : sourceToInfoReg.get(sourceId)) {
271             if (reg instanceof ExplicitRegisteredModuleInfo && info.equals(reg.info)) {
272                 final ExplicitRegisteredModuleInfo explicit = (ExplicitRegisteredModuleInfo) reg;
273                 explicit.incRef();
274                 LOG.debug("Reusing explicit registration {}", explicit);
275                 return explicit;
276             }
277         }
278
279         // Create an explicit registration
280         final YangTextSchemaSourceRegistration reg;
281         try {
282             reg = ctxResolver.registerSource(toYangTextSource(sourceId, info));
283         } catch (YangSyntaxErrorException | SchemaSourceException | IOException e) {
284             throw new IllegalStateException("Failed to register info " + info, e);
285         }
286
287         final Class<?> infoClass = info.getClass();
288         final String packageName = BindingReflections.getModelRootPackageName(infoClass.getPackage());
289         final ExplicitRegisteredModuleInfo regInfo = new ExplicitRegisteredModuleInfo(info, reg,
290             infoClass.getClassLoader());
291         LOG.debug("Created new explicit registration {}", regInfo);
292
293         sourceToInfoReg.put(sourceId, regInfo);
294         removeImplicit(sourceToInfoReg.get(sourceId));
295         packageToInfoReg.put(packageName, regInfo);
296         removeImplicit(packageToInfoReg.get(packageName));
297
298         return regInfo;
299     }
300
301     synchronized void unregister(final ImmutableList<ExplicitRegisteredModuleInfo> regInfos) {
302         for (ExplicitRegisteredModuleInfo regInfo : regInfos) {
303             if (!regInfo.decRef()) {
304                 LOG.debug("Registration {} has references, not removing it", regInfo);
305                 continue;
306             }
307
308             final SourceIdentifier sourceId = sourceIdentifierFrom(regInfo.info);
309             if (!sourceToInfoReg.remove(sourceId, regInfo)) {
310                 LOG.warn("Failed to find {} registered under {}", regInfo, sourceId);
311             }
312
313             final String packageName = BindingReflections.getModelRootPackageName(regInfo.info.getClass().getPackage());
314             if (!packageToInfoReg.remove(packageName, regInfo)) {
315                 LOG.warn("Failed to find {} registered under {}", regInfo, packageName);
316             }
317
318             regInfo.reg.close();
319         }
320     }
321
322     @Holding("this")
323     private static void removeImplicit(final List<AbstractRegisteredModuleInfo> regs) {
324         /*
325          * Search for implicit registration for a sourceId/packageName.
326          *
327          * Since we are called while an explicit registration is being created (and has already been inserted, we know
328          * there is at least one entry in the maps. We also know registrations retain the order in which they were
329          * created and that implicit registrations are not created if there already is a registration.
330          *
331          * This means that if an implicit registration exists, it will be the first entry in the list.
332          */
333         final AbstractRegisteredModuleInfo reg = regs.get(0);
334         if (reg instanceof ImplicitRegisteredModuleInfo) {
335             LOG.debug("Removing implicit registration {}", reg);
336             regs.remove(0);
337             reg.reg.close();
338         }
339     }
340
341     private static @NonNull YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier,
342             final YangModuleInfo moduleInfo) {
343         return YangTextSchemaSource.delegateForByteSource(identifier, moduleInfo.getYangTextByteSource());
344     }
345
346     private static SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) {
347         final QName name = moduleInfo.getName();
348         return RevisionSourceIdentifier.create(name.getLocalName(), name.getRevision());
349     }
350
351     private static List<YangModuleInfo> flatDependencies(final YangModuleInfo moduleInfo) {
352         // Flatten the modules being registered, with the triggering module being first...
353         final Set<YangModuleInfo> requiredInfos = new LinkedHashSet<>();
354         flatDependencies(requiredInfos, moduleInfo);
355
356         // ... now reverse the order in an effort to register dependencies first (triggering module last)
357         final List<YangModuleInfo> intendedOrder = new ArrayList<>(requiredInfos);
358         Collections.reverse(intendedOrder);
359
360         return intendedOrder;
361     }
362
363     private static void flatDependencies(final Set<YangModuleInfo> set, final YangModuleInfo moduleInfo) {
364         if (set.add(moduleInfo)) {
365             for (YangModuleInfo dep : moduleInfo.getImportedModules()) {
366                 flatDependencies(set, dep);
367             }
368         }
369     }
370 }