Search in sources :

Example 1 with NormalizedMetadata

use of org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata in project netconf by opendaylight.

the class NetconfUtil method writeNormalizedNode.

/**
 * Write data specified by {@link YangInstanceIdentifier} along with corresponding {@code metadata}
 * into {@link DOMResult}.
 *
 * @param query      path to the root node
 * @param metadata   metadata to be written
 * @param result     DOM result holder
 * @param schemaPath schema path of the parent node
 * @param context    mountpoint schema context
 * @throws IOException        when failed to write data into {@link NormalizedNodeStreamWriter}
 * @throws XMLStreamException when failed to serialize data into XML document
 */
public static void writeNormalizedNode(final YangInstanceIdentifier query, @Nullable final NormalizedMetadata metadata, final DOMResult result, final SchemaPath schemaPath, final EffectiveModelContext context) throws IOException, XMLStreamException {
    if (metadata == null) {
        writeNormalizedNode(query, result, schemaPath, context);
        return;
    }
    final XMLStreamWriter xmlWriter = XML_FACTORY.createXMLStreamWriter(result);
    XML_NAMESPACE_SETTER.initializeNamespace(xmlWriter);
    try (var streamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, context, schemaPath);
        var writer = new EmptyListXmlMetadataWriter(streamWriter, xmlWriter, streamWriter.getExtensions().getInstance(StreamWriterMetadataExtension.class), metadata)) {
        final Iterator<PathArgument> it = query.getPathArguments().iterator();
        final PathArgument first = it.next();
        StreamingContext.fromSchemaAndQNameChecked(context, first.getNodeType()).streamToWriter(writer, first, it);
    } finally {
        xmlWriter.close();
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) StreamWriterMetadataExtension(org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension)

Example 2 with NormalizedMetadata

use of org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata in project yangtools by opendaylight.

the class NormalizedMetadataWriter method write.

/**
 * Iterate over the provided {@link NormalizedNode} and {@link NormalizedMetadata} and emit write events to the
 * encapsulated {@link NormalizedNodeStreamWriter}.
 *
 * @param data NormalizedNode data
 * @param metadata {@link NormalizedMetadata} metadata
 * @return NormalizedNodeWriter this
 * @throws NullPointerException if any argument is null
 * @throws IllegalArgumentException if metadata does not match data
 * @throws IOException when thrown from the backing writer.
 */
@NonNull
public NormalizedMetadataWriter write(final NormalizedNode data, final NormalizedMetadata metadata) throws IOException {
    final PathArgument dataId = data.getIdentifier();
    final PathArgument metaId = metadata.getIdentifier();
    checkArgument(dataId.equals(metaId), "Mismatched data %s and metadata %s", dataId, metaId);
    final StreamWriterMetadataExtension metaWriter = writer.getExtensions().getInstance(StreamWriterMetadataExtension.class);
    final NormalizedNodeStreamWriter delegate = metaWriter == null ? writer : new NormalizedNodeStreamWriterMetadataDecorator(writer, metaWriter, metadata);
    final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(delegate, orderKeyLeaves);
    nnWriter.write(data);
    nnWriter.flush();
    return this;
}
Also used : NormalizedNodeWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) StreamWriterMetadataExtension(org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 3 with NormalizedMetadata

use of org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata in project netconf by opendaylight.

the class NetconfMessageTransformUtil method leafMetadata.

private static NormalizedMetadata leafMetadata(final YangInstanceIdentifier path, final ModifyAction oper) {
    final List<PathArgument> args = path.getPathArguments();
    final Deque<Builder> builders = new ArrayDeque<>(args.size());
    // Step one: open builders
    for (PathArgument arg : args) {
        builders.push(ImmutableNormalizedMetadata.builder().withIdentifier(arg));
    }
    // Step two: set the top builder's metadata
    builders.peek().withAnnotation(NETCONF_OPERATION_QNAME_LEGACY, oper.toString().toLowerCase(Locale.US));
    // Step three: build the tree
    while (true) {
        final ImmutableNormalizedMetadata currentMeta = builders.pop().build();
        final Builder parent = builders.peek();
        if (parent != null) {
            parent.withChild(currentMeta);
        } else {
            return currentMeta;
        }
    }
}
Also used : Builder(org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata.Builder) RpcResultBuilder(org.opendaylight.yangtools.yang.common.RpcResultBuilder) ImmutableNormalizedMetadata(org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) ArrayDeque(java.util.ArrayDeque)

Aggregations

PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)3 StreamWriterMetadataExtension (org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension)2 ArrayDeque (java.util.ArrayDeque)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 ImmutableNormalizedMetadata (org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata)1 Builder (org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata.Builder)1 RpcResultBuilder (org.opendaylight.yangtools.yang.common.RpcResultBuilder)1 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)1 NormalizedNodeWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter)1