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.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.mdsal.binding.generator.api.ClassLoadingStrategy;
21 import org.opendaylight.mdsal.binding.generator.api.ModuleInfoRegistry;
22 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
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 final String modulePackageName = BindingReflections.getModelRootPackageName(fullyQualifiedName);
65 final WeakReference<ClassLoader> classLoaderRef = packageNameToClassLoader.get(modulePackageName);
66 if (classLoaderRef != null) {
67 final ClassLoader classLoader = classLoaderRef.get();
68 if (classLoader != null) {
69 return ClassLoaderUtils.loadClass(classLoader, fullyQualifiedName);
73 if (backingLoadingStrategy == null) {
74 throw new ClassNotFoundException(fullyQualifiedName);
77 final Class<?> cls = backingLoadingStrategy.loadClass(fullyQualifiedName);
78 if (BindingReflections.isBindingClass(cls)) {
79 resolveModuleInfo(cls);
85 // TODO finish schema parsing and expose as SchemaService
86 // Unite with current SchemaService
87 // Implement remove ModuleInfo to update SchemaContext
89 public Optional<SchemaContext> tryToCreateSchemaContext() {
90 return ctxResolver.getSchemaContext();
93 private boolean resolveModuleInfo(final Class<?> cls) {
95 return resolveModuleInfo(BindingReflections.getModuleInfo(cls));
96 } catch (Exception e) {
97 throw new IllegalStateException(String.format("Failed to resolve module information for class %s", cls), e);
101 private boolean resolveModuleInfo(final YangModuleInfo moduleInfo) {
103 SourceIdentifier identifier = sourceIdentifierFrom(moduleInfo);
104 YangModuleInfo previous = sourceIdentifierToModuleInfo.putIfAbsent(identifier, moduleInfo);
105 ClassLoader moduleClassLoader = moduleInfo.getClass().getClassLoader();
107 if (previous == null) {
108 String modulePackageName = moduleInfo.getClass().getPackage().getName();
109 packageNameToClassLoader.putIfAbsent(modulePackageName,
110 new WeakReference<>(moduleClassLoader));
111 ctxResolver.registerSource(toYangTextSource(identifier, moduleInfo));
112 for (YangModuleInfo importedInfo : moduleInfo.getImportedModules()) {
113 resolveModuleInfo(importedInfo);
118 } catch (Exception e) {
119 LOG.error("Not including {} in YANG sources because of error.", moduleInfo, e);
124 private static YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier, final YangModuleInfo moduleInfo) {
125 return new YangTextSchemaSource(identifier) {
128 public InputStream openStream() throws IOException {
129 return moduleInfo.getModuleSourceStream();
133 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
134 return toStringHelper;
139 private static SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) {
140 return RevisionSourceIdentifier.create(moduleInfo.getName(), Optional.of(moduleInfo.getRevision()));
143 public void addModuleInfos(final Iterable<? extends YangModuleInfo> moduleInfos) {
144 for (YangModuleInfo yangModuleInfo : moduleInfos) {
145 registerModuleInfo(yangModuleInfo);
150 public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
151 YangModuleInfoRegistration registration = new YangModuleInfoRegistration(yangModuleInfo, this);
152 resolveModuleInfo(yangModuleInfo);
156 @Override public CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> getSource(
157 final SourceIdentifier sourceIdentifier) {
158 final YangModuleInfo yangModuleInfo = sourceIdentifierToModuleInfo.get(sourceIdentifier);
160 if (yangModuleInfo == null) {
161 LOG.debug("Unknown schema source requested: {}, available sources: {}", sourceIdentifier,
162 sourceIdentifierToModuleInfo.keySet());
163 return Futures.immediateFailedCheckedFuture(new SchemaSourceException(
164 "Unknown schema source: " + sourceIdentifier));
167 return Futures.immediateCheckedFuture(YangTextSchemaSource.delegateForByteSource(sourceIdentifier,
170 public InputStream openStream() throws IOException {
171 return yangModuleInfo.getModuleSourceStream();
176 private static class YangModuleInfoRegistration extends AbstractObjectRegistration<YangModuleInfo> {
178 private final ModuleInfoBackedContext context;
180 public YangModuleInfoRegistration(final YangModuleInfo instance, final ModuleInfoBackedContext context) {
182 this.context = context;
186 protected void removeRegistration() {
187 context.remove(this);
192 private void remove(final YangModuleInfoRegistration registration) {
197 public SchemaContext getSchemaContext() {
198 final Optional<SchemaContext> contextOptional = tryToCreateSchemaContext();
199 if (contextOptional.isPresent()) {
200 return contextOptional.get();
203 throw new IllegalStateException("Unable to recreate SchemaContext, error while parsing");