Search in sources :

Example 6 with XMLNamespace

use of org.opendaylight.yangtools.yang.common.XMLNamespace in project yangtools by opendaylight.

the class RandomPrefixTest method testQNameWithPrefix.

@Test
public void testQNameWithPrefix() {
    final RandomPrefix a = new RandomPrefix(null);
    final List<String> allGenerated = new ArrayList<>();
    for (int i = 0; i < MAX_COUNTER; i++) {
        final String prefix = RandomPrefix.encode(i);
        final XMLNamespace uri = XMLNamespace.of("localhost:" + prefix);
        final QName qname = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
        allGenerated.add(a.encodePrefix(qname.getNamespace()));
    }
    assertEquals(MAX_COUNTER, allGenerated.size());
    // We are generating MAX_COUNTER_VALUE + 27 prefixes total, so we should encounter a reset in prefix a start
    // from 0 at some point. At the end, there should be only 27 values in RandomPrefix cache
    assertEquals(MAX_COUNTER, Iterables.size(a.getPrefixes()));
    assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xml")));
    assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmla")));
    assertThat(allGenerated, CoreMatchers.not(CoreMatchers.hasItem("xmlz")));
    assertEquals(1, Iterables.frequency(allGenerated, "a"));
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) ArrayList(java.util.ArrayList) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Test(org.junit.Test)

Example 7 with XMLNamespace

use of org.opendaylight.yangtools.yang.common.XMLNamespace in project yangtools by opendaylight.

the class ModuleDependencySort method processDependencies.

/**
 * Extract module:revision from modules.
 */
private static void processDependencies(final Table<String, Optional<Revision>, ModuleNodeImpl> moduleGraph, final Collection<? extends Module> mmbs) {
    final Map<XMLNamespace, Module> allNS = new HashMap<>();
    // Create edges in graph
    for (final Module module : mmbs) {
        final Map<String, Optional<Revision>> imported = new HashMap<>();
        final String fromName = module.getName();
        final XMLNamespace ns = module.getNamespace();
        final Optional<Revision> fromRevision = module.getRevision();
        // check for existence of module with same namespace
        final Module prev = allNS.putIfAbsent(ns, module);
        if (prev != null) {
            final String name = prev.getName();
            if (!fromName.equals(name)) {
                LOG.warn("Error while sorting module [{}, {}]: module with same namespace ({}) already loaded:" + " [{}, {}]", fromName, fromRevision, ns, name, prev.getRevision());
            }
        }
        // no need to check if other Type of object, check is performed in process modules
        for (final ModuleImport imprt : allImports(module)) {
            final String toName = imprt.getModuleName();
            final Optional<Revision> toRevision = imprt.getRevision();
            final ModuleNodeImpl from = moduleGraph.get(fromName, fromRevision);
            final ModuleNodeImpl to = getModuleByNameAndRevision(moduleGraph, fromName, fromRevision, toName, toRevision);
            /*
                 * If it is an yang 1 module, check imports: If module is imported twice with different
                 * revisions then throw exception
                 */
            if (module.getYangVersion() == YangVersion.VERSION_1) {
                final Optional<Revision> impRevision = imported.get(toName);
                if (impRevision != null && impRevision.isPresent() && !impRevision.equals(toRevision) && toRevision.isPresent()) {
                    throw new IllegalArgumentException(String.format("Module:%s imported twice with different revisions:%s, %s", toName, formatRevDate(impRevision), formatRevDate(toRevision)));
                }
            }
            imported.put(toName, toRevision);
            from.addEdge(to);
        }
    }
}
Also used : Optional(java.util.Optional) Revision(org.opendaylight.yangtools.yang.common.Revision) HashMap(java.util.HashMap) ModuleImport(org.opendaylight.yangtools.yang.model.api.ModuleImport) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Module(org.opendaylight.yangtools.yang.model.api.Module)

Example 8 with XMLNamespace

use of org.opendaylight.yangtools.yang.common.XMLNamespace in project yangtools by opendaylight.

the class SimpleSchemaContextTest method testGetModulesOrdering.

@Test
public void testGetModulesOrdering() {
    final Module foo0 = mockModule("foo", Optional.empty());
    final Module foo1 = mockModule("foo", Revision.ofNullable("2018-01-01"));
    final Module foo2 = mockModule("foo", Revision.ofNullable("2018-01-16"));
    final List<Module> expected = ImmutableList.of(foo2, foo1, foo0);
    assertGetModules(expected, foo0, foo1, foo2);
    assertGetModules(expected, foo0, foo2, foo1);
    assertGetModules(expected, foo1, foo0, foo2);
    assertGetModules(expected, foo1, foo2, foo0);
    assertGetModules(expected, foo2, foo0, foo1);
    assertGetModules(expected, foo2, foo1, foo0);
    assertFindModules(expected, "foo", foo0, foo1, foo2);
    assertFindModules(expected, "foo", foo0, foo2, foo1);
    assertFindModules(expected, "foo", foo1, foo0, foo2);
    assertFindModules(expected, "foo", foo1, foo2, foo0);
    assertFindModules(expected, "foo", foo2, foo0, foo1);
    assertFindModules(expected, "foo", foo2, foo1, foo0);
    final XMLNamespace uri = XMLNamespace.of("foo");
    assertFindModules(expected, uri, foo0, foo1, foo2);
    assertFindModules(expected, uri, foo0, foo2, foo1);
    assertFindModules(expected, uri, foo1, foo0, foo2);
    assertFindModules(expected, uri, foo1, foo2, foo0);
    assertFindModules(expected, uri, foo2, foo0, foo1);
    assertFindModules(expected, uri, foo2, foo1, foo0);
}
Also used : XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Module(org.opendaylight.yangtools.yang.model.api.Module) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) Test(org.junit.Test)

Example 9 with XMLNamespace

use of org.opendaylight.yangtools.yang.common.XMLNamespace in project yangtools by opendaylight.

the class ImportStatementSupport method onPreLinkageDeclared.

@Override
public void onPreLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
    /*
         * Add ModuleIdentifier of a module which is required by this module.
         * Based on this information, required modules are searched from library
         * sources.
         */
    final SourceIdentifier importId = RevisionImport.getImportedSourceIdentifier(stmt);
    stmt.addRequiredSource(importId);
    final String moduleName = stmt.getArgument();
    final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
    final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
    final Prerequisite<Mutable<?, ?, ?>> rootPrereq = importAction.mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
    importAction.apply(new InferenceAction() {

        @Override
        public void apply(final InferenceContext ctx) {
            final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
            verify(moduleName.equals(importedModuleContext.getRawArgument()));
            final XMLNamespace importedModuleNamespace = verifyNotNull(importedModuleContext.getFromNamespace(ModuleNameToNamespace.class, moduleName));
            final String impPrefix = SourceException.throwIfNull(firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt, "Missing prefix statement");
            final Mutable<?, ?, ?> root = rootPrereq.resolve(ctx);
            // Version 1 sources must not import-by-revision Version 1.1 modules
            if (importId.getRevision().isPresent() && root.yangVersion() == YangVersion.VERSION_1) {
                final YangVersion importedVersion = importedModuleContext.yangVersion();
                if (importedVersion != YangVersion.VERSION_1) {
                    throw new YangVersionLinkageException(stmt, "Cannot import by revision version %s module %s", importedVersion, moduleName);
                }
            }
            stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
        }

        @Override
        public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
            InferenceException.throwIf(failed.contains(imported), stmt, "Imported module [%s] was not found.", moduleName);
        }
    });
}
Also used : InferenceContext(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) StmtContext(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext) InferenceAction(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction) Mutable(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable) YangVersion(org.opendaylight.yangtools.yang.common.YangVersion) YangVersionLinkageException(org.opendaylight.yangtools.yang.parser.spi.source.YangVersionLinkageException) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) ModelActionBuilder(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder) ImpPrefixToNamespace(org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace)

Example 10 with XMLNamespace

use of org.opendaylight.yangtools.yang.common.XMLNamespace in project yangtools by opendaylight.

the class ModuleStatementSupport method onPreLinkageDeclared.

@Override
public void onPreLinkageDeclared(final Mutable<Unqualified, ModuleStatement, ModuleEffectiveStatement> stmt) {
    final String moduleName = stmt.getRawArgument();
    final XMLNamespace moduleNs = SourceException.throwIfNull(firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class), stmt, "Namespace of the module [%s] is missing", moduleName);
    stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs);
    final String modulePrefix = SourceException.throwIfNull(firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt, "Prefix of the module [%s] is missing", moduleName);
    stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs);
    stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt);
    final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
    final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
    stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
    stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getArgument().getLocalName(), revisionDate));
}
Also used : NamespaceStatement(org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement) Revision(org.opendaylight.yangtools.yang.common.Revision) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) PrefixStatement(org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement)

Aggregations

XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)35 Module (org.opendaylight.yangtools.yang.model.api.Module)14 QName (org.opendaylight.yangtools.yang.common.QName)12 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)9 Revision (org.opendaylight.yangtools.yang.common.Revision)9 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)9 Test (org.junit.Test)8 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)4 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)3 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)3 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)3 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)3 ModuleImport (org.opendaylight.yangtools.yang.model.api.ModuleImport)3 SchemaContext (org.opendaylight.yangtools.yang.model.api.SchemaContext)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 Optional (java.util.Optional)2