use of org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException in project yangtools by opendaylight.
the class YangTextSchemaContextResolver method getEffectiveModelContext.
/**
* Try to parse all currently available yang files and build new schema context depending on specified parsing mode.
*
* @param statementParserMode mode of statement parser
* @return new schema context iif there is at least 1 yang file registered and
* new schema context was successfully built.
*/
public Optional<? extends EffectiveModelContext> getEffectiveModelContext(final StatementParserMode statementParserMode) {
final EffectiveModelContextFactory factory = repository.createEffectiveModelContextFactory(config(statementParserMode));
Optional<EffectiveModelContext> sc;
Object ver;
do {
// Spin get stable context version
Object cv;
do {
cv = contextVersion;
sc = currentSchemaContext.get();
if (version == cv) {
return sc;
}
} while (cv != contextVersion);
// Version has been updated
Collection<SourceIdentifier> sources;
do {
ver = version;
sources = ImmutableSet.copyOf(requiredSources);
} while (ver != version);
while (true) {
final ListenableFuture<EffectiveModelContext> f = factory.createEffectiveModelContext(sources);
try {
sc = Optional.of(f.get());
break;
} catch (final InterruptedException e) {
throw new IllegalStateException("Interrupted while assembling schema context", e);
} catch (final ExecutionException e) {
LOG.info("Failed to fully assemble schema context for {}", sources, e);
final Throwable cause = e.getCause();
Verify.verify(cause instanceof SchemaResolutionException);
sources = ((SchemaResolutionException) cause).getResolvedSources();
}
}
LOG.debug("Resolved schema context for {}", sources);
synchronized (this) {
if (contextVersion == cv) {
currentSchemaContext.set(sc);
contextVersion = ver;
}
}
} while (version == ver);
return sc;
}
use of org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException in project netconf by opendaylight.
the class NetconfDeviceTest method testNetconfDeviceMissingSource.
@Test
public void testNetconfDeviceMissingSource() throws Exception {
final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
final NetconfDeviceCommunicator listener = getListener();
final EffectiveModelContextFactory schemaFactory = getSchemaFactory();
final SchemaRepository schemaRepository = getSchemaRepository();
// Make fallback attempt to fail due to empty resolved sources
final MissingSchemaSourceException schemaResolutionException = new MissingSchemaSourceException("fail first", TEST_SID);
doReturn(Futures.immediateFailedFuture(schemaResolutionException)).when(schemaRepository).getSchemaSource(eq(TEST_SID), eq(YangTextSchemaSource.class));
doAnswer(invocation -> {
if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
return Futures.immediateFailedFuture(schemaResolutionException);
} else {
return Futures.immediateFuture(SCHEMA_CONTEXT);
}
}).when(schemaFactory).createEffectiveModelContext(anyCollection());
final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id, schemaContext) -> {
final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
final QName qName = QName.create(first.getQNameModule(), first.getName());
final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
final NetconfStateSchemas.RemoteYangSchema source2 = new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
};
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
final NetconfDevice device = new NetconfDeviceBuilder().setReconnectOnSchemasChange(true).setSchemaResourcesDTO(schemaResourcesDTO).setGlobalProcessingExecutor(getExecutor()).setBaseSchemas(BASE_SCHEMAS).setId(getId()).setSalFacade(facade).build();
// Monitoring supported
final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
device.onRemoteSessionUp(sessionCaps, listener);
verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
verify(schemaFactory, times(1)).createEffectiveModelContext(anyCollection());
}
use of org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException in project yangtools by opendaylight.
the class AssembleSources method apply.
@Override
public FluentFuture<EffectiveModelContext> apply(final List<IRSchemaSource> sources) throws SchemaResolutionException, ReactorException {
final Map<SourceIdentifier, IRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
final Map<SourceIdentifier, YangModelDependencyInfo> deps = Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
LOG.debug("Resolving dependency reactor {}", deps);
final StatementParserMode statementParserMode = config.getStatementParserMode();
final DependencyResolver res = statementParserMode == StatementParserMode.SEMVER_MODE ? SemVerDependencyResolver.create(deps) : RevisionDependencyResolver.create(deps);
if (!res.getUnresolvedSources().isEmpty()) {
LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
throw new SchemaResolutionException("Failed to resolve required models", res.getResolvedSources(), res.getUnsatisfiedImports());
}
final YangParser parser = parserFactory.createParser(res.parserConfig());
config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
try {
parser.addSource(entry.getValue());
} catch (YangSyntaxErrorException | IOException e) {
throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
}
}
final EffectiveModelContext schemaContext;
try {
schemaContext = parser.buildEffectiveModel();
} catch (final YangParserException e) {
throw new SchemaResolutionException("Failed to resolve required models", e);
}
return immediateFluentFuture(schemaContext);
}
use of org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException in project netconf by opendaylight.
the class NetconfDeviceTest method testNetconfDeviceFailFirstSchemaFailSecondEmpty.
@Test
public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
final NetconfDeviceCommunicator listener = getListener();
final EffectiveModelContextFactory schemaFactory = getSchemaFactory();
final SchemaRepository schemaRepository = getSchemaRepository();
// Make fallback attempt to fail due to empty resolved sources
final SchemaResolutionException schemaResolutionException = new SchemaResolutionException("fail first", Collections.emptyList(), HashMultimap.create());
doReturn(Futures.immediateFailedFuture(schemaResolutionException)).when(schemaFactory).createEffectiveModelContext(anyCollection());
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder().setReconnectOnSchemasChange(true).setSchemaResourcesDTO(schemaResourcesDTO).setGlobalProcessingExecutor(getExecutor()).setId(getId()).setSalFacade(facade).setBaseSchemas(BASE_SCHEMAS).build();
// Monitoring not supported
final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
device.onRemoteSessionUp(sessionCaps, listener);
verify(facade, timeout(5000)).onDeviceDisconnected();
verify(listener, timeout(5000)).close();
verify(schemaFactory, times(1)).createEffectiveModelContext(anyCollection());
}
use of org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException in project netconf by opendaylight.
the class NetconfDeviceTest method testNetconfDeviceFlawedModelFailedResolution.
@Test
public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
final NetconfDeviceCommunicator listener = getListener();
final EffectiveModelContextFactory schemaFactory = getSchemaFactory();
final SchemaRepository schemaRepository = getSchemaRepository();
final SchemaResolutionException schemaResolutionException = new SchemaResolutionException("fail first", TEST_SID, new Throwable("YangTools parser fail"));
doAnswer(invocation -> {
if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
return Futures.immediateFailedFuture(schemaResolutionException);
} else {
return Futures.immediateFuture(SCHEMA_CONTEXT);
}
}).when(schemaFactory).createEffectiveModelContext(anyCollection());
final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id, schemaContext) -> {
final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
final QName qName = QName.create(first.getQNameModule(), first.getName());
final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
final NetconfStateSchemas.RemoteYangSchema source2 = new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
};
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
final NetconfDevice device = new NetconfDeviceBuilder().setReconnectOnSchemasChange(true).setSchemaResourcesDTO(schemaResourcesDTO).setGlobalProcessingExecutor(getExecutor()).setId(getId()).setSalFacade(facade).setBaseSchemas(BASE_SCHEMAS).build();
// Monitoring supported
final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
device.onRemoteSessionUp(sessionCaps, listener);
verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
verify(schemaFactory, times(2)).createEffectiveModelContext(anyCollection());
}
Aggregations