X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fconnect%2Fnetconf%2Futil%2FNetconfMessageTransformUtil.java;h=4f792a0a7169b00c72672d182393bd011ad1ca74;hp=08a5822d366d16fa5d078e17ae8af2065cdba30e;hb=1bf9f8c060f6adf32f527f6efe82f78f774ffd8d;hpb=daa1d3344b3394accd85152b6f0a2fcda8491759 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..4f792a0a71 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,6 +7,14 @@ */ package org.opendaylight.controller.sal.connect.netconf.util; +import com.google.common.base.Preconditions; +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.Iterables; +import com.google.common.collect.Sets; + import java.net.URI; import java.util.ArrayList; import java.util.Collections; @@ -19,15 +27,16 @@ import javax.annotation.Nullable; 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.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.RpcResultBuilder; import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; 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.NodeFactory; 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; @@ -36,28 +45,18 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; 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 NetconfMessageTransformUtil() {} - 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 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)); + 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"); @@ -65,26 +64,39 @@ public class NetconfMessageTransformUtil { 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_DEFAULT_OPERATION_QNAME = QName.create(NETCONF_OPERATION_QNAME, "default-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_DISCARD_CHANGES_QNAME = QName.create(NETCONF_QNAME, "discard-changes"); 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 .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0"); public static String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error"; - public static Node toFilterStructure(final InstanceIdentifier identifier) { + public static URI NETCONF_CANDIDATE_URI = URI + .create("urn:ietf:params:netconf:capability:candidate:1.0"); + + // Discard changes message + public static final CompositeNode DISCARD_CHANGES_RPC_CONTENT = + NodeFactory.createImmutableCompositeNode(NETCONF_DISCARD_CHANGES_QNAME, null, Collections.>emptyList()); + + // Commit changes message + public static final CompositeNode COMMIT_RPC_CONTENT = + NodeFactory.createImmutableCompositeNode(NETCONF_COMMIT_QNAME, null, Collections.>emptyList()); + + public static Node toFilterStructure(final YangInstanceIdentifier identifier) { Node previous = null; - if (identifier.getPath().isEmpty()) { + if (Iterables.isEmpty(identifier.getPathArguments())) { return null; } - 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); + for (final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument component : identifier.getReversePathArguments()) { + if (component instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) { + previous = toNode((YangInstanceIdentifier.NodeIdentifierWithPredicates)component, previous); } else { previous = toNode(component, previous); } @@ -92,7 +104,7 @@ public class NetconfMessageTransformUtil { return filter("subtree", previous); } - static Node toNode(final InstanceIdentifier.NodeIdentifierWithPredicates argument, final Node node) { + static Node toNode(final YangInstanceIdentifier.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())); @@ -104,15 +116,15 @@ public class NetconfMessageTransformUtil { } public static void checkValidReply(final NetconfMessage input, final NetconfMessage output) - throws NetconfDocumentedException { + throws NetconfDocumentedException { final String inputMsgId = input.getDocument().getDocumentElement().getAttribute("message-id"); final String outputMsgId = output.getDocument().getDocumentElement().getAttribute("message-id"); if(inputMsgId.equals(outputMsgId) == false) { Map errorInfo = ImmutableMap.builder() - .put( "actual-message-id", outputMsgId ) - .put( "expected-message-id", inputMsgId ) - .build(); + .put( "actual-message-id", outputMsgId ) + .put( "expected-message-id", inputMsgId ) + .build(); throw new NetconfDocumentedException( "Response message contained unknown \"message-id\"", null, NetconfDocumentedException.ErrorType.protocol, @@ -127,7 +139,7 @@ public class NetconfMessageTransformUtil { } } - public static RpcError toRpcError( NetconfDocumentedException ex ) + public static RpcError toRpcError( final NetconfDocumentedException ex ) { StringBuilder infoBuilder = new StringBuilder(); Map errorInfo = ex.getErrorInfo(); @@ -135,44 +147,50 @@ public class NetconfMessageTransformUtil { { for( 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() ); + 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 ) { + private static ErrorSeverity toRpcErrorSeverity( final NetconfDocumentedException.ErrorSeverity severity ) { switch( severity ) { - case warning: - return RpcError.ErrorSeverity.WARNING; - default: - return RpcError.ErrorSeverity.ERROR; + case warning: + return RpcError.ErrorSeverity.WARNING; + default: + return RpcError.ErrorSeverity.ERROR; } } - private static RpcError.ErrorType toRpcErrorType( NetconfDocumentedException.ErrorType type ) + 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; + 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) + if (input == null) { return node; + } if (input instanceof CompositeNode) { final List> nodes = ImmutableList.> builder() // @@ -180,7 +198,7 @@ public class NetconfMessageTransformUtil { .addAll(Collections2.filter(node.getValue(), new Predicate>() { @Override public boolean apply(@Nullable final Node input) { - return input.getNodeType() != inputQName; + return !inputQName.equals(input.getNodeType()); } })) // .build(); @@ -191,7 +209,7 @@ public class NetconfMessageTransformUtil { return input; } - static Node toNode(final InstanceIdentifier.PathArgument argument, final Node node) { + static Node toNode(final YangInstanceIdentifier.PathArgument argument, final Node node) { if (node != null) { return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.> singletonList(node)); } else { @@ -209,6 +227,14 @@ public class NetconfMessageTransformUtil { NETCONF_GET_QNAME.getLocalName())); } + public static boolean isGetOperation(final QName rpc) { + return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_GET_QNAME.getLocalName()); + } + + public static boolean isGetConfigOperation(final QName rpc) { + return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()); + } + public static boolean isDataEditOperation(final QName rpc) { return NETCONF_URI.equals(rpc.getNamespace()) && rpc.getLocalName().equals(NETCONF_EDIT_CONFIG_QNAME.getLocalName()); @@ -239,6 +265,80 @@ public class NetconfMessageTransformUtil { return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.newHashSet(editConfigProxy)); } + /** + * Creates artificial schema node for edit-config rpc. This artificial schema looks like: + *
+     * {@code
+     * rpc
+     *   get
+     *     filter
+     *         // All schema nodes from remote schema
+     *     filter
+     *   get
+     * rpc
+     * }
+     * 
+ * + * This makes the translation of rpc get request(especially the config node) + * to xml use schema which is crucial for some types of nodes e.g. identity-ref. + */ + public static DataNodeContainer createSchemaForGet(final SchemaContext schemaContext) { + final QName filter = QName.create(NETCONF_GET_QNAME, "filter"); + final QName get = QName.create(NETCONF_GET_QNAME, "get"); + final NodeContainerProxy configProxy = new NodeContainerProxy(filter, schemaContext.getChildNodes()); + final NodeContainerProxy editConfigProxy = new NodeContainerProxy(get, Sets.newHashSet(configProxy)); + return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.newHashSet(editConfigProxy)); + } + + /** + * Creates artificial schema node for get rpc. This artificial schema looks like: + *
+     * {@code
+     * rpc
+     *   get-config
+     *     filter
+     *         // All schema nodes from remote schema
+     *     filter
+     *   get-config
+     * rpc
+     * }
+     * 
+ * + * This makes the translation of rpc get-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 createSchemaForGetConfig(final SchemaContext schemaContext) { + final QName filter = QName.create(NETCONF_GET_CONFIG_QNAME, "filter"); + final QName getConfig = QName.create(NETCONF_GET_CONFIG_QNAME, "get-config"); + final NodeContainerProxy configProxy = new NodeContainerProxy(filter, schemaContext.getChildNodes()); + final NodeContainerProxy editConfigProxy = new NodeContainerProxy(getConfig, Sets.newHashSet(configProxy)); + return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.newHashSet(editConfigProxy)); + } + + /** + * Creates artificial schema node for schema defined rpc. This artificial schema looks like: + *
+     * {@code
+     * rpc
+     *   rpc-name
+     *      // All schema nodes from remote schema
+     *   rpc-name
+     * rpc
+     * }
+     * 
+ * + * This makes the translation of schema defined rpc request + * to xml use schema which is crucial for some types of nodes e.g. identity-ref. + */ + public static DataNodeContainer createSchemaForRpc(final QName rpcName, final SchemaContext schemaContext) { + Preconditions.checkNotNull(rpcName); + Preconditions.checkNotNull(schemaContext); + + final NodeContainerProxy rpcBodyProxy = new NodeContainerProxy(rpcName, schemaContext.getChildNodes()); + return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.newHashSet(rpcBodyProxy)); + + } + public static CompositeNodeTOImpl wrap(final QName name, final Node node) { if (node != null) { return new CompositeNodeTOImpl(name, null, Collections.> singletonList(node)); @@ -265,5 +365,4 @@ public class NetconfMessageTransformUtil { return it.toInstance(); } } - }