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.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
/**
* Simple helpers to help with reconstruction of BindingRuntimeContext from generated binding classes. These involve
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 Collection<? extends YangModuleInfo> infos) {
+ final ModuleInfoSnapshot snapshot;
+
+ try {
+ snapshot = prepareContext(ServiceLoaderState.ParserFactory.INSTANCE, infos);
+ } catch (YangParserException e) {
+ throw new IllegalStateException("Failed to parse models", e);
+ }
+
+ return new DefaultBindingRuntimeContext(
+ ServiceLoaderState.Generator.INSTANCE.generateTypeMapping(snapshot.getEffectiveModelContext()), snapshot);
}
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()));
}
@SuppressWarnings("checkstyle:IllegalCatch")
- static @NonNull YangModuleInfo extractYangModuleInfo(final Class<?> clazz) {
+ private static @NonNull YangModuleInfo extractYangModuleInfo(final Class<?> clazz) {
try {
return BindingReflections.getModuleInfo(clazz);
} catch (Exception e) {
}
private static @NonNull ModuleInfoSnapshot prepareContext(final YangParserFactory parserFactory,
- final Iterable<? extends YangModuleInfo> moduleInfos) {
- final ModuleInfoSnapshotBuilder ctx = new ModuleInfoSnapshotBuilder("helper", parserFactory);
- ctx.registerModuleInfos(moduleInfos);
- return ctx.build();
+ final Iterable<? extends YangModuleInfo> moduleInfos) throws YangParserException {
+ return new ModuleInfoSnapshotBuilder(parserFactory).add(moduleInfos).build();
}
}