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.yangtools.sal.binding.generator.impl;
10 import com.google.common.base.MoreObjects.ToStringHelper;
11 import com.google.common.base.Optional;
12 import com.google.common.io.ByteSource;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.lang.ref.WeakReference;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
21 import org.opendaylight.yangtools.concepts.ObjectRegistration;
22 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
23 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
24 import org.opendaylight.yangtools.util.ClassLoaderUtils;
25 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
26 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
29 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
34 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy
39 implements ModuleInfoRegistry, SchemaContextProvider, SchemaSourceProvider<YangTextSchemaSource> {
41 private final YangTextSchemaContextResolver ctxResolver = YangTextSchemaContextResolver.create("binding-context");
43 private ModuleInfoBackedContext(final ClassLoadingStrategy loadingStrategy) {
44 this.backingLoadingStrategy = loadingStrategy;
47 public static ModuleInfoBackedContext create() {
48 return new ModuleInfoBackedContext(getTCCLClassLoadingStrategy());
51 public static ModuleInfoBackedContext create(final ClassLoadingStrategy loadingStrategy) {
52 return new ModuleInfoBackedContext(loadingStrategy);
55 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBackedContext.class);
57 private final ConcurrentMap<String, WeakReference<ClassLoader>> packageNameToClassLoader = new ConcurrentHashMap<>();
58 private final ConcurrentMap<SourceIdentifier, YangModuleInfo> sourceIdentifierToModuleInfo = new ConcurrentHashMap<>();
60 private final ClassLoadingStrategy backingLoadingStrategy;
63 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
64 String modulePackageName = BindingReflections.getModelRootPackageName(fullyQualifiedName);
66 WeakReference<ClassLoader> classLoaderRef = packageNameToClassLoader.get(modulePackageName);
67 ClassLoader classloader;
68 if (classLoaderRef != null && (classloader = classLoaderRef.get()) != null) {
69 return ClassLoaderUtils.loadClass(classloader, fullyQualifiedName);
71 if (backingLoadingStrategy == null) {
72 throw new ClassNotFoundException(fullyQualifiedName);
74 Class<?> cls = backingLoadingStrategy.loadClass(fullyQualifiedName);
75 if (BindingReflections.isBindingClass(cls)) {
76 resolveModuleInfo(cls);
82 // TODO finish schema parsing and expose as SchemaService
83 // Unite with current SchemaService
84 // Implement remove ModuleInfo to update SchemaContext
86 public Optional<SchemaContext> tryToCreateSchemaContext() {
87 return ctxResolver.getSchemaContext();
90 private boolean resolveModuleInfo(final Class<?> cls) {
92 return resolveModuleInfo(BindingReflections.getModuleInfo(cls));
93 } catch (Exception e) {
94 throw new IllegalStateException(String.format("Failed to resolve module information for class %s", cls), e);
98 private boolean resolveModuleInfo(final YangModuleInfo moduleInfo) {
100 SourceIdentifier identifier = sourceIdentifierFrom(moduleInfo);
101 YangModuleInfo previous = sourceIdentifierToModuleInfo.putIfAbsent(identifier, moduleInfo);
102 ClassLoader moduleClassLoader = moduleInfo.getClass().getClassLoader();
104 if (previous == null) {
105 String modulePackageName = moduleInfo.getClass().getPackage().getName();
106 packageNameToClassLoader.putIfAbsent(modulePackageName,
107 new WeakReference<>(moduleClassLoader));
108 ctxResolver.registerSource(toYangTextSource(identifier, moduleInfo));
109 for (YangModuleInfo importedInfo : moduleInfo.getImportedModules()) {
110 resolveModuleInfo(importedInfo);
115 } catch (Exception e) {
116 LOG.error("Not including {} in YANG sources because of error.", moduleInfo, e);
121 private static YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier, final YangModuleInfo moduleInfo) {
122 return new YangTextSchemaSource(identifier) {
125 public InputStream openStream() throws IOException {
126 return moduleInfo.getModuleSourceStream();
130 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
131 return toStringHelper;
136 private static SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) {
137 return RevisionSourceIdentifier.create(moduleInfo.getName(), Optional.of(moduleInfo.getRevision()));
140 public void addModuleInfos(final Iterable<? extends YangModuleInfo> moduleInfos) {
141 for (YangModuleInfo yangModuleInfo : moduleInfos) {
142 registerModuleInfo(yangModuleInfo);
147 public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
148 YangModuleInfoRegistration registration = new YangModuleInfoRegistration(yangModuleInfo, this);
149 resolveModuleInfo(yangModuleInfo);
153 @Override public CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> getSource(
154 final SourceIdentifier sourceIdentifier) {
155 final YangModuleInfo yangModuleInfo = sourceIdentifierToModuleInfo.get(sourceIdentifier);
157 if (yangModuleInfo == null) {
158 LOG.debug("Unknown schema source requested: {}, available sources: {}", sourceIdentifier, sourceIdentifierToModuleInfo.keySet());
160 .immediateFailedCheckedFuture(new SchemaSourceException("Unknown schema source: " + sourceIdentifier));
164 .immediateCheckedFuture(YangTextSchemaSource.delegateForByteSource(sourceIdentifier, new ByteSource() {
165 @Override public InputStream openStream() throws IOException {
166 return yangModuleInfo.getModuleSourceStream();
171 private static class YangModuleInfoRegistration extends AbstractObjectRegistration<YangModuleInfo> {
173 private final ModuleInfoBackedContext context;
175 public YangModuleInfoRegistration(final YangModuleInfo instance, final ModuleInfoBackedContext context) {
177 this.context = context;
181 protected void removeRegistration() {
182 context.remove(this);
187 private void remove(final YangModuleInfoRegistration registration) {
192 public SchemaContext getSchemaContext() {
193 final Optional<SchemaContext> contextOptional = tryToCreateSchemaContext();
194 if (contextOptional.isPresent()) {
195 return contextOptional.get();
198 throw new IllegalStateException("Unable to recreate SchemaContext, error while parsing");