Merge "TCP_Flag extension model additions for OFPXMC_NXM_1 class"
[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
15 import java.util.Map;
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.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;
33
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;
38
39 public class NetconfMessageTransformUtil {
40
41     private NetconfMessageTransformUtil() {
42     }
43
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,
53             null, null));
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";
70
71     public static Node<?> toFilterStructure(final InstanceIdentifier identifier) {
72         Node<?> previous = null;
73         if (identifier.getPath().isEmpty()) {
74             return null;
75         }
76
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);
81             } else {
82                 previous = toNode(component, previous);
83             }
84         }
85         return filter("subtree", previous);
86     }
87
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()));
92         }
93         if (node != null) {
94             list.add(node);
95         }
96         return new CompositeNodeTOImpl(argument.getNodeType(), null, list);
97     }
98
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");
102
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));
107         }
108     }
109
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())));
113         }
114     }
115
116     public static CompositeNode flattenInput(final CompositeNode node) {
117         final QName inputQName = QName.create(node.getNodeType(), "input");
118         final CompositeNode input = node.getFirstCompositeByName(inputQName);
119         if (input == null)
120             return node;
121         if (input instanceof CompositeNode) {
122
123             final List<Node<?>> nodes = ImmutableList.<Node<?>> builder() //
124                     .addAll(input.getValue()) //
125                     .addAll(Collections2.filter(node.getValue(), new Predicate<Node<?>>() {
126                         @Override
127                         public boolean apply(@Nullable final Node<?> input) {
128                             return input.getNodeType() != inputQName;
129                         }
130                     })) //
131                     .build();
132
133             return ImmutableCompositeNode.create(node.getNodeType(), nodes);
134         }
135
136         return input;
137     }
138
139     static Node<?> toNode(final InstanceIdentifier.PathArgument argument, final Node<?> node) {
140         if (node != null) {
141             return new CompositeNodeTOImpl(argument.getNodeType(), null, Collections.<Node<?>> singletonList(node));
142         } else {
143             return new SimpleNodeTOImpl<Void>(argument.getNodeType(), null, null);
144         }
145     }
146
147     public static Element getDataSubtree(final Document doc) {
148         return (Element) doc.getElementsByTagNameNS(NETCONF_URI.toString(), "data").item(0);
149     }
150
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()));
155     }
156
157     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> node) {
158         if (node != null) {
159             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> singletonList(node));
160         } else {
161             return new CompositeNodeTOImpl(name, null, Collections.<Node<?>> emptyList());
162         }
163     }
164
165     public static CompositeNodeTOImpl wrap(final QName name, final Node<?> additional, final Node<?> node) {
166         if (node != null) {
167             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
168         } else {
169             return new CompositeNodeTOImpl(name, null, ImmutableList.<Node<?>> of(additional));
170         }
171     }
172
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);
177         if (node != null) {
178             return it.add(node).toInstance();
179         } else {
180             return it.toInstance();
181         }
182     }
183
184 }