X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fconnect%2Fnetconf%2Futil%2FNetconfMessageTransformUtil.java;h=f1586b1e496fa375a94c23ba6ff7228e7800b4e1;hb=071a641d7c12c0e6112d5ce0afe806b54f116ed2;hp=08a5822d366d16fa5d078e17ae8af2065cdba30e;hpb=81bbe76bd26399118d028663d08e464ce6b7d040;p=controller.git diff --git a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/util/NetconfMessageTransformUtil.java b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/util/NetconfMessageTransformUtil.java index 08a5822d36..f1586b1e49 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/util/NetconfMessageTransformUtil.java +++ b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/util/NetconfMessageTransformUtil.java @@ -7,112 +7,186 @@ */ package org.opendaylight.controller.sal.connect.netconf.util; +import com.google.common.base.Optional; +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.ArrayList; +import java.util.AbstractMap; import java.util.Collections; -import java.util.List; import java.util.Map; import java.util.Map.Entry; - -import javax.annotation.Nullable; - +import javax.xml.stream.XMLOutputFactory; +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.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil; -import org.opendaylight.controller.sal.common.util.RpcErrors; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +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.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl; -import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; -import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl; -import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.opendaylight.yangtools.yang.data.api.ModifyAction; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +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.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.api.schema.stream.NormalizedNodeWriter; +import org.opendaylight.yangtools.yang.data.impl.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.model.api.SchemaPath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; -import com.google.common.base.Predicate; -import com.google.common.collect.Collections2; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; - public class NetconfMessageTransformUtil { - private NetconfMessageTransformUtil() { + private static final Logger LOG= LoggerFactory.getLogger(NetconfMessageTransformUtil.class); + + public static final String MESSAGE_ID_ATTR = "message-id"; + public static final XMLOutputFactory XML_FACTORY; + + static { + XML_FACTORY = XMLOutputFactory.newFactory(); + XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false); } - public static final QName IETF_NETCONF_MONITORING = QName.create( - "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "2010-10-04", "ietf-netconf-monitoring"); - public static URI NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"); - public static QName NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf"); - public static QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data"); - public static QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply"); - public static QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option"); - public static QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running"); - static List> RUNNING = Collections.> singletonList(new SimpleNodeTOImpl<>(NETCONF_RUNNING_QNAME, - null, null)); - public static QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source"); - public static CompositeNode CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME, null, RUNNING); - public static QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate"); - public static QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target"); - public static QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config"); - public static QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit"); - public static QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation"); - public static QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config"); - public static QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config"); - public static QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type"); - public static QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter"); - public static QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get"); - public static QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc"); - public static URI NETCONF_ROLLBACK_ON_ERROR_URI = URI + public static final QName CREATE_SUBSCRIPTION_RPC_QNAME = QName.cachedReference(QName.create(CreateSubscriptionInput.QNAME, "create-subscription")); + 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"); + + public static final URI NETCONF_ROLLBACK_ON_ERROR_URI = URI .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0"); - public static String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error"; + public static final String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error"; - public static Node toFilterStructure(final InstanceIdentifier identifier) { - Node previous = null; - if (identifier.getPath().isEmpty()) { - return null; - } + public static final URI NETCONF_CANDIDATE_URI = URI + .create("urn:ietf:params:netconf:capability:candidate:1.0"); - for (final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument component : Lists - .reverse(identifier.getPath())) { - if (component instanceof InstanceIdentifier.NodeIdentifierWithPredicates) { - previous = toNode((InstanceIdentifier.NodeIdentifierWithPredicates)component, previous); - } else { - previous = toNode(component, previous); - } - } - return filter("subtree", previous); + public static final URI NETCONF_NOTIFICATONS_URI = URI + .create("urn:ietf:params:netconf:capability:notification:1.0"); + + 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"); + + // Discard changes message + public static final ContainerNode DISCARD_CHANGES_RPC_CONTENT = + Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(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(); + + // Get message + public static final ContainerNode GET_RPC_CONTENT = + Builders.containerBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NETCONF_GET_QNAME)).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"); + + anyXmlBuilder.withValue(new DOMSource(element)); + + EMPTY_FILTER = anyXmlBuilder.build(); } - static Node toNode(final InstanceIdentifier.NodeIdentifierWithPredicates argument, final Node node) { - final List> list = new ArrayList<>(); - for (final Map.Entry arg : argument.getKeyValues().entrySet()) { - list.add(new SimpleNodeTOImpl(arg.getKey(), null, arg.getValue())); - } - if (node != null) { - list.add(node); + 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)); + + 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"); + + try { + writeNormalizedNode(filterContent, new DOMResult(element), SchemaPath.ROOT, ctx); + } catch (IOException | XMLStreamException e) { + throw new IllegalStateException("Unable to serialize filter element for path " + identifier, e); } - return new CompositeNodeTOImpl(argument.getNodeType(), null, list); + anyXmlBuilder.withValue(new DOMSource(element)); + + return anyXmlBuilder.build(); } public static void checkValidReply(final NetconfMessage input, final NetconfMessage output) - throws NetconfDocumentedException { - final String inputMsgId = input.getDocument().getDocumentElement().getAttribute("message-id"); - final String outputMsgId = output.getDocument().getDocumentElement().getAttribute("message-id"); + throws NetconfDocumentedException { + 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) { - Map errorInfo = ImmutableMap.builder() - .put( "actual-message-id", outputMsgId ) - .put( "expected-message-id", inputMsgId ) - .build(); + 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, @@ -123,80 +197,59 @@ public class NetconfMessageTransformUtil { public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException { if(NetconfMessageUtil.isErrorMessage(output)) { - throw NetconfDocumentedException.fromXMLDocument( output.getDocument() ); + throw NetconfDocumentedException.fromXMLDocument(output.getDocument()); } } - public static RpcError toRpcError( NetconfDocumentedException ex ) - { - StringBuilder infoBuilder = new StringBuilder(); - Map errorInfo = ex.getErrorInfo(); - if( errorInfo != null ) - { - for( Entry e: errorInfo.entrySet() ) { + 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( "' ); + .append( "' ); } } - return RpcErrors.getRpcError( null, ex.getErrorTag().getTagValue(), infoBuilder.toString(), - toRpcErrorSeverity( ex.getErrorSeverity() ), ex.getLocalizedMessage(), - toRpcErrorType( ex.getErrorType() ), ex.getCause() ); + 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( NetconfDocumentedException.ErrorSeverity severity ) { - switch( severity ) { - case warning: - return RpcError.ErrorSeverity.WARNING; - default: - return RpcError.ErrorSeverity.ERROR; + 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( 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; + 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; } } - public static CompositeNode flattenInput(final CompositeNode node) { - final QName inputQName = QName.create(node.getNodeType(), "input"); - final CompositeNode input = node.getFirstCompositeByName(inputQName); - if (input == null) - return node; - if (input instanceof CompositeNode) { - - final List> nodes = ImmutableList.> builder() // - .addAll(input.getValue()) // - .addAll(Collections2.filter(node.getValue(), new Predicate>() { - @Override - public boolean apply(@Nullable final Node input) { - return input.getNodeType() != inputQName; - } - })) // - .build(); - - return ImmutableCompositeNode.create(node.getNodeType(), nodes); - } - - return input; + public static YangInstanceIdentifier.NodeIdentifier toId(final YangInstanceIdentifier.PathArgument qname) { + return toId(qname.getNodeType()); } - static Node toNode(final InstanceIdentifier.PathArgument argument, final Node node) { - if (node != null) { - return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.> singletonList(node)); - } else { - return new SimpleNodeTOImpl(argument.getNodeType(), null, null); - } + public static YangInstanceIdentifier.NodeIdentifier toId(final QName nodeType) { + return new YangInstanceIdentifier.NodeIdentifier(nodeType); } public static Element getDataSubtree(final Document doc) { @@ -205,65 +258,81 @@ public class NetconfMessageTransformUtil { public static boolean isDataRetrievalOperation(final QName rpc) { return NETCONF_URI.equals(rpc.getNamespace()) - && (rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()) || rpc.getLocalName().equals( - NETCONF_GET_QNAME.getLocalName())); + && (NETCONF_GET_CONFIG_QNAME.getLocalName().equals(rpc.getLocalName()) + || NETCONF_GET_QNAME.getLocalName().equals(rpc.getLocalName())); } - public static boolean isDataEditOperation(final QName rpc) { - return NETCONF_URI.equals(rpc.getNamespace()) - && rpc.getLocalName().equals(NETCONF_EDIT_CONFIG_QNAME.getLocalName()); + public static ContainerSchemaNode createSchemaForDataRead(final SchemaContext schemaContext) { + return new NodeContainerProxy(NETCONF_DATA_QNAME, schemaContext.getChildNodes()); } - /** - * Creates artificial schema node for edit-config rpc. This artificial schema looks like: - *
-     * {@code
-     * rpc
-     *   edit-config
-     *     config
-     *         // All schema nodes from remote schema
-     *     config
-     *   edit-config
-     * rpc
-     * }
-     * 
- * - * This makes the translation of rpc edit-config request(especially the config node) - * to xml use schema which is crucial for some types of nodes e.g. identity-ref. - */ - public static DataNodeContainer createSchemaForEdit(final SchemaContext schemaContext) { - final QName config = QName.create(NETCONF_EDIT_CONFIG_QNAME, "config"); - final QName editConfig = QName.create(NETCONF_EDIT_CONFIG_QNAME, "edit-config"); - final NodeContainerProxy configProxy = new NodeContainerProxy(config, schemaContext.getChildNodes()); - final NodeContainerProxy editConfigProxy = new NodeContainerProxy(editConfig, Sets.newHashSet(configProxy)); - return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.newHashSet(editConfigProxy)); + public static ContainerSchemaNode createSchemaForNotification(final NotificationDefinition next) { + return new NodeContainerProxy(next.getQName(), next.getChildNodes(), next.getAvailableAugmentations()); } - public static CompositeNodeTOImpl wrap(final QName name, final Node node) { - if (node != null) { - return new CompositeNodeTOImpl(name, null, Collections.> singletonList(node)); - } else { - return new CompositeNodeTOImpl(name, null, Collections.> emptyList()); - } + public static ContainerNode wrap(final QName name, final DataContainerChild... node) { + return Builders.containerBuilder().withNodeIdentifier(toId(name)).withValue(ImmutableList.copyOf(node)).build(); } - public static CompositeNodeTOImpl wrap(final QName name, final Node additional, final Node node) { - if (node != null) { - return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node)); + public static DataContainerChild createEditConfigStructure(final SchemaContext ctx, final YangInstanceIdentifier dataPath, + final Optional operation, final Optional> lastChildOverride) { + final NormalizedNode configContent; + + 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(); } else { - return new CompositeNodeTOImpl(name, null, ImmutableList.> of(additional)); + final Entry modifyOperation = + operation.isPresent() ? new AbstractMap.SimpleEntry<>(NETCONF_OPERATION_QNAME, operation.get()) : null; + configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride, Optional.fromNullable(modifyOperation)); } - } - static ImmutableCompositeNode filter(final String type, final Node node) { - final CompositeNodeBuilder it = ImmutableCompositeNode.builder(); // - it.setQName(NETCONF_FILTER_QNAME); - it.setAttribute(NETCONF_TYPE_QNAME, type); - if (node != null) { - return it.add(node).toInstance(); - } else { - return it.toInstance(); + final Element element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(), Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString())); + try { + 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); } + final DOMSource value = new DOMSource(element); + + return Builders.choiceBuilder().withNodeIdentifier(toId(EditContent.QNAME)).withChild( + Builders.anyXmlBuilder().withNodeIdentifier(toId(NETCONF_CONFIG_QNAME)).withValue(value).build()).build(); } + public static SchemaPath toPath(final QName rpc) { + return SchemaPath.create(true, rpc); + } + + // FIXME similar code is in netconf-notifications-impl , DRY + public static void writeNormalizedNode(final NormalizedNode normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext context) + throws IOException, XMLStreamException { + NormalizedNodeWriter normalizedNodeWriter = null; + NormalizedNodeStreamWriter normalizedNodeStreamWriter = null; + XMLStreamWriter writer = null; + try { + writer = XML_FACTORY.createXMLStreamWriter(result); + normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath); + normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter); + + normalizedNodeWriter.write(normalized); + + normalizedNodeWriter.flush(); + } finally { + try { + if(normalizedNodeWriter != null) { + normalizedNodeWriter.close(); + } + if(normalizedNodeStreamWriter != null) { + normalizedNodeStreamWriter.close(); + } + if(writer != null) { + writer.close(); + } + } catch (final Exception e) { + LOG.warn("Unable to close resource properly", e); + } + } + } }