Search in sources :

Example 1 with MountPointIdentifier

use of org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier 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 2 with MountPointIdentifier

use of org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier in project yangtools by opendaylight.

the class AbstractMountPointDataWithSchema method getMountPointData.

public final MountPointData getMountPointData(final MountPointIdentifier label, final MountPointContextFactory factory) {
    if (mountedData != null) {
        final MountPointIdentifier existing = mountedData.getIdentifier();
        checkState(label.equals(existing), "Mismatched mount label {}, already have {}", label, existing);
    } else {
        mountedData = new MountPointData(label, factory);
    }
    return mountedData;
}
Also used : MountPointIdentifier(org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier)

Example 3 with MountPointIdentifier

use of org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier in project netconf by opendaylight.

the class DeviceMountPointContext method create.

static MountPointContext create(final MountPointContext emptyContext, final ContainerNode mountData) {
    final Optional<DataContainerChild> optMountPoint = mountData.findChildByArg(MOUNT_POINT);
    if (optMountPoint.isEmpty()) {
        LOG.debug("mount-point list not present in {}", mountData);
        return emptyContext;
    }
    final EffectiveModelContext schemaContext = emptyContext.getEffectiveModelContext();
    final DataContainerChild mountPoint = optMountPoint.get();
    checkArgument(mountPoint instanceof MapNode, "mount-point list %s is not a MapNode", mountPoint);
    final Map<MountPointIdentifier, NetconfMountPointContextFactory> mountPoints = new HashMap<>();
    for (MapEntryNode entry : ((MapNode) mountPoint).body()) {
        final String moduleName = entry.findChildByArg(MODULE).map(mod -> {
            checkArgument(mod instanceof LeafNode, "Unexpected module leaf %s", mod);
            final Object value = mod.body();
            checkArgument(value instanceof String, "Unexpected module leaf value %s", value);
            return (String) value;
        }).orElseThrow(() -> new IllegalArgumentException("Mount module missing in " + entry));
        final Iterator<? extends Module> it = schemaContext.findModules(moduleName).iterator();
        checkArgument(it.hasNext(), "Failed to find a module named %s", moduleName);
        final QNameModule module = it.next().getQNameModule();
        final MountPointIdentifier mountId = MountPointIdentifier.of(QName.create(module, entry.findChildByArg(LABEL).map(lbl -> {
            checkArgument(lbl instanceof LeafNode, "Unexpected label leaf %s", lbl);
            final Object value = lbl.body();
            checkArgument(value instanceof String, "Unexpected label leaf value %s", value);
            return (String) value;
        }).orElseThrow(() -> new IllegalArgumentException("Mount module missing in " + entry))));
        final DataContainerChild child = entry.findChildByArg(SCHEMA_REF).orElseThrow(() -> new IllegalArgumentException("Missing schema-ref choice in " + entry));
        checkArgument(child instanceof ChoiceNode, "Unexpected schema-ref choice %s", child);
        final ChoiceNode schemaRef = (ChoiceNode) child;
        final Optional<DataContainerChild> maybeShared = schemaRef.findChildByArg(SHARED_SCHEMA);
        if (maybeShared.isEmpty()) {
            LOG.debug("Ignoring non-shared mountpoint entry {}", entry);
            continue;
        }
        mountPoints.put(mountId, new NetconfMountPointContextFactory(schemaContext));
    }
    return new DeviceMountPointContext(schemaContext, mountPoints);
}
Also used : NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) SchemaMountConstants(org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MountPointIdentifier(org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MountPointContextFactory(org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory) AbstractEffectiveModelContextProvider(org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveModelContextProvider) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) Immutable(org.opendaylight.yangtools.concepts.Immutable) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Module(org.opendaylight.yangtools.yang.model.api.Module) ImmutableMap(com.google.common.collect.ImmutableMap) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) MountPointContext(org.opendaylight.yangtools.rfc8528.data.api.MountPointContext) QName(org.opendaylight.yangtools.yang.common.QName) Optional(java.util.Optional) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NonNull(org.eclipse.jdt.annotation.NonNull) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) HashMap(java.util.HashMap) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) MountPointIdentifier(org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Aggregations

MountPointIdentifier (org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier)3 MountPointContextFactory (org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory)2 QName (org.opendaylight.yangtools.yang.common.QName)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 DOMSource (javax.xml.transform.dom.DOMSource)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 Immutable (org.opendaylight.yangtools.concepts.Immutable)1 MountPointContext (org.opendaylight.yangtools.rfc8528.data.api.MountPointContext)1 MountPointSchemaNode (org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode)1 SchemaMountConstants (org.opendaylight.yangtools.rfc8528.model.api.SchemaMountConstants)1