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"));
}
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);
}
}
}
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);
}
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);
}
});
}
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));
}
Aggregations