use of org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository 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.parser.repo.SharedSchemaRepository in project netconf by opendaylight.
the class DefaultSchemaResourceManager method createResources.
@NonNull
private SchemaResourcesDTO createResources(final String subdir) {
// Setup the baseline empty registry
final SharedSchemaRepository repository = new SharedSchemaRepository(subdir, parserFactory);
// Teach the registry how to transform YANG text to IRSchemaSource internally
repository.registerSchemaSourceListener(TextToIRTransformer.create(repository, repository));
// Attach a soft cache of IRSchemaSource instances. This is important during convergence when we are fishing
// for a consistent set of modules, as it skips the need to re-parse the text sources multiple times. It also
// helps establishing different sets of contexts, as they can share this pre-made cache.
repository.registerSchemaSourceListener(// FIXME: add knobs to control cache lifetime explicitly
GuavaSchemaSourceCache.createSoftCache(repository, IRSchemaSource.class));
// Attach the filesystem cache, providing persistence capability, so that restarts do not require us to
// re-populate the cache. This also acts as a side-load capability, as anything pre-populated into that
// directory will not be fetched from the device.
repository.registerSchemaSourceListener(new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(rootDirectory + File.separator + subdir)));
return new SchemaResourcesDTO(repository, repository, repository.createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.getDefault()), new NetconfStateSchemasResolverImpl());
}
use of org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository in project yangtools by opendaylight.
the class FilesystemSchemaSourceCacheIntegrationTest method testWithCacheRunning.
@Test
public void testWithCacheRunning() throws Exception {
final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
final File storageDir = Files.createTempDir();
final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
sharedSchemaRepository.registerSchemaSourceListener(cache);
final SourceIdentifier runningId = RevisionSourceIdentifier.create("running", Revision.of("2012-12-12"));
sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(new YangTextSchemaSource(runningId) {
@Override
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
return toStringHelper;
}
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("running".getBytes(StandardCharsets.UTF_8));
}
@Override
public Optional<String> getSymbolicName() {
return Optional.empty();
}
}), PotentialSchemaSource.create(runningId, YangTextSchemaSource.class, PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
final TextToIRTransformer transformer = TextToIRTransformer.create(sharedSchemaRepository, sharedSchemaRepository);
sharedSchemaRepository.registerSchemaSourceListener(transformer);
// Request schema to make repository notify the cache
final ListenableFuture<EffectiveModelContext> schemaFuture = sharedSchemaRepository.createEffectiveModelContextFactory().createEffectiveModelContext(runningId);
Futures.addCallback(schemaFuture, new FutureCallback<SchemaContext>() {
@Override
public void onSuccess(final SchemaContext result) {
fail("Creation of schema context should fail from non-regular sources");
}
@Override
public void onFailure(final Throwable cause) {
// Creation of schema context fails, since we do not provide regular sources, but we just want
// to check cache
final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
assertEquals(1, cachedSchemas.size());
assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");
}
}, MoreExecutors.directExecutor());
try {
schemaFuture.get();
} catch (final ExecutionException e) {
assertNotNull(e.getCause());
assertEquals(MissingSchemaSourceException.class, e.getCause().getClass());
return;
}
fail("Creation of schema context should fail from non-regular sources");
}
use of org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository 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.parser.repo.SharedSchemaRepository in project netconf by opendaylight.
the class NetconfDeviceSimulator method start.
public List<Integer> start() {
LOG.info("Starting {}, {} simulated devices starting on port {}", configuration.getDeviceCount(), configuration.isSsh() ? "SSH" : "TCP", configuration.getStartingPort());
final SharedSchemaRepository schemaRepo = new SharedSchemaRepository("netconf-simulator");
final Set<Capability> capabilities = parseSchemasToModuleCapabilities(schemaRepo);
final NetconfServerDispatcherImpl dispatcher = createDispatcher(capabilities, sourceIdentifier -> schemaRepo.getSchemaSource(sourceIdentifier, YangTextSchemaSource.class));
int currentPort = configuration.getStartingPort();
final List<Integer> openDevices = new ArrayList<>();
// Generate key to temp folder
final KeyPairProvider keyPairProvider = new VirtualKeyPairProvider();
final AsynchronousChannelGroup group;
try {
group = AsynchronousChannelGroup.withThreadPool(nioExecutor);
} catch (final IOException e) {
throw new IllegalStateException("Failed to create group", e);
}
for (int i = 0; i < configuration.getDeviceCount(); i++) {
if (currentPort > 65535) {
LOG.warn("Port cannot be greater than 65535, stopping further attempts.");
break;
}
final InetSocketAddress address = getAddress(configuration.getIp(), currentPort);
final ChannelFuture server;
if (configuration.isSsh()) {
final InetSocketAddress bindingAddress = InetSocketAddress.createUnresolved("0.0.0.0", currentPort);
final LocalAddress tcpLocalAddress = new LocalAddress(address.toString());
server = dispatcher.createLocalServer(tcpLocalAddress);
try {
final SshProxyServer sshServer = new SshProxyServer(minaTimerExecutor, nettyThreadgroup, group);
sshServer.bind(getSshConfiguration(bindingAddress, tcpLocalAddress, keyPairProvider));
sshWrappers.add(sshServer);
} catch (final BindException e) {
LOG.warn("Cannot start simulated device on {}, port already in use. Skipping.", address);
// Close local server and continue
server.cancel(true);
if (server.isDone()) {
server.channel().close();
}
continue;
} catch (final IOException e) {
LOG.warn("Cannot start simulated device on {} due to IOException.", address, e);
break;
} finally {
currentPort++;
}
try {
server.get();
} catch (final InterruptedException e) {
throw new RuntimeException(e);
} catch (final ExecutionException e) {
LOG.warn("Cannot start ssh simulated device on {}, skipping", address, e);
continue;
}
LOG.debug("Simulated SSH device started on {}", address);
} else {
server = dispatcher.createServer(address);
currentPort++;
try {
server.get();
} catch (final InterruptedException e) {
throw new RuntimeException(e);
} catch (final ExecutionException e) {
LOG.warn("Cannot start tcp simulated device on {}, skipping", address, e);
continue;
}
LOG.debug("Simulated TCP device started on {}", server.channel().localAddress());
}
devicesChannels.add(server.channel());
openDevices.add(currentPort - 1);
}
if (openDevices.size() == configuration.getDeviceCount()) {
LOG.info("All simulated devices started successfully from port {} to {}", configuration.getStartingPort(), currentPort - 1);
} else if (openDevices.size() == 0) {
LOG.warn("No simulated devices started.");
} else {
LOG.warn("Not all simulated devices started successfully. Started devices ar on ports {}", openDevices);
}
return openDevices;
}
Aggregations