Search in sources :

Example 1 with SharedSchemaRepository

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();
}
Also used : SchemaSourceListener(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener) SchemaListenerRegistration(org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration) PotentialSchemaSource(org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource) SharedSchemaRepository(org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository) TextToIRTransformer(org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer) SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation) Before(org.junit.Before)

Example 2 with SharedSchemaRepository

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());
}
Also used : IRSchemaSource(org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) NetconfStateSchemasResolverImpl(org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl) File(java.io.File) SharedSchemaRepository(org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository) SchemaResourcesDTO(org.opendaylight.netconf.sal.connect.netconf.NetconfDevice.SchemaResourcesDTO) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 3 with SharedSchemaRepository

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");
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) ToStringHelper(com.google.common.base.MoreObjects.ToStringHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) MissingSchemaSourceException(org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) SharedSchemaRepository(org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository) TextToIRTransformer(org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 4 with SharedSchemaRepository

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")))));
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) PotentialSchemaSource(org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource) SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation) SchemaSourceListener(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) SharedSchemaRepository(org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository) Test(org.junit.Test)

Example 5 with SharedSchemaRepository

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;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) Capability(org.opendaylight.netconf.api.capability.Capability) YangModuleCapability(org.opendaylight.netconf.api.capability.YangModuleCapability) BasicCapability(org.opendaylight.netconf.api.capability.BasicCapability) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) SshProxyServer(org.opendaylight.netconf.ssh.SshProxyServer) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) BindException(java.net.BindException) IOException(java.io.IOException) NetconfServerDispatcherImpl(org.opendaylight.netconf.impl.NetconfServerDispatcherImpl) KeyPairProvider(org.opendaylight.netconf.shaded.sshd.common.keyprovider.KeyPairProvider) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) ExecutionException(java.util.concurrent.ExecutionException) SharedSchemaRepository(org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository)

Aggregations

SharedSchemaRepository (org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository)7 YangTextSchemaSource (org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource)5 Test (org.junit.Test)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 RevisionSourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier)3 SourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier)3 PotentialSchemaSource (org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource)3 TextToIRTransformer (org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer)3 Props (akka.actor.Props)2 TestActorRef (akka.testkit.TestActorRef)2 InetSocketAddress (java.net.InetSocketAddress)2 NetconfTopologySetup (org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup)2 ActorRef (akka.actor.ActorRef)1 ActorSystem (akka.actor.ActorSystem)1 Cluster (akka.cluster.Cluster)1 Dispatchers (akka.dispatch.Dispatchers)1 TestKit (akka.testkit.javadsl.TestKit)1