X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fconnect%2Fnetconf%2Futil%2FNetconfMessageTransformUtil.java;h=3ee435b998ac6f1e6f4bbe29741c4828e848c8f9;hb=14323f32667c16d5206e73bfe34eea48d4e2272f;hp=a0622d719c41fa60a0ed1c4d5823dc5a9a0ef802;hpb=9b80e63527cf2a9844d4a6b2e743c45169d19777;p=netconf.git diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfMessageTransformUtil.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfMessageTransformUtil.java index a0622d719c..3ee435b998 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfMessageTransformUtil.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfMessageTransformUtil.java @@ -7,100 +7,179 @@ */ package org.opendaylight.netconf.sal.connect.netconf.util; -import com.google.common.base.Optional; +import static org.opendaylight.netconf.util.NetconfUtil.NETCONF_DATA_QNAME; +import static org.opendaylight.netconf.util.NetconfUtil.NETCONF_QNAME; + import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.io.IOException; import java.net.URI; -import java.util.AbstractMap; -import java.util.Collections; +import java.time.Instant; +import java.time.format.DateTimeParseException; +import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Deque; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; -import org.opendaylight.controller.config.util.xml.XmlUtil; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier; +import org.opendaylight.netconf.api.DocumentedException; +import org.opendaylight.netconf.api.FailedNetconfMessage; +import org.opendaylight.netconf.api.ModifyAction; import org.opendaylight.netconf.api.NetconfDocumentedException; import org.opendaylight.netconf.api.NetconfMessage; +import org.opendaylight.netconf.api.xml.XmlElement; +import org.opendaylight.netconf.api.xml.XmlUtil; +import org.opendaylight.netconf.notifications.NetconfNotification; +import org.opendaylight.netconf.sal.connect.util.MessageCounter; import org.opendaylight.netconf.util.NetconfUtil; import org.opendaylight.netconf.util.messages.NetconfMessageUtil; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.EditContent; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange; +import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata; +import org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata; +import org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata.Builder; +import org.opendaylight.yangtools.yang.common.ErrorSeverity; +import org.opendaylight.yangtools.yang.common.ErrorTag; +import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcError; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity; +import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.opendaylight.yangtools.yang.data.api.ModifyAction; +import org.opendaylight.yangtools.yang.common.XMLNamespace; +import org.opendaylight.yangtools.yang.common.YangConstants; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; +import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; -import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; -import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter; +import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode; +import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; -public class NetconfMessageTransformUtil { +public final class NetconfMessageTransformUtil { - private static final Logger LOG= LoggerFactory.getLogger(NetconfMessageTransformUtil.class); + private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageTransformUtil.class); + public static final String MESSAGE_ID_PREFIX = "m"; public static final String MESSAGE_ID_ATTR = "message-id"; - public static final QName CREATE_SUBSCRIPTION_RPC_QNAME = QName.cachedReference(QName.create(CreateSubscriptionInput.QNAME, "create-subscription")); + public static final @NonNull QName CREATE_SUBSCRIPTION_RPC_QNAME = + QName.create(CreateSubscriptionInput.QNAME, "create-subscription").intern(); private static final String SUBTREE = "subtree"; // Blank document used for creation of new DOM nodes private static final Document BLANK_DOCUMENT = XmlUtil.newDocument(); public static final String EVENT_TIME = "eventTime"; - private NetconfMessageTransformUtil() {} - - public static final QName IETF_NETCONF_MONITORING = QName.create(NetconfState.QNAME, "ietf-netconf-monitoring"); - public static final QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data"); - public static final QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema"); - public static final QName IETF_NETCONF_MONITORING_SCHEMA_FORMAT = QName.create(IETF_NETCONF_MONITORING, "format"); - public static final QName IETF_NETCONF_MONITORING_SCHEMA_LOCATION = QName.create(IETF_NETCONF_MONITORING, "location"); - public static final QName IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER = QName.create(IETF_NETCONF_MONITORING, "identifier"); - public static final QName IETF_NETCONF_MONITORING_SCHEMA_VERSION = QName.create(IETF_NETCONF_MONITORING, "version"); - public static final QName IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE = QName.create(IETF_NETCONF_MONITORING, "namespace"); - - public static final QName IETF_NETCONF_NOTIFICATIONS = QName.create(NetconfCapabilityChange.QNAME, "ietf-netconf-notifications"); - - public static final QName NETCONF_QNAME = QName.cachedReference(QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "netconf")); - public static final URI NETCONF_URI = NETCONF_QNAME.getNamespace(); - - public static final QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data"); - public static final QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply"); - public static final QName NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok"); - public static final QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option"); - public static final QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running"); - public static final QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source"); - public static final QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate"); - public static final QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target"); - public static final QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config"); - public static final QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit"); - public static final QName NETCONF_VALIDATE_QNAME = QName.create(NETCONF_QNAME, "validate"); - public static final QName NETCONF_COPY_CONFIG_QNAME = QName.create(NETCONF_QNAME, "copy-config"); - public static final QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation"); - public static final QName NETCONF_DEFAULT_OPERATION_QNAME = QName.create(NETCONF_OPERATION_QNAME, "default-operation"); - public static final QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config"); - public static final QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config"); - public static final QName NETCONF_DISCARD_CHANGES_QNAME = QName.create(NETCONF_QNAME, "discard-changes"); - public static final QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type"); - public static final QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter"); - public static final QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get"); - public static final QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc"); + private NetconfMessageTransformUtil() { + + } + + public static final @NonNull QName IETF_NETCONF_MONITORING = + QName.create(NetconfState.QNAME, "ietf-netconf-monitoring").intern(); + public static final @NonNull QName GET_DATA_QNAME = QName.create(IETF_NETCONF_MONITORING, "data").intern(); + public static final @NonNull QName GET_SCHEMA_QNAME = QName.create(IETF_NETCONF_MONITORING, "get-schema").intern(); + public static final @NonNull QName IETF_NETCONF_MONITORING_SCHEMA_FORMAT = + QName.create(IETF_NETCONF_MONITORING, "format").intern(); + public static final @NonNull QName IETF_NETCONF_MONITORING_SCHEMA_LOCATION = + QName.create(IETF_NETCONF_MONITORING, "location").intern(); + public static final @NonNull QName IETF_NETCONF_MONITORING_SCHEMA_IDENTIFIER = + QName.create(IETF_NETCONF_MONITORING, "identifier").intern(); + public static final @NonNull QName IETF_NETCONF_MONITORING_SCHEMA_VERSION = + QName.create(IETF_NETCONF_MONITORING, "version").intern(); + public static final @NonNull QName IETF_NETCONF_MONITORING_SCHEMA_NAMESPACE = + QName.create(IETF_NETCONF_MONITORING, "namespace").intern(); + + public static final @NonNull QName IETF_NETCONF_NOTIFICATIONS = + QName.create(NetconfCapabilityChange.QNAME, "ietf-netconf-notifications").intern(); + + public static final XMLNamespace NETCONF_URI = NETCONF_QNAME.getNamespace(); + + public static final @NonNull NodeIdentifier NETCONF_DATA_NODEID = NodeIdentifier.create(NETCONF_DATA_QNAME); + + public static final @NonNull QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply").intern(); + public static final @NonNull NodeIdentifier NETCONF_RPC_REPLY_NODEID = + NodeIdentifier.create(NETCONF_RPC_REPLY_QNAME); + + public static final @NonNull QName NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok").intern(); + public static final @NonNull QName NETCONF_ERROR_OPTION_QNAME = + QName.create(NETCONF_QNAME, "error-option").intern(); + public static final @NonNull NodeIdentifier NETCONF_ERROR_OPTION_NODEID = + NodeIdentifier.create(NETCONF_ERROR_OPTION_QNAME); + public static final @NonNull QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running").intern(); + public static final @NonNull QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source").intern(); + public static final @NonNull NodeIdentifier NETCONF_SOURCE_NODEID = NodeIdentifier.create(NETCONF_SOURCE_QNAME); + public static final @NonNull QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate").intern(); + public static final @NonNull QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target").intern(); + public static final @NonNull NodeIdentifier NETCONF_TARGET_NODEID = NodeIdentifier.create(NETCONF_TARGET_QNAME); + public static final @NonNull QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config").intern(); + public static final @NonNull NodeIdentifier NETCONF_CONFIG_NODEID = NodeIdentifier.create(NETCONF_CONFIG_QNAME); + + public static final @NonNull QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit").intern(); + public static final @NonNull SchemaPath NETCONF_COMMIT_PATH = toPath(NETCONF_COMMIT_QNAME); + public static final @NonNull QName NETCONF_VALIDATE_QNAME = QName.create(NETCONF_QNAME, "validate").intern(); + public static final @NonNull NodeIdentifier NETCONF_VALIDATE_NODEID = NodeIdentifier.create(NETCONF_VALIDATE_QNAME); + public static final @NonNull SchemaPath NETCONF_VALIDATE_PATH = toPath(NETCONF_VALIDATE_QNAME); + public static final @NonNull QName NETCONF_COPY_CONFIG_QNAME = QName.create(NETCONF_QNAME, "copy-config").intern(); + public static final @NonNull NodeIdentifier NETCONF_COPY_CONFIG_NODEID = + NodeIdentifier.create(NETCONF_COPY_CONFIG_QNAME); + public static final @NonNull SchemaPath NETCONF_COPY_CONFIG_PATH = toPath(NETCONF_COPY_CONFIG_QNAME); + + public static final @NonNull QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation").intern(); + private static final @NonNull QName NETCONF_OPERATION_QNAME_LEGACY = + NETCONF_OPERATION_QNAME.withoutRevision().intern(); + public static final @NonNull QName NETCONF_DEFAULT_OPERATION_QNAME = + QName.create(NETCONF_OPERATION_QNAME, "default-operation").intern(); + public static final @NonNull NodeIdentifier NETCONF_DEFAULT_OPERATION_NODEID = + NodeIdentifier.create(NETCONF_DEFAULT_OPERATION_QNAME); + public static final @NonNull QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config").intern(); + public static final @NonNull NodeIdentifier NETCONF_EDIT_CONFIG_NODEID = + NodeIdentifier.create(NETCONF_EDIT_CONFIG_QNAME); + public static final @NonNull SchemaPath NETCONF_EDIT_CONFIG_PATH = toPath(NETCONF_EDIT_CONFIG_QNAME); + public static final @NonNull QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config"); + public static final @NonNull NodeIdentifier NETCONF_GET_CONFIG_NODEID = + NodeIdentifier.create(NETCONF_GET_CONFIG_QNAME); + public static final @NonNull SchemaPath NETCONF_GET_CONFIG_PATH = toPath(NETCONF_GET_CONFIG_QNAME); + public static final @NonNull QName NETCONF_DISCARD_CHANGES_QNAME = QName.create(NETCONF_QNAME, "discard-changes"); + public static final @NonNull SchemaPath NETCONF_DISCARD_CHANGES_PATH = toPath(NETCONF_DISCARD_CHANGES_QNAME); + public static final @NonNull QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type").intern(); + public static final @NonNull QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter").intern(); + public static final @NonNull QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get").intern(); + public static final @NonNull NodeIdentifier NETCONF_GET_NODEID = NodeIdentifier.create(NETCONF_GET_QNAME); + public static final @NonNull SchemaPath NETCONF_GET_PATH = toPath(NETCONF_GET_QNAME); + public static final @NonNull QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc").intern(); + public static final QName YANG_QNAME = null; + public static final URI NETCONF_ACTION_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:1"); + public static final String NETCONF_ACTION = "action"; public static final URI NETCONF_ROLLBACK_ON_ERROR_URI = URI .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0"); @@ -115,56 +194,93 @@ public class NetconfMessageTransformUtil { public static final URI NETCONF_RUNNING_WRITABLE_URI = URI .create("urn:ietf:params:netconf:capability:writable-running:1.0"); - public static final QName NETCONF_LOCK_QNAME = QName.create(NETCONF_QNAME, "lock"); - public static final QName NETCONF_UNLOCK_QNAME = QName.create(NETCONF_QNAME, "unlock"); + public static final @NonNull QName NETCONF_LOCK_QNAME = QName.create(NETCONF_QNAME, "lock").intern(); + public static final @NonNull NodeIdentifier NETCONF_LOCK_NODEID = NodeIdentifier.create(NETCONF_LOCK_QNAME); + public static final @NonNull SchemaPath NETCONF_LOCK_PATH = toPath(NETCONF_LOCK_QNAME); + public static final @NonNull QName NETCONF_UNLOCK_QNAME = QName.create(NETCONF_QNAME, "unlock").intern(); + public static final @NonNull NodeIdentifier NETCONF_UNLOCK_NODEID = NodeIdentifier.create(NETCONF_UNLOCK_QNAME); + public static final @NonNull SchemaPath NETCONF_UNLOCK_PATH = toPath(NETCONF_UNLOCK_QNAME); + + public static final @NonNull NodeIdentifier EDIT_CONTENT_NODEID = NodeIdentifier.create(EditContent.QNAME); // Discard changes message - public static final ContainerNode DISCARD_CHANGES_RPC_CONTENT = - Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_DISCARD_CHANGES_QNAME)).build(); + public static final @NonNull ContainerNode DISCARD_CHANGES_RPC_CONTENT = Builders.containerBuilder() + .withNodeIdentifier(NodeIdentifier.create(NETCONF_DISCARD_CHANGES_QNAME)).build(); // Commit changes message - public static final ContainerNode COMMIT_RPC_CONTENT = - Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_COMMIT_QNAME)).build(); + public static final @NonNull ContainerNode COMMIT_RPC_CONTENT = Builders.containerBuilder() + .withNodeIdentifier(NodeIdentifier.create(NETCONF_COMMIT_QNAME)).build(); // Get message - public static final ContainerNode GET_RPC_CONTENT = - Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_GET_QNAME)).build(); + public static final @NonNull ContainerNode GET_RPC_CONTENT = Builders.containerBuilder() + .withNodeIdentifier(NETCONF_GET_NODEID).build(); // Create-subscription changes message - public static final ContainerNode CREATE_SUBSCRIPTION_RPC_CONTENT = - Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CREATE_SUBSCRIPTION_RPC_QNAME)).build(); - - public static final DataContainerChild EMPTY_FILTER; - - static { - final NormalizedNodeAttrBuilder anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME)); - anyXmlBuilder.withAttributes(Collections.singletonMap(NETCONF_TYPE_QNAME, SUBTREE)); - - final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_FILTER_QNAME.getLocalName(), Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString())); - element.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(), "subtree"); + public static final @NonNull ContainerNode CREATE_SUBSCRIPTION_RPC_CONTENT = Builders.containerBuilder() + .withNodeIdentifier(NodeIdentifier.create(CREATE_SUBSCRIPTION_RPC_QNAME)).build(); - anyXmlBuilder.withValue(new DOMSource(element)); + public static final @NonNull SchemaPath CREATE_SUBSCRIPTION_RPC_PATH = toPath(CREATE_SUBSCRIPTION_RPC_QNAME); - EMPTY_FILTER = anyXmlBuilder.build(); - } - - public static DataContainerChild toFilterStructure(final YangInstanceIdentifier identifier, final SchemaContext ctx) { - final NormalizedNodeAttrBuilder anyXmlBuilder = Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_FILTER_QNAME)); - anyXmlBuilder.withAttributes(Collections.singletonMap(NETCONF_TYPE_QNAME, SUBTREE)); + public static final @NonNull NodeIdentifier NETCONF_FILTER_NODEID = NodeIdentifier.create(NETCONF_FILTER_QNAME); - final NormalizedNode filterContent = ImmutableNodes.fromInstanceId(ctx, identifier); - - final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_FILTER_QNAME.getLocalName(), Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString())); - element.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(), "subtree"); + public static final @NonNull AnyxmlNode EMPTY_FILTER = buildFilterStructure(getNetconfFilterElement()); + /** + * Creation of the subtree filter structure using {@link YangInstanceIdentifier} path. + * + * @param identifier parent path / query + * @param ctx mountpoint schema context + * @return created DOM structure with subtree filter + */ + public static AnyxmlNode toFilterStructure(final YangInstanceIdentifier identifier, + final EffectiveModelContext ctx) { + final Element element = getNetconfFilterElement(); try { - NetconfUtil.writeNormalizedNode(filterContent, new DOMResult(element), SchemaPath.ROOT, ctx); + NetconfUtil.writeFilter(identifier, new DOMResult(element), SchemaPath.ROOT, ctx); } catch (IOException | XMLStreamException e) { throw new IllegalStateException("Unable to serialize filter element for path " + identifier, e); } - anyXmlBuilder.withValue(new DOMSource(element)); + return buildFilterStructure(element); + } + + /** + * Creation of the subtree filter structure using list of parent {@link YangInstanceIdentifier} + * and specific selection fields. Field paths are relative to parent query path. + * + * @param fieldsFilters list of: parent path and selection fields + * @param ctx mountpoint schema context + * @return created DOM structure with subtree filter + */ + public static AnyxmlNode toFilterStructure(final List fieldsFilters, + final EffectiveModelContext ctx) { + Preconditions.checkState(!fieldsFilters.isEmpty(), "An empty list of subtree filters is not allowed"); + final Element element = getNetconfFilterElement(); + + for (final FieldsFilter filter : fieldsFilters) { + try { + NetconfUtil.writeFilter(filter.path(), new DOMResult(element), SchemaPath.ROOT, ctx, filter.fields()); + } catch (IOException | XMLStreamException e) { + throw new IllegalStateException(String.format( + "Unable to serialize filter element for path %s with fields: %s", + filter.path(), filter.fields()), e); + } + } + return buildFilterStructure(element); + } - return anyXmlBuilder.build(); + private static Element getNetconfFilterElement() { + final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_FILTER_QNAME.getLocalName(), + Optional.of(NETCONF_FILTER_QNAME.getNamespace().toString())); + element.setAttributeNS(NETCONF_FILTER_QNAME.getNamespace().toString(), NETCONF_TYPE_QNAME.getLocalName(), + SUBTREE); + return element; + } + + private static AnyxmlNode buildFilterStructure(final Element element) { + return Builders.anyXmlBuilder() + .withNodeIdentifier(NETCONF_FILTER_NODEID) + .withValue(new DOMSource(element)) + .build(); } public static void checkValidReply(final NetconfMessage input, final NetconfMessage output) @@ -172,74 +288,44 @@ public class NetconfMessageTransformUtil { final String inputMsgId = input.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR); final String outputMsgId = output.getDocument().getDocumentElement().getAttribute(MESSAGE_ID_ATTR); - if(inputMsgId.equals(outputMsgId) == false) { - final Map errorInfo = ImmutableMap.builder() - .put( "actual-message-id", outputMsgId ) - .put( "expected-message-id", inputMsgId ) - .build(); - - throw new NetconfDocumentedException( "Response message contained unknown \"message-id\"", - null, NetconfDocumentedException.ErrorType.protocol, - NetconfDocumentedException.ErrorTag.bad_attribute, - NetconfDocumentedException.ErrorSeverity.error, errorInfo ); + if (!inputMsgId.equals(outputMsgId)) { + throw new NetconfDocumentedException("Response message contained unknown \"message-id\"", null, + ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, ErrorSeverity.ERROR, + ImmutableMap.of("actual-message-id", outputMsgId, "expected-message-id", inputMsgId)); } } public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException { - if(NetconfMessageUtil.isErrorMessage(output)) { + if (NetconfMessageUtil.isErrorMessage(output)) { throw NetconfDocumentedException.fromXMLDocument(output.getDocument()); } } - public static RpcError toRpcError( final NetconfDocumentedException ex ) { + public static RpcError toRpcError(final NetconfDocumentedException ex) { final StringBuilder infoBuilder = new StringBuilder(); final Map errorInfo = ex.getErrorInfo(); - if(errorInfo != null) { - for( final Entry e: errorInfo.entrySet() ) { - infoBuilder.append( '<' ).append( e.getKey() ).append( '>' ).append( e.getValue() ) - .append( "' ); + if (errorInfo != null) { + for (final Entry e : errorInfo.entrySet()) { + infoBuilder.append('<').append(e.getKey()).append('>').append(e.getValue()) + .append("'); } } - final ErrorSeverity severity = toRpcErrorSeverity( ex.getErrorSeverity() ); - return severity == ErrorSeverity.ERROR ? - RpcResultBuilder.newError( - toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(), - ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() ) : - RpcResultBuilder.newWarning( - toRpcErrorType( ex.getErrorType() ), ex.getErrorTag().getTagValue(), - ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause() ); - } - - private static ErrorSeverity toRpcErrorSeverity( final NetconfDocumentedException.ErrorSeverity severity ) { - switch (severity) { - case warning: - return RpcError.ErrorSeverity.WARNING; - default: - return RpcError.ErrorSeverity.ERROR; - } - } - - private static RpcError.ErrorType toRpcErrorType(final NetconfDocumentedException.ErrorType type) { - switch (type) { - case protocol: - return RpcError.ErrorType.PROTOCOL; - case rpc: - return RpcError.ErrorType.RPC; - case transport: - return RpcError.ErrorType.TRANSPORT; - default: - return RpcError.ErrorType.APPLICATION; - } + return ex.getErrorSeverity() == ErrorSeverity.ERROR + ? RpcResultBuilder.newError(ex.getErrorType().toLegacy(), ex.getErrorTag().elementBody(), + ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause()) + : RpcResultBuilder.newWarning( + ex.getErrorType().toLegacy(), ex.getErrorTag().elementBody(), + ex.getLocalizedMessage(), null, infoBuilder.toString(), ex.getCause()); } - public static YangInstanceIdentifier.NodeIdentifier toId(final YangInstanceIdentifier.PathArgument qname) { - return toId(qname.getNodeType()); + public static NodeIdentifier toId(final PathArgument qname) { + return qname instanceof NodeIdentifier ? (NodeIdentifier) qname : toId(qname.getNodeType()); } - public static YangInstanceIdentifier.NodeIdentifier toId(final QName nodeType) { - return new YangInstanceIdentifier.NodeIdentifier(nodeType); + public static NodeIdentifier toId(final QName nodeType) { + return new NodeIdentifier(nodeType); } public static Element getDataSubtree(final Document doc) { @@ -252,47 +338,221 @@ public class NetconfMessageTransformUtil { || NETCONF_GET_QNAME.getLocalName().equals(rpc.getLocalName())); } - public static ContainerSchemaNode createSchemaForDataRead(final SchemaContext schemaContext) { - return new NodeContainerProxy(NETCONF_DATA_QNAME, schemaContext.getChildNodes()); + @Deprecated + public static @NonNull ContainerNode wrap(final QName name, final DataContainerChild... node) { + return wrap(toId(name), node); + } + + public static @NonNull ContainerNode wrap(final NodeIdentifier name, final DataContainerChild... node) { + return Builders.containerBuilder().withNodeIdentifier(name).withValue(ImmutableList.copyOf(node)).build(); + } + + /** + * 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 operation, final Optional 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(); } - public static ContainerSchemaNode createSchemaForNotification(final NotificationDefinition next) { - return new NodeContainerProxy(next.getQName(), next.getChildNodes(), next.getAvailableAugmentations()); + private static NormalizedMetadata leafMetadata(final YangInstanceIdentifier path, final ModifyAction oper) { + final List args = path.getPathArguments(); + final Deque 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; + } + } } - public static ContainerNode wrap(final QName name, final DataContainerChild... node) { - return Builders.containerBuilder().withNodeIdentifier(toId(name)).withValue(ImmutableList.copyOf(node)).build(); + public static DataContainerChild createEditConfigStructure(final EffectiveModelContext ctx, + final YangInstanceIdentifier dataPath, final Optional operation, + final Optional lastChildOverride) { + return Builders.choiceBuilder().withNodeIdentifier(EDIT_CONTENT_NODEID) + .withChild(createEditConfigAnyxml(ctx, dataPath, operation, lastChildOverride)).build(); } - public static DataContainerChild createEditConfigStructure(final SchemaContext ctx, final YangInstanceIdentifier dataPath, - final Optional operation, final Optional> lastChildOverride) { - final NormalizedNode configContent; + public static @NonNull SchemaPath toPath(final QName rpc) { + return SchemaPath.ROOT.createChild(rpc); + } - 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()); - configContent = lastChildOverride.get(); + public static Map.Entry stripNotification(final NetconfMessage message) { + final XmlElement xmlElement = XmlElement.fromDomDocument(message.getDocument()); + final List childElements = xmlElement.getChildElements(); + Preconditions.checkArgument(childElements.size() == 2, "Unable to parse notification %s, unexpected format." + + "\nExpected 2 childElements, actual childElements size is %s", message, childElements.size()); + + final XmlElement eventTimeElement; + final XmlElement notificationElement; + + if (childElements.get(0).getName().equals(EVENT_TIME)) { + eventTimeElement = childElements.get(0); + notificationElement = childElements.get(1); + } else if (childElements.get(1).getName().equals(EVENT_TIME)) { + eventTimeElement = childElements.get(1); + notificationElement = childElements.get(0); } else { - final Entry modifyOperation = - operation.isPresent() ? new AbstractMap.SimpleEntry<>(NETCONF_OPERATION_QNAME, operation.get()) : null; - configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride, Optional.fromNullable(modifyOperation)); + throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message); } - final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(), Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString())); try { - NetconfUtil.writeNormalizedNode(configContent, new DOMResult(element), SchemaPath.ROOT, ctx); - } catch (IOException | XMLStreamException e) { - throw new IllegalStateException("Unable to serialize edit config content element for path " + dataPath, e); + return new SimpleEntry<>( + NetconfNotification.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()).toInstant(), + notificationElement); + } catch (final DocumentedException e) { + throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message, + e); + } catch (final DateTimeParseException e) { + LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement, + NetconfNotification.UNKNOWN_EVENT_TIME, e); + return new SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME.toInstant(), + notificationElement); } - final DOMSource value = new DOMSource(element); + } + + public static DOMResult prepareDomResultForRpcRequest(final QName rpcQName, final MessageCounter counter) { + final Document document = XmlUtil.newDocument(); + final Element rpcNS = + document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName()); + // set msg id + rpcNS.setAttribute(MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX)); + final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName()); + rpcNS.appendChild(elementNS); + document.appendChild(rpcNS); + return new DOMResult(elementNS); + } + + public static DOMResult prepareDomResultForActionRequest(final DataSchemaContextTree dataSchemaContextTree, + final DOMDataTreeIdentifier domDataTreeIdentifier, final MessageCounter counter, final QName action) { + final Document document = XmlUtil.newDocument(); + final Element rpcNS = + document.createElementNS(NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName()); + // set msg id + rpcNS.setAttribute(MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX)); + + final Element actionNS = document.createElementNS(NETCONF_ACTION_NAMESPACE.toString(), NETCONF_ACTION); + final DataSchemaContextNode rootSchemaContextNode = dataSchemaContextTree.getRoot(); + final Element actionData = prepareActionData(rootSchemaContextNode, actionNS, + domDataTreeIdentifier.getRootIdentifier().getPathArguments().iterator(), document); + + final Element specificActionElement = + document.createElementNS(action.getNamespace().toString(), action.getLocalName()); + actionData.appendChild(specificActionElement); + rpcNS.appendChild(actionNS); + document.appendChild(rpcNS); + return new DOMResult(specificActionElement); + } + + private static Element prepareActionData(final DataSchemaContextNode currentParentSchemaNode, + final Element actionNS, final Iterator iterator, final Document document) { + if (iterator.hasNext()) { + final PathArgument next = iterator.next(); - return Builders.choiceBuilder().withNodeIdentifier(toId(EditContent.QNAME)).withChild( - Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_CONFIG_QNAME)).withValue(value).build()).build(); + final DataSchemaContextNode current = currentParentSchemaNode.getChild(next); + Preconditions.checkArgument(current != null, "Invalid input: schema for argument %s not found", next); + + if (current.isMixin()) { + return prepareActionData(current, actionNS, iterator, document); + } + + final QName actualNS = next.getNodeType(); + final Element actualElement = document.createElementNS(actualNS.getNamespace().toString(), + actualNS.getLocalName()); + if (next instanceof NodeWithValue) { + actualElement.setNodeValue(((NodeWithValue) next).getValue().toString()); + } else if (next instanceof NodeIdentifierWithPredicates) { + for (Entry entry : ((NodeIdentifierWithPredicates) next).entrySet()) { + final Element entryElement = document.createElementNS(entry.getKey().getNamespace().toString(), + entry.getKey().getLocalName()); + entryElement.setTextContent(entry.getValue().toString()); + entryElement.setNodeValue(entry.getValue().toString()); + actualElement.appendChild(entryElement); + } + } + actionNS.appendChild(actualElement); + return prepareActionData(current, actualElement, iterator, document); + } else { + return actionNS; + } } - public static SchemaPath toPath(final QName rpc) { - return SchemaPath.create(true, rpc); + @SuppressWarnings("checkstyle:IllegalCatch") + public static void writeNormalizedOperationInput(final ContainerNode normalized, final DOMResult result, + final Absolute operationPath, final EffectiveModelContext baseNetconfCtx) + throws IOException, XMLStreamException { + final QName inputQName = YangConstants.operationInputQName(operationPath.lastNodeIdentifier().getModule()); + // FIXME: eliminate this conversion + final SchemaPath inputPath = operationPath.asSchemaPath().createChild(inputQName); + + final XMLStreamWriter writer = NetconfUtil.XML_FACTORY.createXMLStreamWriter(result); + try { + try (NormalizedNodeStreamWriter normalizedNodeStreamWriter = + XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, inputPath)) { + try (SchemaOrderedNormalizedNodeWriter normalizedNodeWriter = + new SchemaOrderedNormalizedNodeWriter(normalizedNodeStreamWriter, baseNetconfCtx, inputPath)) { + final Collection value = normalized.body(); + normalizedNodeWriter.write(value); + normalizedNodeWriter.flush(); + } + } + } finally { + try { + writer.close(); + } catch (final Exception e) { + LOG.warn("Unable to close resource properly", e); + } + } } + public static RpcResult toRpcResult(final FailedNetconfMessage message) { + return RpcResultBuilder.failed() + .withRpcError(toRpcError(new NetconfDocumentedException(message.getException().getMessage(), + ErrorType.APPLICATION, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR))) + .build(); + } }