Search in sources :

Example 1 with SchemaSourceRepresentation

use of org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation 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 SchemaSourceRepresentation

use of org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation in project yangtools by opendaylight.

the class FilesystemSchemaSourceCache method getSource.

@Override
public synchronized FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
    final File file = sourceIdToFile(sourceIdentifier, storageDirectory);
    if (file.exists() && file.canRead()) {
        LOG.trace("Source {} found in cache as {}", sourceIdentifier, file);
        final SchemaSourceRepresentation restored = STORAGE_ADAPTERS.get(representation).restore(sourceIdentifier, file);
        return immediateFluentFuture(representation.cast(restored));
    }
    LOG.debug("Source {} not found in cache as {}", sourceIdentifier, file);
    return immediateFailedFluentFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
}
Also used : MissingSchemaSourceException(org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException) File(java.io.File) SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation)

Example 3 with SchemaSourceRepresentation

use of org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation in project yangtools by opendaylight.

the class AbstractSchemaRepository method getSchemaSource.

@Override
public <T extends SchemaSourceRepresentation> ListenableFuture<T> getSchemaSource(final SourceIdentifier id, final Class<T> representation) {
    final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations;
    synchronized (this) {
        final ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> srcs = sources.get(id);
        if (srcs == null) {
            return immediateFailedFluentFuture(new MissingSchemaSourceException("No providers registered for source " + id, id));
        }
        sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation));
    }
    // TODO, remove and make sources keep sorted multimap (e.g. ArrayListMultimap with SortedLists)
    sortedSchemaSourceRegistrations.sort(SchemaProviderCostComparator.INSTANCE);
    final Iterator<AbstractSchemaSourceRegistration<?>> regs = sortedSchemaSourceRegistrations.iterator();
    if (!regs.hasNext()) {
        return immediateFailedFluentFuture(new MissingSchemaSourceException("No providers for source " + id + " representation " + representation + " available", id));
    }
    final ListenableFuture<T> fetchSourceFuture = fetchSource(id, regs);
    // Add callback to notify cache listeners about encountered schema
    Futures.addCallback(fetchSourceFuture, new FutureCallback<T>() {

        @Override
        public void onSuccess(final T result) {
            for (final SchemaListenerRegistration listener : listeners) {
                listener.getInstance().schemaSourceEncountered(result);
            }
        }

        @Override
        @SuppressWarnings("checkstyle:parameterName")
        public void onFailure(final Throwable t) {
            LOG.trace("Skipping notification for encountered source {}, fetching source failed", id, t);
        }
    }, MoreExecutors.directExecutor());
    return fetchSourceFuture;
}
Also used : SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation) MissingSchemaSourceException(org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException)

Example 4 with SchemaSourceRepresentation

use of org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation 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;
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) Capability(org.opendaylight.netconf.api.capability.Capability) YangModuleCapability(org.opendaylight.netconf.api.capability.YangModuleCapability) BasicCapability(org.opendaylight.netconf.api.capability.BasicCapability) SchemaSourceCache(org.opendaylight.netconf.test.tool.schemacache.SchemaSourceCache) FilesystemSchemaSourceCache(org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) PotentialSchemaSource(org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource) Submodule(org.opendaylight.yangtools.yang.model.api.Submodule) SchemaSourceRepresentation(org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation) SchemaSourceListener(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener) FilesystemSchemaSourceCache(org.opendaylight.yangtools.yang.model.repo.fs.FilesystemSchemaSourceCache) ExecutionException(java.util.concurrent.ExecutionException) Module(org.opendaylight.yangtools.yang.model.api.Module) HashSet(java.util.HashSet)

Example 5 with SchemaSourceRepresentation

use of org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation 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)

Aggregations

SchemaSourceRepresentation (org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation)6 YangTextSchemaSource (org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource)3 PotentialSchemaSource (org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource)3 SchemaSourceListener (org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener)3 File (java.io.File)2 MissingSchemaSourceException (org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException)2 RevisionSourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier)2 SourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier)2 SharedSchemaRepository (org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Before (org.junit.Before)1 Test (org.junit.Test)1 BasicCapability (org.opendaylight.netconf.api.capability.BasicCapability)1 Capability (org.opendaylight.netconf.api.capability.Capability)1 YangModuleCapability (org.opendaylight.netconf.api.capability.YangModuleCapability)1 SchemaSourceCache (org.opendaylight.netconf.test.tool.schemacache.SchemaSourceCache)1 Module (org.opendaylight.yangtools.yang.model.api.Module)1