2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.binding.runtime.spi;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
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;
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;
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;
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);
73 public final String toString() {
74 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
77 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
78 return helper.add("info", info).add("registration", reg).add("classLoader", loader);
82 private static final class ExplicitRegisteredModuleInfo extends AbstractRegisteredModuleInfo {
83 private int refcount = 1;
85 ExplicitRegisteredModuleInfo(final YangModuleInfo info, final YangTextSchemaSourceRegistration reg,
86 final ClassLoader loader) {
87 super(info, reg, loader);
95 return --refcount == 0;
99 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
100 return super.addToStringAttributes(helper).add("refCount", refcount);
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);
111 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBackedContext.class);
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>>() {
118 public LoadingCache<ImmutableSet<YangModuleInfo>, ModuleInfoBackedContext> load(
119 final ClassLoadingStrategy strategy) {
120 return CacheBuilder.newBuilder().weakValues().build(
121 new CacheLoader<Set<YangModuleInfo>, ModuleInfoBackedContext>() {
123 public ModuleInfoBackedContext load(final Set<YangModuleInfo> key) {
124 final ModuleInfoBackedContext context = ModuleInfoBackedContext.create(strategy);
125 context.addModuleInfos(key);
132 private final YangTextSchemaContextResolver ctxResolver = YangTextSchemaContextResolver.create("binding-context");
135 private final ListMultimap<String, AbstractRegisteredModuleInfo> packageToInfoReg =
136 MultimapBuilder.hashKeys().arrayListValues().build();
138 private final ListMultimap<SourceIdentifier, AbstractRegisteredModuleInfo> sourceToInfoReg =
139 MultimapBuilder.hashKeys().arrayListValues().build();
141 private final ClassLoadingStrategy backingLoadingStrategy;
143 private ModuleInfoBackedContext(final ClassLoadingStrategy loadingStrategy) {
144 this.backingLoadingStrategy = loadingStrategy;
148 public static ModuleInfoBackedContext cacheContext(final ClassLoadingStrategy loadingStrategy,
149 final ImmutableSet<YangModuleInfo> infos) {
150 return CONTEXT_CACHES.getUnchecked(loadingStrategy).getUnchecked(infos);
153 public static ModuleInfoBackedContext create(final ClassLoadingStrategy loadingStrategy) {
154 return new ModuleInfoBackedContext(loadingStrategy);
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();
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);
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);
177 // We have not found a matching registration, consult the backing strategy
178 if (backingLoadingStrategy == null) {
179 throw new ClassNotFoundException(fullyQualifiedName);
182 final Class<?> cls = backingLoadingStrategy.loadClass(fullyQualifiedName);
183 registerImplicitModuleInfo(BindingRuntimeHelpers.extractYangModuleInfo(cls));
189 public synchronized ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
190 return register(requireNonNull(yangModuleInfo));
194 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
195 return ctxResolver.getSource(sourceIdentifier);
198 public synchronized void addModuleInfos(final Iterable<? extends YangModuleInfo> moduleInfos) {
199 for (YangModuleInfo yangModuleInfo : moduleInfos) {
200 register(requireNonNull(yangModuleInfo));
205 public @NonNull BindingRuntimeContext createRuntimeContext(final BindingRuntimeGenerator generator) {
206 return BindingRuntimeContext.create(generator.generateTypeMapping(tryToCreateModelContext().orElseThrow()),
210 // TODO finish schema parsing and expose as SchemaService
211 // Unite with current SchemaService
213 public Optional<? extends EffectiveModelContext> tryToCreateModelContext() {
214 return ctxResolver.getEffectiveModelContext();
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));
223 final ImmutableList<ExplicitRegisteredModuleInfo> regInfos = regBuilder.build();
225 return new AbstractObjectRegistration<>(moduleInfo) {
227 protected void removeRegistration() {
228 unregister(regInfos);
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.
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);
247 final YangTextSchemaSourceRegistration reg;
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);
255 final ImplicitRegisteredModuleInfo regInfo = new ImplicitRegisteredModuleInfo(info, reg,
256 infoClass.getClassLoader());
257 sourceToInfoReg.put(sourceId, regInfo);
258 packageToInfoReg.put(BindingReflections.getModelRootPackageName(infoClass.getPackage()), regInfo);
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.
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;
274 LOG.debug("Reusing explicit registration {}", explicit);
279 // Create an explicit registration
280 final YangTextSchemaSourceRegistration reg;
282 reg = ctxResolver.registerSource(toYangTextSource(sourceId, info));
283 } catch (YangSyntaxErrorException | SchemaSourceException | IOException e) {
284 throw new IllegalStateException("Failed to register info " + info, e);
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);
293 sourceToInfoReg.put(sourceId, regInfo);
294 removeImplicit(sourceToInfoReg.get(sourceId));
295 packageToInfoReg.put(packageName, regInfo);
296 removeImplicit(packageToInfoReg.get(packageName));
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);
308 final SourceIdentifier sourceId = sourceIdentifierFrom(regInfo.info);
309 if (!sourceToInfoReg.remove(sourceId, regInfo)) {
310 LOG.warn("Failed to find {} registered under {}", regInfo, sourceId);
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);
323 private static void removeImplicit(final List<AbstractRegisteredModuleInfo> regs) {
325 * Search for implicit registration for a sourceId/packageName.
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.
331 * This means that if an implicit registration exists, it will be the first entry in the list.
333 final AbstractRegisteredModuleInfo reg = regs.get(0);
334 if (reg instanceof ImplicitRegisteredModuleInfo) {
335 LOG.debug("Removing implicit registration {}", reg);
341 private static @NonNull YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier,
342 final YangModuleInfo moduleInfo) {
343 return YangTextSchemaSource.delegateForByteSource(identifier, moduleInfo.getYangTextByteSource());
346 private static SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) {
347 final QName name = moduleInfo.getName();
348 return RevisionSourceIdentifier.create(name.getLocalName(), name.getRevision());
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);
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);
360 return intendedOrder;
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);