Search in sources :

Example 26 with XMLNamespace

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

the class JsonParserStream method toModuleNames.

private String toModuleNames(final Set<XMLNamespace> potentialUris) {
    final StringBuilder builder = new StringBuilder();
    for (final XMLNamespace potentialUri : potentialUris) {
        builder.append('\n');
        // FIXME how to get information about revision from JSON input? currently first available is used.
        builder.append(codecs.getEffectiveModelContext().findModules(potentialUri).iterator().next().getName());
    }
    return builder.toString();
}
Also used : XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace)

Example 27 with XMLNamespace

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

the class JsonParserStream method read.

private void read(final JsonReader in, AbstractNodeDataWithSchema<?> parent) throws IOException {
    switch(in.peek()) {
        case STRING:
        case NUMBER:
            setValue(parent, in.nextString());
            break;
        case BOOLEAN:
            setValue(parent, Boolean.toString(in.nextBoolean()));
            break;
        case NULL:
            in.nextNull();
            setValue(parent, null);
            break;
        case BEGIN_ARRAY:
            in.beginArray();
            while (in.hasNext()) {
                if (parent instanceof LeafNodeDataWithSchema) {
                    read(in, parent);
                } else {
                    final AbstractNodeDataWithSchema<?> newChild = newArrayEntry(parent);
                    read(in, newChild);
                }
            }
            in.endArray();
            return;
        case BEGIN_OBJECT:
            final Set<String> namesakes = new HashSet<>();
            in.beginObject();
            /*
                 * This allows parsing of incorrectly /as showcased/
                 * in testconf nesting of list items - eg.
                 * lists with one value are sometimes serialized
                 * without wrapping array.
                 *
                 */
            if (isArray(parent)) {
                parent = newArrayEntry(parent);
            }
            while (in.hasNext()) {
                final String jsonElementName = in.nextName();
                final DataSchemaNode parentSchema = parent.getSchema();
                final Entry<String, XMLNamespace> namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
                final String localName = namespaceAndName.getKey();
                final XMLNamespace namespace = namespaceAndName.getValue();
                if (lenient && (localName == null || namespace == null)) {
                    LOG.debug("Schema node with name {} was not found under {}", localName, parentSchema.getQName());
                    in.skipValue();
                    continue;
                }
                addNamespace(namespace);
                if (!namesakes.add(jsonElementName)) {
                    throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
                }
                final Deque<DataSchemaNode> childDataSchemaNodes = ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, localName, getCurrentNamespace());
                checkState(!childDataSchemaNodes.isEmpty(), "Schema for node with name %s and namespace %s does not exist at %s", localName, getCurrentNamespace(), parentSchema);
                final QName qname = childDataSchemaNodes.peekLast().getQName();
                final AbstractNodeDataWithSchema<?> newChild = ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes, ChildReusePolicy.NOOP);
                if (newChild instanceof AnyXmlNodeDataWithSchema) {
                    readAnyXmlValue(in, (AnyXmlNodeDataWithSchema) newChild, jsonElementName);
                } else {
                    stack.enterDataTree(qname);
                    read(in, newChild);
                    stack.exit();
                }
                removeNamespace();
            }
            in.endObject();
            return;
        default:
            break;
    }
}
Also used : TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema) JsonSyntaxException(com.google.gson.JsonSyntaxException) CompositeNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) HashSet(java.util.HashSet) AnyXmlNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema)

Example 28 with XMLNamespace

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

the class SchemaContextProxyTest method mockModuleLike.

private static void mockModuleLike(final ModuleLike mockedModule, final String name) {
    doReturn(name).when(mockedModule).getName();
    doReturn(Optional.of(REVISION)).when(mockedModule).getRevision();
    final XMLNamespace newNamespace = XMLNamespace.of(NAMESPACE.toString() + ":" + name);
    doReturn(newNamespace).when(mockedModule).getNamespace();
    doReturn(QNameModule.create(newNamespace, REVISION)).when(mockedModule).getQNameModule();
    doReturn(new HashSet<>()).when(mockedModule).getSubmodules();
    doReturn(mockedModule.getQNameModule().toString()).when(mockedModule).toString();
    mockModuleImport(mockedModule);
}
Also used : XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace)

Example 29 with XMLNamespace

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

the class SourceSpecificContext method preLinkagePrefixes.

private PrefixToModule preLinkagePrefixes() {
    final PrefixToModuleMap preLinkagePrefixes = new PrefixToModuleMap();
    final Map<String, XMLNamespace> prefixToNamespaceMap = getAllFromLocalStorage(ImpPrefixToNamespace.class);
    if (prefixToNamespaceMap == null) {
        // :FIXME if it is a submodule without any import, the map is null. Handle also submodules and includes...
        return null;
    }
    prefixToNamespaceMap.forEach((key, value) -> preLinkagePrefixes.put(key, QNameModule.create(value)));
    return preLinkagePrefixes;
}
Also used : XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) PrefixToModuleMap(org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModuleMap)

Example 30 with XMLNamespace

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

the class ModuleStatementSupport method onLinkageDeclared.

@Override
public void onLinkageDeclared(final Mutable<Unqualified, ModuleStatement, ModuleEffectiveStatement> stmt) {
    final XMLNamespace moduleNs = SourceException.throwIfNull(firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class), stmt, "Namespace of the module [%s] is missing", stmt.argument());
    final Optional<Revision> revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements());
    final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern();
    final StmtContext<?, ModuleStatement, ModuleEffectiveStatement> possibleDuplicateModule = stmt.getFromNamespace(NamespaceToModule.class, qNameModule);
    if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) {
        throw new SourceException(stmt, "Module namespace collision: %s. At %s", qNameModule.getNamespace(), possibleDuplicateModule.sourceReference());
    }
    final String moduleName = stmt.getRawArgument();
    final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(moduleName, revisionDate);
    stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt);
    stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt);
    stmt.addContext(NamespaceToModule.class, qNameModule, stmt);
    final String modulePrefix = SourceException.throwIfNull(firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt, "Prefix of the module [%s] is missing", stmt.argument());
    stmt.addToNs(QNameModuleNamespace.class, Empty.value(), qNameModule);
    stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule);
    stmt.addToNs(ModuleNameToModuleQName.class, moduleName, qNameModule);
    stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule);
    stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier);
    stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, moduleName);
    stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt);
    if (semanticVersioning) {
        addToSemVerModuleNamespace(stmt, moduleIdentifier);
    }
}
Also used : NamespaceStatement(org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement) Revision(org.opendaylight.yangtools.yang.common.Revision) ModuleStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement) SemVerSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) RevisionSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier) ModuleCtxToSourceIdentifier(org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) SourceException(org.opendaylight.yangtools.yang.parser.spi.source.SourceException) ModuleEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement) 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