Search in sources :

Example 16 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class NetconfDeviceRpcTest method resolveNode.

private static Node resolveNode(final DOMRpcResult result) {
    DataContainerChild value = ((ContainerNode) result.getResult()).findChildByArg(NetconfMessageTransformUtil.NETCONF_DATA_NODEID).get();
    Node node = ((DOMSourceAnyxmlNode) value).body().getNode();
    assertNotNull(node);
    return node;
}
Also used : DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Node(org.w3c.dom.Node) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)

Example 17 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class NetconfRemoteSchemaYangSourceProvider method getSchemaFromRpc.

private static Optional<String> getSchemaFromRpc(final RemoteDeviceId id, final NormalizedNode result) {
    if (result == null) {
        return Optional.empty();
    }
    final Optional<DataContainerChild> child = ((ContainerNode) result).findChildByArg(NETCONF_DATA_PATHARG);
    checkState(child.isPresent() && child.get() instanceof DOMSourceAnyxmlNode, "%s Unexpected response to get-schema, expected response with one child %s, but was %s", id, NETCONF_DATA, result);
    final DOMSource wrappedNode = ((DOMSourceAnyxmlNode) child.get()).body();
    final Element dataNode = (Element) requireNonNull(wrappedNode.getNode());
    return Optional.of(dataNode.getTextContent().trim());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Element(org.w3c.dom.Element) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)

Example 18 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class BaseRpcSchemalessTransformer method toRpcResult.

@Override
public DOMRpcResult toRpcResult(final NetconfMessage message, final QName rpc) {
    final NormalizedNode normalizedNode;
    if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpc)) {
        final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
        final Document data = XmlUtil.newDocument();
        data.appendChild(data.importNode(xmlData, true));
        DOMSourceAnyxmlNode xmlDataNode = Builders.anyXmlBuilder().withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_NODEID).withValue(new DOMSource(data)).build();
        normalizedNode = Builders.containerBuilder().withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_NODEID).withChild(xmlDataNode).build();
    } else {
        // other base rpcs don't have any output, we can simply construct the payload here
        Preconditions.checkArgument(isOkPresent(message.getDocument()), "Unexpected content in response of rpc: %s, %s", rpc, message);
        normalizedNode = null;
    }
    return new DefaultDOMRpcResult(normalizedNode);
}
Also used : DefaultDOMRpcResult(org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult) DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Document(org.w3c.dom.Document)

Example 19 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class NetconfMessageTransformUtil method createEditConfigAnyxml.

/**
 * Create edit-config structure to invoke {@code operation} with {@code lastChildOverride} data on {@code dataPath}.
 *
 * @param ctx {@link EffectiveModelContext} device's model context
 * @param dataPath {@link YangInstanceIdentifier} path to data in device's data-store
 * @param operation Optional of {@link ModifyAction} action to be invoked
 * @param lastChildOverride Optional of {@code NormalizedNode} data on which action will be invoked
 * @return {@link DOMSourceAnyxmlNode} containing edit-config structure
 */
public static DOMSourceAnyxmlNode createEditConfigAnyxml(final EffectiveModelContext ctx, final YangInstanceIdentifier dataPath, final Optional<ModifyAction> operation, final Optional<NormalizedNode> lastChildOverride) {
    if (dataPath.isEmpty()) {
        Preconditions.checkArgument(lastChildOverride.isPresent(), "Data has to be present when creating structure for top level element");
        Preconditions.checkArgument(lastChildOverride.get() instanceof DataContainerChild, "Data has to be either container or a list node when creating structure for top level element, " + "but was: %s", lastChildOverride.get());
    }
    final var element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(), Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString()));
    final var metadata = operation.map(o -> leafMetadata(dataPath, o)).orElse(null);
    try {
        if (lastChildOverride.isPresent()) {
            // FIXME remove ImmutableNodes.fromInstanceId usage
            final var configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride.get());
            NetconfUtil.writeNormalizedNode(configContent, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
        } else {
            NetconfUtil.writeNormalizedNode(dataPath, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
        }
    } catch (final IOException | XMLStreamException e) {
        throw new IllegalStateException("Unable to serialize edit config content element for path " + dataPath, e);
    }
    return Builders.anyXmlBuilder().withNodeIdentifier(NETCONF_CONFIG_NODEID).withValue(new DOMSource(element)).build();
}
Also used : NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) LoggerFactory(org.slf4j.LoggerFactory) NetconfUtil(org.opendaylight.netconf.util.NetconfUtil) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) ErrorSeverity(org.opendaylight.yangtools.yang.common.ErrorSeverity) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) MessageCounter(org.opendaylight.netconf.sal.connect.util.MessageCounter) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) NETCONF_QNAME(org.opendaylight.netconf.util.NetconfUtil.NETCONF_QNAME) NetconfState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) FailedNetconfMessage(org.opendaylight.netconf.api.FailedNetconfMessage) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) URI(java.net.URI) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) XmlUtil(org.opendaylight.netconf.api.xml.XmlUtil) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) NodeWithValue(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Instant(java.time.Instant) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) Builder(org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata.Builder) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) CreateSubscriptionInput(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) YangConstants(org.opendaylight.yangtools.yang.common.YangConstants) Entry(java.util.Map.Entry) Optional(java.util.Optional) NormalizedMetadata(org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata) NonNull(org.eclipse.jdt.annotation.NonNull) DOMSource(javax.xml.transform.dom.DOMSource) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) NETCONF_DATA_QNAME(org.opendaylight.netconf.util.NetconfUtil.NETCONF_DATA_QNAME) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) ImmutableNormalizedMetadata(org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata) Deque(java.util.Deque) DataSchemaContextNode(org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode) ModifyAction(org.opendaylight.netconf.api.ModifyAction) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) ImmutableList(com.google.common.collect.ImmutableList) DocumentedException(org.opendaylight.netconf.api.DocumentedException) SimpleEntry(java.util.AbstractMap.SimpleEntry) Builders(org.opendaylight.yangtools.yang.data.impl.schema.Builders) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) AnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode) IOException(java.io.IOException) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) DataSchemaContextTree(org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) QName(org.opendaylight.yangtools.yang.common.QName) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) Element(org.w3c.dom.Element) EditContent(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.EditContent) RpcResultBuilder(org.opendaylight.yangtools.yang.common.RpcResultBuilder) NetconfCapabilityChange(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) NetconfNotification(org.opendaylight.netconf.notifications.NetconfNotification) Preconditions(com.google.common.base.Preconditions) ArrayDeque(java.util.ArrayDeque) DOMResult(javax.xml.transform.dom.DOMResult) NetconfMessageUtil(org.opendaylight.netconf.util.messages.NetconfMessageUtil) RpcError(org.opendaylight.yangtools.yang.common.RpcError) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) IOException(java.io.IOException)

Example 20 with DOMSourceAnyxmlNode

use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.

the class SchemalessRpcStructureTransformer method selectFromDataStructure.

/**
 * Selects elements in anyxml data node, which matches path arguments QNames. Since class in not context aware,
 * method searches for all elements as they are named in path.
 * @param data data, must be of type {@link DOMSourceAnyxmlNode}
 * @param path path to select
 * @return selected data
 */
@Override
public Optional<NormalizedNode> selectFromDataStructure(final DataContainerChild data, final YangInstanceIdentifier path) {
    Preconditions.checkArgument(data instanceof DOMSourceAnyxmlNode);
    final List<XmlElement> xmlElements = selectMatchingNodes(getSourceElement(((DOMSourceAnyxmlNode) data).body()), path);
    final Document result = XmlUtil.newDocument();
    final Element dataElement = result.createElementNS(NETCONF_DATA_QNAME.getNamespace().toString(), NETCONF_DATA_QNAME.getLocalName());
    result.appendChild(dataElement);
    for (XmlElement xmlElement : xmlElements) {
        dataElement.appendChild(result.importNode(xmlElement.getDomElement(), true));
    }
    final DOMSourceAnyxmlNode resultAnyxml = Builders.anyXmlBuilder().withNodeIdentifier(NETCONF_DATA_NODEID).withValue(new DOMSource(result)).build();
    return Optional.of(resultAnyxml);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Document(org.w3c.dom.Document)

Aggregations

DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)30 Test (org.junit.Test)16 DOMSource (javax.xml.transform.dom.DOMSource)15 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)15 Diff (org.custommonkey.xmlunit.Diff)10 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)10 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)10 Document (org.w3c.dom.Document)10 DataContainerChild (org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild)9 QName (org.opendaylight.yangtools.yang.common.QName)8 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)7 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)7 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)6 Element (org.w3c.dom.Element)6 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)5 IOException (java.io.IOException)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)4 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)4 URISyntaxException (java.net.URISyntaxException)3