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.Optional;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
14 import org.opendaylight.yangtools.concepts.ObjectRegistration;
15 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
16 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
17 import org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils;
18 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
19 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
23 import org.opendaylight.yangtools.yang.model.util.repo.AdvancedSchemaSourceProvider;
24 import org.opendaylight.yangtools.yang.model.util.repo.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.lang.ref.WeakReference;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
36 public class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy //
38 AdvancedSchemaSourceProvider<InputStream>, ModuleInfoRegistry, SchemaContextProvider {
40 private ModuleInfoBackedContext(ClassLoadingStrategy loadingStrategy) {
41 this.backingLoadingStrategy = loadingStrategy;
44 public static ModuleInfoBackedContext create() {
45 return new ModuleInfoBackedContext(getTCCLClassLoadingStrategy());
48 public static ModuleInfoBackedContext create(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 = new ConcurrentHashMap<>();
55 private final ConcurrentMap<SourceIdentifier, YangModuleInfo> sourceIdentifierToModuleInfo = new ConcurrentHashMap<>();
57 private final ClassLoadingStrategy backingLoadingStrategy;
60 public Class<?> loadClass(String fullyQualifiedName) throws ClassNotFoundException {
61 String modulePackageName = BindingReflections.getModelRootPackageName(fullyQualifiedName);
63 WeakReference<ClassLoader> classLoaderRef = packageNameToClassLoader.get(modulePackageName);
64 ClassLoader classloader = null;
65 if (classLoaderRef != null && (classloader = classLoaderRef.get()) != null) {
66 return ClassLoaderUtils.loadClass(classloader, fullyQualifiedName);
68 if (backingLoadingStrategy == null) {
69 throw new ClassNotFoundException(fullyQualifiedName);
71 Class<?> cls = backingLoadingStrategy.loadClass(fullyQualifiedName);
72 if (BindingReflections.isBindingClass(cls)) {
73 boolean newModule = resolveModuleInfo(cls);
75 recreateSchemaContext();
82 private synchronized Optional<SchemaContext> recreateSchemaContext() {
84 ImmutableList<InputStream> streams = getAvailableStreams();
85 YangParserImpl parser = new YangParserImpl();
86 Set<Module> modules = parser.parseYangModelsFromStreams(streams);
87 SchemaContext schemaContext = parser.resolveSchemaContext(modules);
88 return Optional.of(schemaContext);
89 } catch (IOException e) {
90 LOG.error("Schema was not recreated.",e);
92 return Optional.absent();
95 // TODO finish schema parsing and expose as SchemaService
96 // Unite with current SchemaService
97 // Implement remove ModuleInfo to update SchemaContext
99 public synchronized Optional<SchemaContext> tryToCreateSchemaContext() {
100 return recreateSchemaContext();
103 private ImmutableList<InputStream> getAvailableStreams() throws IOException {
104 ImmutableSet<YangModuleInfo> moduleInfos = ImmutableSet.copyOf(sourceIdentifierToModuleInfo.values());
106 ImmutableList.Builder<InputStream> sourceStreams = ImmutableList.<InputStream> builder();
107 for (YangModuleInfo moduleInfo : moduleInfos) {
108 sourceStreams.add(moduleInfo.getModuleSourceStream());
110 return sourceStreams.build();
113 private boolean resolveModuleInfo(Class<?> cls) {
115 return resolveModuleInfo(BindingReflections.getModuleInfo(cls));
116 } catch (Exception e) {
117 throw new IllegalStateException(e);
121 private boolean resolveModuleInfo(YangModuleInfo moduleInfo) {
123 SourceIdentifier identifier = sourceIdentifierFrom(moduleInfo);
124 YangModuleInfo previous = sourceIdentifierToModuleInfo.putIfAbsent(identifier, moduleInfo);
125 ClassLoader moduleClassLoader = moduleInfo.getClass().getClassLoader();
126 if (previous == null) {
127 String modulePackageName = moduleInfo.getClass().getPackage().getName();
128 packageNameToClassLoader.putIfAbsent(modulePackageName, new WeakReference<ClassLoader>(moduleClassLoader));
130 for (YangModuleInfo importedInfo : moduleInfo.getImportedModules()) {
131 resolveModuleInfo(importedInfo);
139 private SourceIdentifier sourceIdentifierFrom(YangModuleInfo moduleInfo) {
140 return SourceIdentifier.create(moduleInfo.getName(), Optional.of(moduleInfo.getRevision()));
143 public void addModuleInfos(Iterable<? extends YangModuleInfo> moduleInfos) {
144 for (YangModuleInfo yangModuleInfo : moduleInfos) {
145 registerModuleInfo(yangModuleInfo);
150 public ObjectRegistration<YangModuleInfo> registerModuleInfo(YangModuleInfo yangModuleInfo) {
151 YangModuleInfoRegistration registration = new YangModuleInfoRegistration(yangModuleInfo, this);
153 resolveModuleInfo(yangModuleInfo);
159 public Optional<InputStream> getSchemaSource(SourceIdentifier sourceIdentifier) {
160 YangModuleInfo info = sourceIdentifierToModuleInfo.get(sourceIdentifier);
162 return Optional.absent();
165 return Optional.of(info.getModuleSourceStream());
166 } catch (IOException e) {
167 return Optional.absent();
172 public Optional<InputStream> getSchemaSource(String moduleName, Optional<String> revision) {
173 return getSchemaSource(SourceIdentifier.create(moduleName, revision));
176 private static class YangModuleInfoRegistration extends AbstractObjectRegistration<YangModuleInfo> {
178 private final ModuleInfoBackedContext context;
180 public YangModuleInfoRegistration(YangModuleInfo instance, ModuleInfoBackedContext context) {
182 this.context = context;
186 protected void removeRegistration() {
187 context.remove(this);
192 private void remove(YangModuleInfoRegistration registration) {
197 public SchemaContext getSchemaContext() {
198 return tryToCreateSchemaContext().get();