2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.connect.netconf.util;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
16 import javax.annotation.Nullable;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.controller.netconf.api.NetconfMessage;
20 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.Node;
26 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
27 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
28 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
29 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
36 import com.google.common.base.Predicate;
37 import com.google.common.collect.Collections2;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.Lists;
40 import com.google.common.collect.Sets;
42 public class NetconfMessageTransformUtil {
44 private NetconfMessageTransformUtil() {
47 public static final QName IETF_NETCONF_MONITORING = QName.create(
48 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "2010-10-04", "ietf-netconf-monitoring");
49 public static URI NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0");
50 public static QName NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf");
51 public static QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
52 public static QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
53 public static QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option");
54 public static QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
55 static List<Node<?>> RUNNING = Collections.<Node<?>> singletonList(new SimpleNodeTOImpl<>(NETCONF_RUNNING_QNAME,
57 public static QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
58 public static CompositeNode CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME, null, RUNNING);
59 public static QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate");
60 public static QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target");
61 public static QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config");
62 public static QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit");
63 public static QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation");
64 public static QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config");
65 public static QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
66 public static QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
67 public static QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
68 public static QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
69 public static QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
70 public static URI NETCONF_ROLLBACK_ON_ERROR_URI = URI
71 .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0");
72 public static String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error";
74 public static Node<?> toFilterStructure(final InstanceIdentifier identifier) {
75 Node<?> previous = null;
76 if (identifier.getPath().isEmpty()) {
80 for (final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument component : Lists
81 .reverse(identifier.getPath())) {
82 if (component instanceof InstanceIdentifier.NodeIdentifierWithPredicates) {
83 previous = toNode((InstanceIdentifier.NodeIdentifierWithPredicates)component, previous);
85 previous = toNode(component, previous);
88 return filter("subtree", previous);
91 static Node<?> toNode(final InstanceIdentifier.NodeIdentifierWithPredicates argument, final Node<?> node) {
92 final List<Node<?>> list = new ArrayList<>();
93 for (final Map.Entry<QName, Object> arg : argument.getKeyValues().entrySet()) {
94 list.add(new SimpleNodeTOImpl(arg.getKey(), null, arg.getValue()));
99 return new CompositeNodeTOImpl(argument.getNodeType(), null, list);
102 public static void checkValidReply(final NetconfMessage input, final NetconfMessage output) {
103 final String inputMsgId = input.getDocument().getDocumentElement().getAttribute("message-id");
104 final String outputMsgId = output.getDocument().getDocumentElement().getAttribute("message-id");
106 if(inputMsgId.equals(outputMsgId) == false) {
107 final String requestXml = XmlUtil.toString(input.getDocument());
108 final String responseXml = XmlUtil.toString(output.getDocument());
109 throw new IllegalStateException(String.format("Rpc request and reply message IDs must be same. Request: %s, response: %s", requestXml, responseXml));
113 public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException {
114 if(NetconfMessageUtil.isErrorMessage(output)) {
115 throw new IllegalStateException(String.format("Response contains error: %s", XmlUtil.toString(output.getDocument())));
119 public static CompositeNode flattenInput(final CompositeNode node) {
120 final QName inputQName = QName.create(node.getNodeType(), "input");
121 final CompositeNode input = node.getFirstCompositeByName(inputQName);
124 if (input instanceof CompositeNode) {
126 final List<Node<?>> nodes = ImmutableList.<Node<?>> builder() //
127 .addAll(input.getValue()) //
128 .addAll(Collections2.filter(node.getValue(), new Predicate<Node<?>>() {
130 public boolean apply(@Nullable final Node<?> input) {
131 return input.getNodeType() != inputQName;
136 return ImmutableCompositeNode.create(node.getNodeType(), nodes);
142 static Node<?> toNode(final InstanceIdentifier.PathArgument argument, final Node<?> node) {
144 return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.<Node<?>> singletonList(node));
146 return new SimpleNodeTOImpl<Void>(argument.getNodeType(), null, null);
150 public static Element getDataSubtree(final Document doc) {
151 return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
154 public static boolean isDataRetrievalOperation(final QName rpc) {
155 return NETCONF_URI.equals(rpc.getNamespace())
156 && (rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()) || rpc.getLocalName().equals(
157 NETCONF_GET_QNAME.getLocalName()));
160 public static boolean isDataEditOperation(final QName rpc) {
161 return NETCONF_URI.equals(rpc.getNamespace())
162 && rpc.getLocalName().equals(NETCONF_EDIT_CONFIG_QNAME.getLocalName());
166 * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
172 * // All schema nodes from remote schema
179 * This makes the translation of rpc edit-config request(especially the config node)
180 * to xml use schema which is crucial for some types of nodes e.g. identity-ref.
182 public static DataNodeContainer createSchemaForEdit(final SchemaContext schemaContext) {
183 final QName config = QName.create(NETCONF_EDIT_CONFIG_QNAME, "config");
184 final QName editConfig = QName.create(NETCONF_EDIT_CONFIG_QNAME, "edit-config");
185 final NodeContainerProxy configProxy = new NodeContainerProxy(config, schemaContext.getChildNodes());
186 final NodeContainerProxy editConfigProxy = new NodeContainerProxy(editConfig, Sets.<DataSchemaNode>newHashSet(configProxy));
187 return new NodeContainerProxy(NETCONF_RPC_QNAME, Sets.<DataSchemaNode>newHashSet(editConfigProxy));
190 public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
192 return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
194 return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> emptyList());
198 public static CompositeNodeTOImpl wrap(final QName name, final Node<?> additional, final Node<?> node) {
200 return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
202 return new CompositeNodeTOImpl(name, null, ImmutableList.<Node<?>> of(additional));
206 static ImmutableCompositeNode filter(final String type, final Node<?> node) {
207 final CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder(); //
208 it.setQName(NETCONF_FILTER_QNAME);
209 it.setAttribute(NETCONF_TYPE_QNAME, type);
211 return it.add(node).toInstance();
213 return it.toInstance();