use of org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource in project yangtools by opendaylight.
the class SimpleModuleTest method init.
@Before
public void init() {
schemaRegistry = new SharedSchemaRepository("test");
final TextToIRTransformer astTransformer = TextToIRTransformer.create(schemaRegistry, schemaRegistry);
schemaRegistry.registerSchemaSourceListener(astTransformer);
schemaContextFactory = schemaRegistry.createEffectiveModelContextFactory();
allTestSources = new HashSet<>();
final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
@Override
public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
// NOOP
}
@Override
public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
for (final PotentialSchemaSource<?> source : sources) {
allTestSources.add(source.getSourceIdentifier());
}
}
@Override
public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
// NOOP
}
});
reg.close();
}
use of org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource in project netconf by opendaylight.
the class NetconfDeviceSimulator method parseSchemasToModuleCapabilities.
private Set<Capability> parseSchemasToModuleCapabilities(final SharedSchemaRepository consumer) {
final Set<SourceIdentifier> loadedSources = new HashSet<>();
consumer.registerSchemaSourceListener(TextToIRTransformer.create(consumer, consumer));
consumer.registerSchemaSourceListener(new SchemaSourceListener() {
@Override
public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {
}
@Override
public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> potentialSchemaSources) {
for (final PotentialSchemaSource<?> potentialSchemaSource : potentialSchemaSources) {
loadedSources.add(potentialSchemaSource.getSourceIdentifier());
}
}
@Override
public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {
}
});
if (configuration.getSchemasDir() != null) {
LOG.info("Loading models from directory.");
final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(consumer, YangTextSchemaSource.class, configuration.getSchemasDir());
consumer.registerSchemaSourceListener(cache);
} else if (configuration.getModels() != null) {
LOG.info("Loading models from classpath.");
final SchemaSourceCache<YangTextSchemaSource> cache = new SchemaSourceCache<>(consumer, YangTextSchemaSource.class, configuration.getModels());
consumer.registerSchemaSourceListener(cache);
} else {
LOG.info("Custom module loading skipped.");
}
configuration.getDefaultYangResources().forEach(r -> {
final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create(r.getModuleName(), Revision.ofNullable(r.getRevision()));
registerSource(consumer, r.getResourcePath(), sourceIdentifier);
});
try {
// necessary for creating mdsal data stores and operations
this.schemaContext = consumer.createEffectiveModelContextFactory().createEffectiveModelContext(loadedSources).get();
} catch (final InterruptedException | ExecutionException e) {
throw new RuntimeException("Cannot parse schema context. " + "Please read stack trace and check YANG files in schema directory.", e);
}
final Set<Capability> capabilities = new HashSet<>();
for (final Module module : schemaContext.getModules()) {
for (final Submodule subModule : module.getSubmodules()) {
addModuleCapability(consumer, capabilities, subModule);
}
addModuleCapability(consumer, capabilities, module);
}
return capabilities;
}
use of org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource in project yangtools by opendaylight.
the class FilesystemSchemaSourceCacheIntegrationTest method testWithCacheStartup.
@Test
public void testWithCacheStartup() throws Exception {
final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
class CountingSchemaListener implements SchemaSourceListener {
List<PotentialSchemaSource<?>> registeredSources = new ArrayList<>();
@Override
public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
}
@Override
public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
for (final PotentialSchemaSource<?> source : sources) {
registeredSources.add(source);
}
}
@Override
public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
}
}
final File tempDir = Files.createTempDir();
final CountingSchemaListener listener = new CountingSchemaListener();
sharedSchemaRepository.registerSchemaSourceListener(listener);
final File test = new File(tempDir, "test.yang");
Files.asCharSink(test, StandardCharsets.UTF_8).write("content-test");
final File test2 = new File(tempDir, "test@2012-12-12.yang");
Files.asCharSink(test2, StandardCharsets.UTF_8).write("content-test-2012");
final File test3 = new File(tempDir, "test@2013-12-12.yang");
Files.asCharSink(test3, StandardCharsets.UTF_8).write("content-test-2013");
final File test4 = new File(tempDir, "module@2010-12-12.yang");
Files.asCharSink(test4, StandardCharsets.UTF_8).write("content-module-2010");
final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(sharedSchemaRepository, YangTextSchemaSource.class, tempDir);
sharedSchemaRepository.registerSchemaSourceListener(cache);
assertEquals(4, listener.registeredSources.size());
assertThat(Lists.transform(listener.registeredSources, PotentialSchemaSource::getSourceIdentifier), both(hasItem(RevisionSourceIdentifier.create("test", Optional.empty()))).and(hasItem(RevisionSourceIdentifier.create("test", Revision.of("2012-12-12")))).and(hasItem(RevisionSourceIdentifier.create("test", Revision.of("2013-12-12")))).and(hasItem(RevisionSourceIdentifier.create("module", Revision.of("2010-12-12")))));
}
use of org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource in project netconf by opendaylight.
the class YangLibProviderTest method testSchemaSourceRegistered.
@Test
public void testSchemaSourceRegistered() {
yangLibProvider.init();
when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
doNothing().when(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
List<PotentialSchemaSource<?>> list = new ArrayList<>();
list.add(PotentialSchemaSource.create(RevisionSourceIdentifier.create("no-revision"), YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
list.add(PotentialSchemaSource.create(RevisionSourceIdentifier.create("with-revision", org.opendaylight.yangtools.yang.common.Revision.of("2016-04-28")), YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
doReturn(emptyFluentFuture()).when(writeTransaction).commit();
yangLibProvider.schemaSourceRegistered(list);
Map<ModuleKey, Module> newModulesList = new HashMap<>();
Module newModule = new ModuleBuilder().setName(new YangIdentifier("no-revision")).setRevision(RevisionUtils.emptyRevision()).setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/")).build();
newModulesList.put(newModule.key(), newModule);
newModule = new ModuleBuilder().setName(new YangIdentifier("with-revision")).setRevision(new Revision(new RevisionIdentifier("2016-04-28"))).setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28")).build();
newModulesList.put(newModule.key(), newModule);
verify(dataBroker).newWriteOnlyTransaction();
verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), eq(new ModulesStateBuilder().setModule(newModulesList).build()));
verify(writeTransaction).commit();
}
Aggregations