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.mdsal.binding.generator.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.lang.ref.WeakReference;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
17 import org.opendaylight.mdsal.binding.generator.api.ModuleInfoRegistry;
18 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
19 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
20 import org.opendaylight.yangtools.concepts.ObjectRegistration;
21 import org.opendaylight.yangtools.util.ClassLoaderUtils;
22 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
26 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
31 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy
36 implements ModuleInfoRegistry, SchemaContextProvider, SchemaSourceProvider<YangTextSchemaSource> {
38 private final YangTextSchemaContextResolver ctxResolver = YangTextSchemaContextResolver.create("binding-context");
40 private ModuleInfoBackedContext(final ClassLoadingStrategy loadingStrategy) {
41 this.backingLoadingStrategy = loadingStrategy;
44 public static ModuleInfoBackedContext create() {
45 return new ModuleInfoBackedContext(getTCCLClassLoadingStrategy());
48 public static ModuleInfoBackedContext create(final ClassLoadingStrategy loadingStrategy) {
49 return new ModuleInfoBackedContext(loadingStrategy);
52 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBackedContext.class);
54 private final ConcurrentMap<String, WeakReference<ClassLoader>> packageNameToClassLoader =
55 new ConcurrentHashMap<>();
56 private final ConcurrentMap<SourceIdentifier, YangModuleInfo> sourceIdentifierToModuleInfo =
57 new ConcurrentHashMap<>();
59 private final ClassLoadingStrategy backingLoadingStrategy;
62 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
63 final String modulePackageName = BindingReflections.getModelRootPackageName(fullyQualifiedName);
64 final WeakReference<ClassLoader> classLoaderRef = packageNameToClassLoader.get(modulePackageName);
65 if (classLoaderRef != null) {
66 final ClassLoader classLoader = classLoaderRef.get();
67 if (classLoader != null) {
68 return ClassLoaderUtils.loadClass(classLoader, fullyQualifiedName);
72 if (backingLoadingStrategy == null) {
73 throw new ClassNotFoundException(fullyQualifiedName);
76 final Class<?> cls = backingLoadingStrategy.loadClass(fullyQualifiedName);
77 if (BindingReflections.isBindingClass(cls)) {
78 resolveModuleInfo(cls);
84 // TODO finish schema parsing and expose as SchemaService
85 // Unite with current SchemaService
86 // Implement remove ModuleInfo to update SchemaContext
88 public Optional<SchemaContext> tryToCreateSchemaContext() {
89 return Optional.fromJavaUtil(ctxResolver.getSchemaContext());
92 private boolean resolveModuleInfo(final Class<?> cls) {
94 return resolveModuleInfo(BindingReflections.getModuleInfo(cls));
95 } catch (Exception e) {
96 throw new IllegalStateException(String.format("Failed to resolve module information for class %s", cls), e);
100 private boolean resolveModuleInfo(final YangModuleInfo moduleInfo) {
102 SourceIdentifier identifier = sourceIdentifierFrom(moduleInfo);
103 YangModuleInfo previous = sourceIdentifierToModuleInfo.putIfAbsent(identifier, moduleInfo);
104 ClassLoader moduleClassLoader = moduleInfo.getClass().getClassLoader();
106 if (previous == null) {
107 String modulePackageName = moduleInfo.getClass().getPackage().getName();
108 packageNameToClassLoader.putIfAbsent(modulePackageName,
109 new WeakReference<>(moduleClassLoader));
110 ctxResolver.registerSource(toYangTextSource(identifier, moduleInfo));
111 for (YangModuleInfo importedInfo : moduleInfo.getImportedModules()) {
112 resolveModuleInfo(importedInfo);
117 } catch (Exception e) {
118 LOG.error("Not including {} in YANG sources because of error.", moduleInfo, e);
123 private static YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier,
124 final YangModuleInfo moduleInfo) {
125 return YangTextSchemaSource.delegateForByteSource(identifier, moduleInfo.getYangTextByteSource());
128 private static SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) {
129 final QName name = moduleInfo.getName();
130 return RevisionSourceIdentifier.create(name.getLocalName(), name.getRevision());
133 public void addModuleInfos(final Iterable<? extends YangModuleInfo> moduleInfos) {
134 for (YangModuleInfo yangModuleInfo : moduleInfos) {
135 registerModuleInfo(yangModuleInfo);
140 public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
141 YangModuleInfoRegistration registration = new YangModuleInfoRegistration(yangModuleInfo, this);
142 resolveModuleInfo(yangModuleInfo);
147 public ListenableFuture<? extends YangTextSchemaSource> getSource(
148 final SourceIdentifier sourceIdentifier) {
149 final YangModuleInfo yangModuleInfo = sourceIdentifierToModuleInfo.get(sourceIdentifier);
151 if (yangModuleInfo == null) {
152 LOG.debug("Unknown schema source requested: {}, available sources: {}", sourceIdentifier,
153 sourceIdentifierToModuleInfo.keySet());
154 return Futures.immediateFailedFuture(new SchemaSourceException(
155 "Unknown schema source: " + sourceIdentifier));
158 return Futures.immediateFuture(YangTextSchemaSource.delegateForByteSource(sourceIdentifier,
159 yangModuleInfo.getYangTextByteSource()));
162 private static class YangModuleInfoRegistration extends AbstractObjectRegistration<YangModuleInfo> {
164 private final ModuleInfoBackedContext context;
166 public YangModuleInfoRegistration(final YangModuleInfo instance, final ModuleInfoBackedContext context) {
168 this.context = context;
172 protected void removeRegistration() {
173 context.remove(this);
178 private void remove(final YangModuleInfoRegistration registration) {
183 public SchemaContext getSchemaContext() {
184 final Optional<SchemaContext> contextOptional = tryToCreateSchemaContext();
185 if (contextOptional.isPresent()) {
186 return contextOptional.get();
189 throw new IllegalStateException("Unable to recreate SchemaContext, error while parsing");