import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
/**
public static @NonNull EffectiveModelContext createEffectiveModel(
final Iterable<? extends YangModuleInfo> moduleInfos) {
- return createEffectiveModel(ServiceLoaderState.ParserFactory.INSTANCE, moduleInfos);
+ try {
+ return createEffectiveModel(ServiceLoaderState.ParserFactory.INSTANCE, moduleInfos);
+ } catch (YangParserException e) {
+ throw new IllegalStateException("Failed to parse models", e);
+ }
}
public static @NonNull EffectiveModelContext createEffectiveModel(final YangParserFactory parserFactory,
- final Iterable<? extends YangModuleInfo> moduleInfos) {
+ final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
return prepareContext(parserFactory, moduleInfos).getEffectiveModelContext();
}
public static @NonNull BindingRuntimeContext createRuntimeContext() {
- final ModuleInfoSnapshot infos = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE,
- BindingReflections.loadModuleInfos());
+ final ModuleInfoSnapshot infos;
+ try {
+ infos = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE,
+ BindingReflections.loadModuleInfos());
+ } catch (YangParserException e) {
+ throw new IllegalStateException("Failed to parse models", e);
+ }
return new DefaultBindingRuntimeContext(ServiceLoaderState.Generator.INSTANCE.generateTypeMapping(
infos.getEffectiveModelContext()), infos);
}
public static @NonNull BindingRuntimeContext createRuntimeContext(final Class<?>... classes) {
- return createRuntimeContext(ServiceLoaderState.ParserFactory.INSTANCE, ServiceLoaderState.Generator.INSTANCE,
- classes);
+ try {
+ return createRuntimeContext(ServiceLoaderState.ParserFactory.INSTANCE,
+ ServiceLoaderState.Generator.INSTANCE, classes);
+ } catch (YangParserException e) {
+ throw new IllegalStateException("Failed to parse models", e);
+ }
}
public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
- final BindingRuntimeGenerator generator, final Class<?>... classes) {
+ final BindingRuntimeGenerator generator, final Class<?>... classes) throws YangParserException {
return createRuntimeContext(parserFactory, generator, Arrays.asList(classes));
}
public static @NonNull BindingRuntimeContext createRuntimeContext(final YangParserFactory parserFactory,
- final BindingRuntimeGenerator generator, final Collection<Class<?>> classes) {
+ final BindingRuntimeGenerator generator, final Collection<Class<?>> classes) throws YangParserException {
final ModuleInfoSnapshot infos = prepareContext(parserFactory, classes.stream()
.map(BindingRuntimeHelpers::extractYangModuleInfo)
.collect(Collectors.toList()));
}
private static @NonNull ModuleInfoSnapshot prepareContext(final YangParserFactory parserFactory,
- final Iterable<? extends YangModuleInfo> moduleInfos) {
- return new ModuleInfoSnapshotBuilder("helper", parserFactory).add(moduleInfos).build();
+ final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
+ return new ModuleInfoSnapshotBuilder(parserFactory).add(moduleInfos).build();
}
}
*/
package org.opendaylight.mdsal.binding.runtime.spi;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.Beta;
-import java.util.Arrays;
-import java.util.List;
-import java.util.NoSuchElementException;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
+import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.concepts.CheckedBuilder;
import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@Beta
-public final class ModuleInfoSnapshotBuilder implements CheckedBuilder<ModuleInfoSnapshot, NoSuchElementException> {
- private final ModuleInfoSnapshotResolver registry;
+public final class ModuleInfoSnapshotBuilder implements CheckedBuilder<ModuleInfoSnapshot, YangParserException> {
+ private final Set<YangModuleInfo> moduleInfos = new HashSet<>();
+ private final YangParserFactory parserFactory;
- public ModuleInfoSnapshotBuilder(final String name, final YangParserFactory parserFactory) {
- registry = new ModuleInfoSnapshotResolver(name, parserFactory);
+ public ModuleInfoSnapshotBuilder(final YangParserFactory parserFactory) {
+ this.parserFactory = requireNonNull(parserFactory);
}
public @NonNull ModuleInfoSnapshotBuilder add(final YangModuleInfo info) {
- return add(List.of(info));
+ ModuleInfoSnapshotResolver.flatDependencies(moduleInfos, info);
+ return this;
}
public @NonNull ModuleInfoSnapshotBuilder add(final YangModuleInfo... infos) {
- return add(Arrays.asList(infos));
+ for (YangModuleInfo info : infos) {
+ add(info);
+ }
+ return this;
}
public @NonNull ModuleInfoSnapshotBuilder add(final Iterable<? extends YangModuleInfo> infos) {
- registry.registerModuleInfos(infos);
+ for (YangModuleInfo info : infos) {
+ add(info);
+ }
return this;
}
@Override
- public ModuleInfoSnapshot build() {
- return registry.takeSnapshot();
+ public ModuleInfoSnapshot build() throws YangParserException {
+ final YangParser parser = parserFactory.createParser();
+ final Map<SourceIdentifier, YangModuleInfo> mappedInfos = new HashMap<>();
+ final Map<String, ClassLoader> classLoaders = new HashMap<>();
+ for (YangModuleInfo info : moduleInfos) {
+ final YangTextSchemaSource source = ModuleInfoSnapshotResolver.toYangTextSource(info);
+ mappedInfos.put(source.getIdentifier(), info);
+
+ final Class<?> infoClass = info.getClass();
+ classLoaders.put(BindingReflections.getModelRootPackageName(infoClass.getPackage()),
+ infoClass.getClassLoader());
+
+ try {
+ parser.addSource(source);
+ } catch (YangSyntaxErrorException | IOException e) {
+ throw new YangParserException("Failed to add source for " + info, e);
+ }
+ }
+
+ return new DefaultModuleInfoSnapshot(parser.buildEffectiveModel(), mappedInfos, classLoaders);
}
}
}
}
+ static @NonNull YangTextSchemaSource toYangTextSource(final YangModuleInfo moduleInfo) {
+ return YangTextSchemaSource.delegateForByteSource(sourceIdentifierFrom(moduleInfo),
+ moduleInfo.getYangTextByteSource());
+ }
+
private static @NonNull YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier,
final YangModuleInfo moduleInfo) {
return YangTextSchemaSource.delegateForByteSource(identifier, moduleInfo.getYangTextByteSource());
return ImmutableList.copyOf(requiredInfos).reverse();
}
- private static void flatDependencies(final Set<YangModuleInfo> set, final YangModuleInfo moduleInfo) {
+ static void flatDependencies(final Set<YangModuleInfo> set, final YangModuleInfo moduleInfo) {
if (set.add(moduleInfo)) {
for (YangModuleInfo dep : moduleInfo.getImportedModules()) {
flatDependencies(set, dep);
import static com.google.common.base.Verify.verifyNotNull;
import com.google.common.annotations.Beta;
-import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.jdt.annotation.NonNullByDefault;
@Inject
public YangModuleLibrarySupport(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
- final BindingCodecTreeFactory codecFactory) throws YangParserException, IOException {
- final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder("yanglib", parserFactory)
+ final BindingCodecTreeFactory codecFactory) throws YangParserException {
+ final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder(parserFactory)
.add($YangModuleInfoImpl.getInstance())
.build();
context = snapshot.getEffectiveModelContext();
import static com.google.common.base.Verify.verifyNotNull;
import com.google.common.annotations.Beta;
-import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.jdt.annotation.NonNullByDefault;
@Inject
public YangLibrarySupport(final YangParserFactory parserFactory, final BindingRuntimeGenerator generator,
- final BindingCodecTreeFactory codecFactory) throws YangParserException, IOException {
- final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder("yanglib", parserFactory)
+ final BindingCodecTreeFactory codecFactory) throws YangParserException {
+ final ModuleInfoSnapshot snapshot = new ModuleInfoSnapshotBuilder(parserFactory)
.add($YangModuleInfoImpl.getInstance())
.build();
context = snapshot.getEffectiveModelContext();