Bug 1354: Fixed rpc request/result transformation.
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / NetconfToRpcRequestTest.java
1 package org.opendaylight.controller.sal.connect.netconf;
2
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertNotNull;
5 import static junit.framework.Assert.assertTrue;
6
7 import java.io.InputStream;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.Set;
11
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
16 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
20 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
24 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
25 import org.w3c.dom.Document;
26
27
28 /**
29  * Test case for reported bug 1355
30  *
31  * @author Lukas Sedlak
32  * @see <a
33  *      https://bugs.opendaylight.org/show_bug.cgi?id=1355</a>
34  */
35 public class NetconfToRpcRequestTest {
36
37     private final static String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
38     private final static String REVISION = "2014-07-14";
39     private final static QName INPUT_QNAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "input");
40     private final static QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
41     private final static QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
42
43     private final static String CONFIG_TEST_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
44     private final static String CONFIG_TEST_REVISION = "2014-07-21";
45     private final static QName EDIT_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
46     private final static QName GET_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get");
47     private final static QName GET_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get-config");
48
49     static SchemaContext notifCtx;
50     static SchemaContext cfgCtx;
51     static NetconfMessageTransformer messageTransformer;
52
53     @SuppressWarnings("deprecation")
54     @BeforeClass
55     public static void setup() throws Exception {
56         List<InputStream> modelsToParse = Collections
57             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
58         YangContextParser parser = new YangParserImpl();
59         final Set<Module> notifModules = parser.parseYangModelsFromStreams(modelsToParse);
60         assertTrue(!notifModules.isEmpty());
61
62         notifCtx = parser.resolveSchemaContext(notifModules);
63         assertNotNull(notifCtx);
64
65         modelsToParse = Collections
66             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/config-test-rpc.yang"));
67         parser = new YangParserImpl();
68         final Set<Module> configModules = parser.parseYangModelsFromStreams(modelsToParse);
69         cfgCtx = parser.resolveSchemaContext(configModules);
70         assertNotNull(cfgCtx);
71
72         messageTransformer = new NetconfMessageTransformer();
73     }
74
75     @Test
76     public void testIsDataEditOperation() throws Exception {
77         messageTransformer.onGlobalContextUpdated(cfgCtx);
78
79         final CompositeNodeBuilder<ImmutableCompositeNode> rootBuilder = ImmutableCompositeNode.builder();
80         rootBuilder.setQName(EDIT_CONFIG_QNAME);
81
82         final CompositeNodeBuilder<ImmutableCompositeNode> inputBuilder = ImmutableCompositeNode.builder();
83         inputBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "input"));
84
85         final CompositeNodeBuilder<ImmutableCompositeNode> targetBuilder = ImmutableCompositeNode.builder();
86         targetBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "target"));
87         targetBuilder.addLeaf(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "running"), null);
88
89         final CompositeNodeBuilder<ImmutableCompositeNode> configBuilder = ImmutableCompositeNode.builder();
90         configBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "config"));
91
92         final CompositeNodeBuilder<ImmutableCompositeNode> anyxmlTopBuilder = ImmutableCompositeNode.builder();
93         anyxmlTopBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "top"));
94
95         final CompositeNodeBuilder<ImmutableCompositeNode> anyxmlInterfBuilder = ImmutableCompositeNode.builder();
96         anyxmlInterfBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "interface"));
97
98         anyxmlInterfBuilder.addLeaf(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "name"), "Ethernet0/0");
99         anyxmlInterfBuilder.addLeaf(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "mtu"), "1500");
100
101         anyxmlTopBuilder.add(anyxmlInterfBuilder.toInstance());
102         configBuilder.add(anyxmlTopBuilder.toInstance());
103
104         inputBuilder.add(targetBuilder.toInstance());
105         inputBuilder.add(configBuilder.toInstance());
106
107         rootBuilder.add(inputBuilder.toInstance());
108         final ImmutableCompositeNode root = rootBuilder.toInstance();
109
110         final NetconfMessage message = messageTransformer.toRpcRequest(EDIT_CONFIG_QNAME, root);
111         assertNotNull(message);
112
113         final Document xmlDoc = message.getDocument();
114         org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
115         assertEquals(rpcChild.getLocalName(), "rpc");
116
117         final org.w3c.dom.Node editConfigNode = rpcChild.getFirstChild();
118         assertEquals(editConfigNode.getLocalName(), "edit-config");
119
120         final org.w3c.dom.Node targetNode = editConfigNode.getFirstChild();
121         assertEquals(targetNode.getLocalName(), "target");
122
123         final org.w3c.dom.Node runningNode = targetNode.getFirstChild();
124         assertEquals(runningNode.getLocalName(), "running");
125
126         final org.w3c.dom.Node configNode = targetNode.getNextSibling();
127         assertEquals(configNode.getLocalName(), "config");
128
129         final org.w3c.dom.Node topNode = configNode.getFirstChild();
130         assertEquals(topNode.getLocalName(), "top");
131
132         final org.w3c.dom.Node interfaceNode = topNode.getFirstChild();
133         assertEquals(interfaceNode.getLocalName(), "interface");
134
135         final org.w3c.dom.Node nameNode = interfaceNode.getFirstChild();
136         assertEquals(nameNode.getLocalName(), "name");
137
138         final org.w3c.dom.Node mtuNode = nameNode.getNextSibling();
139         assertEquals(mtuNode.getLocalName(), "mtu");
140     }
141
142     @Test
143     public void testIsGetOperation() throws Exception {
144         messageTransformer.onGlobalContextUpdated(cfgCtx);
145
146         final CompositeNodeBuilder<ImmutableCompositeNode> rootBuilder = ImmutableCompositeNode.builder();
147         rootBuilder.setQName(GET_QNAME);
148
149         final CompositeNodeBuilder<ImmutableCompositeNode> inputBuilder = ImmutableCompositeNode.builder();
150         inputBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "input"));
151
152         rootBuilder.add(inputBuilder.toInstance());
153         final ImmutableCompositeNode root = rootBuilder.toInstance();
154
155         final NetconfMessage message = messageTransformer.toRpcRequest(GET_QNAME, root);
156         assertNotNull(message);
157
158         final Document xmlDoc = message.getDocument();
159         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
160         assertEquals(rpcChild.getLocalName(), "rpc");
161
162         final org.w3c.dom.Node get = rpcChild.getFirstChild();
163         assertEquals(get.getLocalName(), "get");
164     }
165
166     @Test
167     public void testIsGetConfigOperation() throws Exception {
168         messageTransformer.onGlobalContextUpdated(cfgCtx);
169
170         final CompositeNodeBuilder<ImmutableCompositeNode> rootBuilder = ImmutableCompositeNode.builder();
171         rootBuilder.setQName(GET_CONFIG_QNAME);
172
173         final CompositeNodeBuilder<ImmutableCompositeNode> inputBuilder = ImmutableCompositeNode.builder();
174         inputBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "input"));
175
176         final CompositeNodeBuilder<ImmutableCompositeNode> sourceBuilder = ImmutableCompositeNode.builder();
177         sourceBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "source"));
178         sourceBuilder.addLeaf(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "running"), null);
179
180         final CompositeNodeBuilder<ImmutableCompositeNode> anyxmlFilterBuilder = ImmutableCompositeNode.builder();
181         anyxmlFilterBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "filter"));
182
183         final CompositeNodeBuilder<ImmutableCompositeNode> anyxmlTopBuilder = ImmutableCompositeNode.builder();
184         anyxmlTopBuilder.setQName(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "top"));
185         anyxmlTopBuilder.addLeaf(QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "users"), null);
186
187         anyxmlFilterBuilder.add(anyxmlTopBuilder.toInstance());
188
189         inputBuilder.add(sourceBuilder.toInstance());
190         inputBuilder.add(anyxmlFilterBuilder.toInstance());
191         rootBuilder.add(inputBuilder.toInstance());
192         final ImmutableCompositeNode root = rootBuilder.toInstance();
193
194         final NetconfMessage message = messageTransformer.toRpcRequest(GET_CONFIG_QNAME, root);
195         assertNotNull(message);
196
197         final Document xmlDoc = message.getDocument();
198         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
199         assertEquals(rpcChild.getLocalName(), "rpc");
200
201         final org.w3c.dom.Node getConfig = rpcChild.getFirstChild();
202         assertEquals(getConfig.getLocalName(), "get-config");
203
204         final org.w3c.dom.Node sourceNode = getConfig.getFirstChild();
205         assertEquals(sourceNode.getLocalName(), "source");
206
207         final org.w3c.dom.Node runningNode = sourceNode.getFirstChild();
208         assertEquals(runningNode.getLocalName(), "running");
209
210         final org.w3c.dom.Node filterNode = sourceNode.getNextSibling();
211         assertEquals(filterNode.getLocalName(), "filter");
212
213         final org.w3c.dom.Node topNode = filterNode.getFirstChild();
214         assertEquals(topNode.getLocalName(), "top");
215
216         final org.w3c.dom.Node usersNode = topNode.getFirstChild();
217         assertEquals(usersNode.getLocalName(), "users");
218     }
219
220     @Test
221     public void testUserDefinedRpcCall() throws Exception {
222         messageTransformer.onGlobalContextUpdated(notifCtx);
223
224         final CompositeNodeBuilder<ImmutableCompositeNode> rootBuilder = ImmutableCompositeNode.builder();
225         rootBuilder.setQName(SUBSCRIBE_RPC_NAME);
226
227         final CompositeNodeBuilder<ImmutableCompositeNode> inputBuilder = ImmutableCompositeNode.builder();
228         inputBuilder.setQName(INPUT_QNAME);
229         inputBuilder.addLeaf(STREAM_NAME, "NETCONF");
230
231         rootBuilder.add(inputBuilder.toInstance());
232         final ImmutableCompositeNode root = rootBuilder.toInstance();
233
234         final CompositeNode flattenedNode = NetconfMessageTransformUtil.flattenInput(root);
235         assertNotNull(flattenedNode);
236         assertEquals(1, flattenedNode.size());
237
238         final List<CompositeNode> inputNode = flattenedNode.getCompositesByName(INPUT_QNAME);
239         assertNotNull(inputNode);
240         assertTrue(inputNode.isEmpty());
241
242         final NetconfMessage message = messageTransformer.toRpcRequest(SUBSCRIBE_RPC_NAME, root);
243         assertNotNull(message);
244
245         final Document xmlDoc = message.getDocument();
246         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
247         assertEquals(rpcChild.getLocalName(), "rpc");
248
249         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
250         assertEquals(subscribeName.getLocalName(), "subscribe");
251
252         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
253         assertEquals(streamName.getLocalName(), "stream-name");
254     }
255
256     @Test
257     public void testNoSchemaContextToRpcRequest() throws Exception {
258         final String exampleNamespace = "http://example.net/me/my-own/1.0";
259         final String exampleRevision = "2014-07-22";
260         final QName myOwnMethodRpcQName = QName.create(exampleNamespace, exampleRevision, "my-own-method");
261
262         final CompositeNodeBuilder<ImmutableCompositeNode> rootBuilder = ImmutableCompositeNode.builder();
263         rootBuilder.setQName(myOwnMethodRpcQName);
264
265         final CompositeNodeBuilder<ImmutableCompositeNode> inputBuilder = ImmutableCompositeNode.builder();
266         inputBuilder.setQName(QName.create(exampleNamespace, exampleRevision, "input"));
267         inputBuilder.addLeaf(QName.create(exampleNamespace, exampleRevision, "my-first-parameter"), "14");
268         inputBuilder.addLeaf(QName.create(exampleNamespace, exampleRevision, "another-parameter"), "fred");
269
270         rootBuilder.add(inputBuilder.toInstance());
271         final ImmutableCompositeNode root = rootBuilder.toInstance();
272
273         final NetconfMessage message = messageTransformer.toRpcRequest(myOwnMethodRpcQName, root);
274         assertNotNull(message);
275
276         final Document xmlDoc = message.getDocument();
277         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
278         assertEquals(rpcChild.getLocalName(), "rpc");
279
280         final org.w3c.dom.Node myOwnMethodNode = rpcChild.getFirstChild();
281         assertEquals(myOwnMethodNode.getLocalName(), "my-own-method");
282
283         final org.w3c.dom.Node firstParamNode = myOwnMethodNode.getFirstChild();
284         assertEquals(firstParamNode.getLocalName(), "my-first-parameter");
285
286         final org.w3c.dom.Node secParamNode = firstParamNode.getNextSibling();
287         assertEquals(secParamNode.getLocalName(), "another-parameter");
288     }
289 }