c51009f5d24f9efb9882c6550201b988a547330d
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / util / NetconfMessageTransformUtil.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.connect.netconf.util;
9
10 import java.net.URI;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Map;
15
16 import javax.annotation.Nullable;
17
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;
35
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;
41
42 public class NetconfMessageTransformUtil {
43
44     private NetconfMessageTransformUtil() {
45     }
46
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,
56             null, null));
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";
73
74     public static Node<?> toFilterStructure(final InstanceIdentifier identifier) {
75         Node<?> previous = null;
76         if (identifier.getPath().isEmpty()) {
77             return null;
78         }
79
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);
84             } else {
85                 previous = toNode(component, previous);
86             }
87         }
88         return filter("subtree", previous);
89     }
90
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()));
95         }
96         if (node != null) {
97             list.add(node);
98         }
99         return new CompositeNodeTOImpl(argument.getNodeType(), null, list);
100     }
101
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");
105
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));
110         }
111     }
112
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())));
116         }
117     }
118
119     public static CompositeNode flattenInput(final CompositeNode node) {
120         final QName inputQName = QName.create(node.getNodeType(), "input");
121         final CompositeNode input = node.getFirstCompositeByName(inputQName);
122         if (input == null)
123             return node;
124         if (input instanceof CompositeNode) {
125
126             final List<Node<?>> nodes = ImmutableList.<Node<?>> builder() //
127                     .addAll(input.getValue()) //
128                     .addAll(Collections2.filter(node.getValue(), new Predicate<Node<?>>() {
129                         @Override
130                         public boolean apply(@Nullable final Node<?> input) {
131                             return input.getNodeType() != inputQName;
132                         }
133                     })) //
134                     .build();
135
136             return ImmutableCompositeNode.create(node.getNodeType(), nodes);
137         }
138
139         return input;
140     }
141
142     static Node<?> toNode(final InstanceIdentifier.PathArgument argument, final Node<?> node) {
143         if (node != null) {
144             return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.<Node<?>> singletonList(node));
145         } else {
146             return new SimpleNodeTOImpl<Void>(argument.getNodeType(), null, null);
147         }
148     }
149
150     public static Element getDataSubtree(final Document doc) {
151         return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
152     }
153
154     public static boolean isDataRetrievalOperation(final QName rpc) {
155         return NETCONF_URI == rpc.getNamespace()
156                 && (rpc.getLocalName().equals(NETCONF_GET_CONFIG_QNAME.getLocalName()) || rpc.getLocalName().equals(
157                         NETCONF_GET_QNAME.getLocalName()));
158     }
159
160     public static boolean isDataEditOperation(final QName rpc) {
161         return NETCONF_URI.equals(rpc.getNamespace())
162                 && rpc.getLocalName().equals(NETCONF_EDIT_CONFIG_QNAME.getLocalName());
163     }
164
165     /**
166      * Creates artificial schema node for edit-config rpc. This artificial schema looks like:
167      * <pre>
168      * {@code
169      * rpc
170      *   edit-config
171      *     config
172      *         // All schema nodes from remote schema
173      *     config
174      *   edit-config
175      * rpc
176      * }
177      * </pre>
178      *
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.
181      */
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));
188     }
189
190     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
191         if (node != null) {
192             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
193         } else {
194             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> emptyList());
195         }
196     }
197
198     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> additional, final Node<?> node) {
199         if (node != null) {
200             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
201         } else {
202             return new CompositeNodeTOImpl(name, null, ImmutableList.<Node<?>> of(additional));
203         }
204     }
205
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);
210         if (node != null) {
211             return it.add(node).toInstance();
212         } else {
213             return it.toInstance();
214         }
215     }
216
217 }