Search in sources :

Example 1 with XMLNamespace

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

the class ConfigurationImplBaseTest method testAddModuleShardConfiguration.

@Test
public void testAddModuleShardConfiguration() throws Exception {
    XMLNamespace namespace = XMLNamespace.of("urn:opendaylight:test:oven");
    String moduleName = "oven";
    String shardName = "oven-shard";
    String shardStrategyName = ModuleShardStrategy.NAME;
    Collection<MemberName> shardMemberNames = ImmutableSortedSet.of(MEMBER_1, MEMBER_4, MEMBER_5);
    configuration.addModuleShardConfiguration(new ModuleShardConfiguration(namespace, moduleName, shardName, shardStrategyName, shardMemberNames));
    assertEquals("getMemberShardNames", ImmutableSortedSet.of("people-1", "cars-1", "test-1", "default", shardName), ImmutableSortedSet.copyOf(configuration.getMemberShardNames(MEMBER_1)));
    assertEquals("getMemberShardNames", ImmutableSortedSet.of(shardName), ImmutableSortedSet.copyOf(configuration.getMemberShardNames(MEMBER_4)));
    assertEquals("getMemberShardNames", ImmutableSortedSet.of(shardName), ImmutableSortedSet.copyOf(configuration.getMemberShardNames(MEMBER_5)));
    assertEquals("getMembersFromShardName", shardMemberNames, ImmutableSortedSet.copyOf(configuration.getMembersFromShardName(shardName)));
    assertEquals("getShardNameForModule", shardName, configuration.getShardNameForModule(moduleName));
    assertEquals("getModuleNameFromNameSpace", moduleName, configuration.getModuleNameFromNameSpace(namespace.toString()));
    assertEquals("getAllShardNames", ImmutableSortedSet.of("people-1", "cars-1", "test-1", "default", shardName), ImmutableSortedSet.copyOf(configuration.getAllShardNames()));
    ShardStrategy strategy = configuration.getStrategyForModule("cars");
    assertNotNull("getStrategyForModule null", strategy);
    assertEquals("getStrategyForModule type", ModuleShardStrategy.class, strategy.getClass());
}
Also used : ShardStrategy(org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy) ModuleShardStrategy(org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) MemberName(org.opendaylight.controller.cluster.access.concepts.MemberName) Test(org.junit.Test)

Example 2 with XMLNamespace

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

the class JsonParserStream method resolveNamespace.

private Entry<String, XMLNamespace> resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
    final int lastIndexOfColon = childName.lastIndexOf(':');
    String moduleNamePart = null;
    String nodeNamePart = null;
    XMLNamespace namespace = null;
    if (lastIndexOfColon != -1) {
        moduleNamePart = childName.substring(0, lastIndexOfColon);
        nodeNamePart = childName.substring(lastIndexOfColon + 1);
        final Iterator<? extends Module> m = codecs.getEffectiveModelContext().findModules(moduleNamePart).iterator();
        namespace = m.hasNext() ? m.next().getNamespace() : null;
    } else {
        nodeNamePart = childName;
    }
    if (namespace == null) {
        final Set<XMLNamespace> potentialUris = resolveAllPotentialNamespaces(nodeNamePart, dataSchemaNode);
        if (potentialUris.contains(getCurrentNamespace())) {
            namespace = getCurrentNamespace();
        } else if (potentialUris.size() == 1) {
            namespace = potentialUris.iterator().next();
        } else if (potentialUris.size() > 1) {
            throw new IllegalStateException("Choose suitable module name for element " + nodeNamePart + ":" + toModuleNames(potentialUris));
        } else if (potentialUris.isEmpty() && !lenient) {
            throw new IllegalStateException("Schema node with name " + nodeNamePart + " was not found under " + dataSchemaNode.getQName() + ".");
        }
    }
    return new SimpleImmutableEntry<>(nodeNamePart, namespace);
}
Also used : SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace)

Example 3 with XMLNamespace

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

the class XmlParserStream method read.

private void read(final XMLStreamReader in, final AbstractNodeDataWithSchema<?> parent, final String rootElement) throws XMLStreamException {
    if (!in.hasNext()) {
        return;
    }
    if (parent instanceof LeafNodeDataWithSchema || parent instanceof LeafListEntryNodeDataWithSchema) {
        parent.setAttributes(getElementAttributes(in));
        setValue((SimpleNodeDataWithSchema<?>) parent, in.getElementText().trim(), in.getNamespaceContext());
        if (isNextEndDocument(in)) {
            return;
        }
        if (!isAtElement(in)) {
            in.nextTag();
        }
        return;
    }
    if (parent instanceof ListEntryNodeDataWithSchema || parent instanceof ContainerNodeDataWithSchema) {
        parent.setAttributes(getElementAttributes(in));
    }
    if (parent instanceof LeafListNodeDataWithSchema || parent instanceof ListNodeDataWithSchema) {
        String xmlElementName = in.getLocalName();
        while (xmlElementName.equals(parent.getSchema().getQName().getLocalName())) {
            read(in, newEntryNode(parent), rootElement);
            if (in.getEventType() == XMLStreamConstants.END_DOCUMENT || in.getEventType() == XMLStreamConstants.END_ELEMENT) {
                break;
            }
            xmlElementName = in.getLocalName();
        }
        return;
    }
    if (parent instanceof AnyXmlNodeDataWithSchema) {
        setValue((AnyXmlNodeDataWithSchema) parent, readAnyXmlValue(in), in.getNamespaceContext());
        if (isNextEndDocument(in)) {
            return;
        }
        if (!isAtElement(in)) {
            in.nextTag();
        }
        return;
    }
    if (parent instanceof AnydataNodeDataWithSchema) {
        final AnydataNodeDataWithSchema anydata = (AnydataNodeDataWithSchema) parent;
        anydata.setObjectModel(DOMSourceAnydata.class);
        anydata.setAttributes(getElementAttributes(in));
        setValue(anydata, readAnyXmlValue(in), in.getNamespaceContext());
        if (isNextEndDocument(in)) {
            return;
        }
        if (!isAtElement(in)) {
            in.nextTag();
        }
        return;
    }
    switch(in.nextTag()) {
        case XMLStreamConstants.START_ELEMENT:
            // FIXME: 7.0.0: why do we even need this tracker? either document it or remove it.
            // it looks like it is a crude duplicate finder, which should really be handled via
            // ChildReusePolicy.REJECT
            final Set<Entry<String, String>> namesakes = new HashSet<>();
            while (in.hasNext()) {
                final String xmlElementName = in.getLocalName();
                final DataSchemaNode parentSchema = parent.getSchema();
                final String parentSchemaName = parentSchema.getQName().getLocalName();
                if (parentSchemaName.equals(xmlElementName) && in.getEventType() == XMLStreamConstants.END_ELEMENT) {
                    if (isNextEndDocument(in)) {
                        break;
                    }
                    if (!isAtElement(in)) {
                        in.nextTag();
                    }
                    break;
                }
                if (in.isEndElement() && rootElement.equals(xmlElementName)) {
                    break;
                }
                final String elementNS = in.getNamespaceURI();
                final boolean added = namesakes.add(new SimpleImmutableEntry<>(elementNS, xmlElementName));
                final XMLNamespace nsUri;
                try {
                    nsUri = rawXmlNamespace(elementNS).getNamespace();
                } catch (IllegalArgumentException e) {
                    throw new XMLStreamException("Failed to convert namespace " + xmlElementName, in.getLocation(), e);
                }
                final Deque<DataSchemaNode> childDataSchemaNodes = ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, xmlElementName, nsUri);
                if (!childDataSchemaNodes.isEmpty()) {
                    final boolean elementList = isElementList(childDataSchemaNodes);
                    if (!added && !elementList) {
                        throw new XMLStreamException(String.format("Duplicate element \"%s\" in namespace \"%s\" with parent \"%s\" in XML input", xmlElementName, elementNS, parentSchema), in.getLocation());
                    }
                    // We have a match, proceed with it
                    final QName qname = childDataSchemaNodes.peekLast().getQName();
                    final AbstractNodeDataWithSchema<?> child = ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes, elementList ? ChildReusePolicy.REUSE : ChildReusePolicy.NOOP);
                    stack.enterDataTree(qname);
                    read(in, child, rootElement);
                    stack.exit();
                    continue;
                }
                if (parent instanceof AbstractMountPointDataWithSchema) {
                    // Parent can potentially hold a mount point, let's see if there is a label present
                    final Optional<MountPointSchemaNode> optMount;
                    if (parentSchema instanceof ContainerSchemaNode) {
                        optMount = MountPointSchemaNode.streamAll((ContainerSchemaNode) parentSchema).findFirst();
                    } else if (parentSchema instanceof ListSchemaNode) {
                        optMount = MountPointSchemaNode.streamAll((ListSchemaNode) parentSchema).findFirst();
                    } else {
                        throw new XMLStreamException("Unhandled mount-aware schema " + parentSchema, in.getLocation());
                    }
                    if (optMount.isPresent()) {
                        final MountPointIdentifier mountId = MountPointIdentifier.of(optMount.get().getQName());
                        LOG.debug("Assuming node {} and namespace {} belongs to mount point {}", xmlElementName, nsUri, mountId);
                        final Optional<MountPointContextFactory> optFactory = codecs.mountPointContext().findMountPoint(mountId);
                        if (optFactory.isPresent()) {
                            final MountPointData mountData = ((AbstractMountPointDataWithSchema<?>) parent).getMountPointData(mountId, optFactory.get());
                            addMountPointChild(mountData, nsUri, xmlElementName, new DOMSource(readAnyXmlValue(in).getDocumentElement()));
                            continue;
                        }
                        LOG.debug("Mount point {} not attached", mountId);
                    }
                }
                // We have not handled the node -- let's decide what to do about that
                if (strictParsing) {
                    throw new XMLStreamException(String.format("Schema for node with name %s and namespace %s does not exist in parent %s", xmlElementName, elementNS, parentSchema), in.getLocation());
                }
                LOG.debug("Skipping unknown node ns=\"{}\" localName=\"{}\" in parent {}", elementNS, xmlElementName, parentSchema);
                skipUnknownNode(in);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (isNextEndDocument(in)) {
                break;
            }
            if (!isAtElement(in)) {
                in.nextTag();
            }
            break;
        default:
            break;
    }
}
Also used : LeafListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema) ListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema) DOMSource(javax.xml.transform.dom.DOMSource) ContainerNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ContainerNodeDataWithSchema) MountPointData(org.opendaylight.yangtools.yang.data.util.MountPointData) AbstractMountPointDataWithSchema(org.opendaylight.yangtools.yang.data.util.AbstractMountPointDataWithSchema) Entry(java.util.Map.Entry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) HashSet(java.util.HashSet) MountPointContextFactory(org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) AnydataNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnydataNodeDataWithSchema) QName(org.opendaylight.yangtools.yang.common.QName) LeafNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema) MountPointSchemaNode(org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode) LeafListEntryNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema) LeafListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema) CompositeNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema) XMLStreamException(javax.xml.stream.XMLStreamException) ListEntryNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema) LeafListEntryNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) MountPointIdentifier(org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier) AnyXmlNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema)

Example 4 with XMLNamespace

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

the class RandomPrefixTest method test2QNames1Namespace.

@Test
public void test2QNames1Namespace() throws Exception {
    final RandomPrefix a = new RandomPrefix(null);
    final XMLNamespace uri = XMLNamespace.of("localhost");
    final QName qname = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
    final QName qname2 = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
    assertEquals(a.encodePrefix(qname.getNamespace()), a.encodePrefix(qname2.getNamespace()));
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Test(org.junit.Test)

Example 5 with XMLNamespace

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

the class RandomPrefixTest method testQNameNoPrefix.

@Test
public void testQNameNoPrefix() throws Exception {
    final RandomPrefix a = new RandomPrefix(null);
    final XMLNamespace uri = XMLNamespace.of("localhost");
    QName qname = QName.create(uri, Revision.of("2000-01-01"), "local-name");
    assertEquals("a", a.encodePrefix(qname.getNamespace()));
    qname = QName.create(QNameModule.create(uri, Revision.of("2000-01-01")), "local-name");
    assertEquals("a", a.encodePrefix(qname.getNamespace()));
    qname = QName.create(QNameModule.create(XMLNamespace.of("second"), Revision.of("2000-01-01")), "local-name");
    assertEquals("b", a.encodePrefix(qname.getNamespace()));
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Test(org.junit.Test)

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