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.codec.xml.XmlDocumentUtils;
30 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
34 import com.google.common.base.Predicate;
35 import com.google.common.collect.Collections2;
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.Lists;
39 public class NetconfMessageTransformUtil {
41 private NetconfMessageTransformUtil() {
44 public static final QName IETF_NETCONF_MONITORING = QName.create(
45 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "2010-10-04", "ietf-netconf-monitoring");
46 public static URI NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0");
47 public static QName NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf");
48 public static QName NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
49 public static QName NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
50 public static QName NETCONF_ERROR_OPTION_QNAME = QName.create(NETCONF_QNAME, "error-option");
51 public static QName NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
52 static List<Node<?>> RUNNING = Collections.<Node<?>> singletonList(new SimpleNodeTOImpl<>(NETCONF_RUNNING_QNAME,
54 public static QName NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
55 public static CompositeNode CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME, null, RUNNING);
56 public static QName NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate");
57 public static QName NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target");
58 public static QName NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config");
59 public static QName NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit");
60 public static QName NETCONF_OPERATION_QNAME = QName.create(NETCONF_QNAME, "operation");
61 public static QName NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config");
62 public static QName NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
63 public static QName NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
64 public static QName NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
65 public static QName NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
66 public static QName NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
67 public static URI NETCONF_ROLLBACK_ON_ERROR_URI = URI
68 .create("urn:ietf:params:netconf:capability:rollback-on-error:1.0");
69 public static String ROLLBACK_ON_ERROR_OPTION = "rollback-on-error";
71 public static Node<?> toFilterStructure(final InstanceIdentifier identifier) {
72 Node<?> previous = null;
73 if (identifier.getPath().isEmpty()) {
77 for (final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument component : Lists
78 .reverse(identifier.getPath())) {
79 if (component instanceof InstanceIdentifier.NodeIdentifierWithPredicates) {
80 previous = toNode((InstanceIdentifier.NodeIdentifierWithPredicates)component, previous);
82 previous = toNode(component, previous);
85 return filter("subtree", previous);
88 static Node<?> toNode(final InstanceIdentifier.NodeIdentifierWithPredicates argument, final Node<?> node) {
89 final List<Node<?>> list = new ArrayList<>();
90 for (final Map.Entry<QName, Object> arg : argument.getKeyValues().entrySet()) {
91 list.add(new SimpleNodeTOImpl(arg.getKey(), null, arg.getValue()));
96 return new CompositeNodeTOImpl(argument.getNodeType(), null, list);
99 public static void checkValidReply(final NetconfMessage input, final NetconfMessage output) {
100 final String inputMsgId = input.getDocument().getDocumentElement().getAttribute("message-id");
101 final String outputMsgId = output.getDocument().getDocumentElement().getAttribute("message-id");
103 if(inputMsgId.equals(outputMsgId) == false) {
104 final String requestXml = XmlUtil.toString(input.getDocument());
105 final String responseXml = XmlUtil.toString(output.getDocument());
106 throw new IllegalStateException(String.format("Rpc request and reply message IDs must be same. Request: %s, response: %s", requestXml, responseXml));
110 public static void checkSuccessReply(final NetconfMessage output) throws NetconfDocumentedException {
111 if(NetconfMessageUtil.isErrorMessage(output)) {
112 throw new IllegalStateException(String.format("Response contains error: %s", XmlUtil.toString(output.getDocument())));
116 public static CompositeNode flattenInput(final CompositeNode node) {
117 final QName inputQName = QName.create(node.getNodeType(), "input");
118 final CompositeNode input = node.getFirstCompositeByName(inputQName);
121 if (input instanceof CompositeNode) {
123 final List<Node<?>> nodes = ImmutableList.<Node<?>> builder() //
124 .addAll(input.getValue()) //
125 .addAll(Collections2.filter(node.getValue(), new Predicate<Node<?>>() {
127 public boolean apply(@Nullable final Node<?> input) {
128 return input.getNodeType() != inputQName;
133 return ImmutableCompositeNode.create(node.getNodeType(), nodes);
139 static Node<?> toNode(final InstanceIdentifier.PathArgument argument, final Node<?> node) {
141 return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.<Node<?>> singletonList(node));
143 return new SimpleNodeTOImpl<Void>(argument.getNodeType(), null, null);
147 public static Element getDataSubtree(final Document doc) {
148 return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
151 public static boolean isDataRetrievalOperation(final QName rpc) {
152 return NETCONF_URI == rpc.getNamespace()
153 && (rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()) || rpc.getLocalName().equals(
154 NETCONF_GET_QNAME.getLocalName()));
157 public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
159 return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
161 return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> emptyList());
165 public static CompositeNodeTOImpl wrap(final QName name, final Node<?> additional, final Node<?> node) {
167 return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
169 return new CompositeNodeTOImpl(name, null, ImmutableList.<Node<?>> of(additional));
173 static ImmutableCompositeNode filter(final String type, final Node<?> node) {
174 final CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder(); //
175 it.setQName(NETCONF_FILTER_QNAME);
176 it.setAttribute(NETCONF_TYPE_QNAME, type);
178 return it.add(node).toInstance();
180 return it.toInstance();