*/
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 org.opendaylight.controller.netconf.api.NetconfDocumentedException;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
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.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.Node;
+import org.opendaylight.yangtools.yang.data.api.SimpleNode;
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;
public class NetconfMessageTransformUtil {
- private NetconfMessageTransformUtil() {
- }
+ public static final String MESSAGE_ID_ATTR = "message-id";
+
+ private NetconfMessageTransformUtil() {}
+
+ public static final QName IETF_NETCONF_MONITORING = QName.create(NetconfState.QNAME, "ietf-netconf-monitoring");
+ 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_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_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 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.<Node<?>>emptyList());
+
+ // Commit changes message
+ public static final CompositeNode COMMIT_RPC_CONTENT =
+ NodeFactory.createImmutableCompositeNode(NETCONF_COMMIT_QNAME, null, Collections.<Node<?>>emptyList());
+
public static Node<?> toFilterStructure(final YangInstanceIdentifier identifier) {
Node<?> previous = null;
if (Iterables.isEmpty(identifier.getPathArguments())) {
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");
+ 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<String,String> errorInfo = ImmutableMap.<String,String>builder()
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());
return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
}
+ /**
+ * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
+ * <pre>
+ * {@code
+ * rpc
+ * get
+ * filter
+ * // All schema nodes from remote schema
+ * filter
+ * get
+ * rpc
+ * }
+ * </pre>
+ *
+ * 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.<DataSchemaNode>newHashSet(configProxy));
+ return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
+ }
+
+ /**
+ * Creates artificial schema node for get rpc. This artificial schema looks like:
+ * <pre>
+ * {@code
+ * rpc
+ * get-config
+ * filter
+ * // All schema nodes from remote schema
+ * filter
+ * get-config
+ * rpc
+ * }
+ * </pre>
+ *
+ * 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.<DataSchemaNode>newHashSet(configProxy));
+ return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
+ }
+
+ /**
+ * Creates artificial schema node for schema defined rpc. This artificial schema looks like:
+ * <pre>
+ * {@code
+ * rpc
+ * rpc-name
+ * // All schema nodes from remote schema
+ * rpc-name
+ * rpc
+ * }
+ * </pre>
+ *
+ * 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.<DataSchemaNode>newHashSet(rpcBodyProxy));
+
+ }
+
public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
if (node != null) {
return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
}
}
+ public static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
+
+ Node<?> current = node;
+ for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
+ if (current instanceof SimpleNode<?>) {
+ return null;
+ } else if (current instanceof CompositeNode) {
+ final CompositeNode currentComposite = (CompositeNode) current;
+
+ current = currentComposite.getFirstCompositeByName(arg.getNodeType());
+ if (current == null) {
+ current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
+ }
+ if (current == null) {
+ current = currentComposite.getFirstSimpleByName(arg.getNodeType());
+ }
+ if (current == null) {
+ current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
+ }
+ if (current == null) {
+ return null;
+ }
+ }
+ }
+ return current;
+ }
}