0d94ef51d71dc9e1d1d19adad6aa1f684f4d4eb9
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / schema / mapping / NetconfMessageTransformerTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.netconf.sal.connect.netconf.schema.mapping;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
31
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.collect.Iterables;
34 import com.google.common.collect.Lists;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Optional;
44 import java.util.Set;
45 import javax.xml.transform.dom.DOMSource;
46 import org.custommonkey.xmlunit.Diff;
47 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
48 import org.custommonkey.xmlunit.XMLUnit;
49 import org.hamcrest.CoreMatchers;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
53 import org.opendaylight.mdsal.dom.api.DOMActionResult;
54 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
55 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
56 import org.opendaylight.netconf.api.NetconfMessage;
57 import org.opendaylight.netconf.api.xml.XmlUtil;
58 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
59 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
60 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
66 import org.opendaylight.yangtools.yang.common.QName;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
68 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
71 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
74 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
78 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
79 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
80 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
81 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
82 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
83 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
84 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
85 import org.w3c.dom.Node;
86 import org.xml.sax.SAXException;
87
88 public class NetconfMessageTransformerTest {
89
90     private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
91     private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
92
93     private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
94     private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
95
96     private NetconfMessageTransformer actionNetconfMessageTransformer;
97     private NetconfMessageTransformer netconfMessageTransformer;
98     private SchemaContext schema;
99
100     @Before
101     public void setUp() throws Exception {
102         XMLUnit.setIgnoreWhitespace(true);
103         XMLUnit.setIgnoreAttributeOrder(true);
104         XMLUnit.setIgnoreComments(true);
105
106         schema = getSchema(true);
107         netconfMessageTransformer = getTransformer(schema);
108         actionNetconfMessageTransformer = getActionMessageTransformer();
109     }
110
111     @Test
112     public void testLockRequestBaseSchemaNotPresent() throws Exception {
113         final SchemaContext partialSchema = getSchema(false);
114         final NetconfMessageTransformer transformer = getTransformer(partialSchema);
115         final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
116                 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
117
118         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
119         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
120     }
121
122     @Test
123     public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
124         final SchemaContext partialSchema = getSchema(true);
125         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(
126                 partialSchema,
127                 true,
128                 BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
129         );
130         NetconfMessage netconfMessage = transformer.toRpcRequest(
131                 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
132                 CREATE_SUBSCRIPTION_RPC_CONTENT
133         );
134         String documentString = XmlUtil.toString(netconfMessage.getDocument());
135         assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
136         assertThat(documentString, CoreMatchers.containsString("<rpc"));
137     }
138
139     @Test
140     public void tesLockSchemaRequest() throws Exception {
141         final SchemaContext partialSchema = getSchema(false);
142         final NetconfMessageTransformer transformer = getTransformer(partialSchema);
143         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
144
145         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
146     }
147
148     @Test
149     public void testDiscardChangesRequest() throws Exception {
150         final NetconfMessage netconfMessage =
151                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
152         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
153         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
154         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
155     }
156
157     @Test
158     public void testGetSchemaRequest() throws Exception {
159         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
160                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
161         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
162                 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
163                 + "<format>yang</format>\n"
164                 + "<identifier>module</identifier>\n"
165                 + "<version>2012-12-12</version>\n"
166                 + "</get-schema>\n"
167                 + "</rpc>");
168     }
169
170     @Test
171     public void testGetSchemaResponse() throws Exception {
172         final NetconfMessageTransformer transformer = getTransformer(getSchema(true));
173         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
174                 "<rpc-reply message-id=\"101\"\n"
175                         + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
176                         + "<data\n"
177                         + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
178                         + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
179                         + "Random YANG SCHEMA\n"
180                         + "</xs:schema>\n"
181                         + "</data>\n"
182                         + "</rpc-reply>"
183         ));
184         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
185         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
186         assertNotNull(compositeNodeRpcResult.getResult());
187         final DOMSource schemaContent =
188             ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
189         assertThat(schemaContent.getNode().getTextContent(),
190                 CoreMatchers.containsString("Random YANG SCHEMA"));
191     }
192
193     @Test
194     public void testGetConfigResponse() throws Exception {
195         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
196                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
197                 + "<data>\n"
198                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
199                 + "<schemas>\n"
200                 + "<schema>\n"
201                 + "<identifier>module</identifier>\n"
202                 + "<version>2012-12-12</version>\n"
203                 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
204                 + "</schema>\n"
205                 + "</schemas>\n"
206                 + "</netconf-state>\n"
207                 + "</data>\n"
208                 + "</rpc-reply>"));
209
210         final NetconfMessageTransformer transformer = getTransformer(getSchema(true));
211         final DOMRpcResult compositeNodeRpcResult =
212                 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
213         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
214         assertNotNull(compositeNodeRpcResult.getResult());
215
216         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
217                 NetconfRemoteSchemaYangSourceProvider
218                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
219
220         final Map<QName, Object> keys = new HashMap<>();
221         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
222             keys.put(value.getNodeType(), value.getValue());
223         }
224
225         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
226                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
227         final MapEntryNode schemaNode =
228                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
229
230         final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult
231                 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
232         final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
233         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
234         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
235         assertEquals(1, Iterables.size(schemaParent.getValue()));
236
237         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
238     }
239
240     @Test
241     public void testGetConfigLeafRequest() throws Exception {
242         final DataContainerChild<?, ?> filter = toFilterStructure(
243                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
244                     new NodeIdentifierWithPredicates(Schema.QNAME, ImmutableMap.of()),
245                     toId(QName.create(Schemas.QNAME, "version"))), schema);
246
247         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
248
249         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
250                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
251
252         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
253                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
254                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
255                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
256                 + "<schemas>\n"
257                 + "<schema>\n"
258                 + "<version/>\n"
259                 + "</schema>\n"
260                 + "</schemas>\n"
261                 + "</netconf-state>\n"
262                 + "</filter>\n"
263                 + "<source>\n"
264                 + "<running/>\n"
265                 + "</source>\n"
266                 + "</get-config>\n"
267                 + "</rpc>");
268     }
269
270     @Test
271     public void testGetConfigRequest() throws Exception {
272         final DataContainerChild<?, ?> filter = toFilterStructure(
273                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
274
275         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
276
277         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
278                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
279
280         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
281                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
282                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
283                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
284                 + "<schemas/>\n"
285                 + "</netconf-state>"
286                 + "</filter>\n"
287                 + "<source>\n"
288                 + "<running/>\n"
289                 + "</source>\n"
290                 + "</get-config>"
291                 + "</rpc>");
292     }
293
294     @Test
295     public void testEditConfigRequest() throws Exception {
296         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
297                 NetconfRemoteSchemaYangSourceProvider
298                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
299
300         final Map<QName, Object> keys = new HashMap<>();
301         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
302             keys.put(value.getNodeType(), value.getValue());
303         }
304
305         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
306                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
307         final MapEntryNode schemaNode =
308                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
309
310         final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
311                 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
312                 .nodeWithKey(Schema.QNAME, keys).build();
313         final DataContainerChild<?, ?> editConfigStructure =
314                 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
315                     Optional.empty(), Optional.ofNullable(schemaNode));
316
317         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
318
319         final ContainerNode wrap =
320                 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
321         final NetconfMessage netconfMessage =
322                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
323
324         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
325                 + "<edit-config>\n"
326                 + "<target>\n"
327                 + "<candidate/>\n"
328                 + "</target>\n"
329                 + "<config>\n"
330                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
331                 + "<schemas>\n"
332                 + "<schema>\n"
333                 + "<identifier>module</identifier>\n"
334                 + "<version>2012-12-12</version>\n"
335                 + "<format>yang</format>\n"
336                 + "</schema>\n"
337                 + "</schemas>\n"
338                 + "</netconf-state>\n"
339                 + "</config>\n"
340                 + "</edit-config>\n"
341                 + "</rpc>");
342     }
343
344     private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
345             throws SAXException, IOException {
346         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
347         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
348         assertTrue(diff.toString(), diff.similar());
349     }
350
351     @Test
352     public void testGetRequest() throws Exception {
353
354         final QName capability = QName.create(Capabilities.QNAME, "capability");
355         final DataContainerChild<?, ?> filter = toFilterStructure(
356                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
357                     new YangInstanceIdentifier.NodeWithValue<>(capability, "a:b:c")), schema);
358
359         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
360                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
361
362         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
363                 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
364                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
365                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
366                 + "<capabilities>\n"
367                 + "<capability>a:b:c</capability>\n"
368                 + "</capabilities>\n"
369                 + "</netconf-state>"
370                 + "</filter>\n"
371                 + "</get>"
372                 + "</rpc>");
373     }
374
375     private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
376         return new NetconfMessageTransformer(schema, true);
377     }
378
379     @Test
380     public void testCommitResponse() throws Exception {
381         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
382                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
383         ));
384         final DOMRpcResult compositeNodeRpcResult =
385                 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
386         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
387         assertNull(compositeNodeRpcResult.getResult());
388     }
389
390     public SchemaContext getSchema(final boolean addBase) {
391         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
392         if (addBase) {
393             moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
394         }
395         moduleInfoBackedContext
396                 .addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
397                         .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
398         return moduleInfoBackedContext.tryToCreateSchemaContext().get();
399     }
400
401     @Test
402     public void getActionsTest() {
403         QName reset = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
404         QName start = QName.create(reset, "start");
405         QName open = QName.create(start, "open");
406         QName enable = QName.create(open, "enable");
407         QName kill = QName.create(URN_EXAMPLE_SERVER_FARM_2, REVISION_EXAMPLE_SERVER_FARM_2, "kill");
408         Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open, enable, kill));
409         Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
410         assertTrue(!actions.isEmpty());
411         for (ActionDefinition actionDefinition : actions) {
412             QName qname = actionDefinition.getQName();
413             assertTrue(qnames.contains(qname));
414             qnames.remove(qname);
415         }
416     }
417
418     @Test
419     public void toActionRequestListTopLevelTest() {
420         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
421         QName nameQname = QName.create(qname, "name");
422         QName actionResetQName = QName.create(qname, "reset");
423         List<PathArgument> nodeIdentifiers = new ArrayList<>();
424         nodeIdentifiers.add(new NodeIdentifier(qname));
425         nodeIdentifiers.add(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
426         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
427
428         ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
429
430         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
431                 SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
432
433         Node childAction = checkBasePartOfActionRequest(actionRequest);
434
435         Node childServer = childAction.getFirstChild();
436         checkNode(childServer, "server", "server", qname.getNamespace().toString());
437
438         Node childName = childServer.getFirstChild();
439         checkNode(childName, "name", "name", qname.getNamespace().toString());
440
441         Node childTest = childName.getFirstChild();
442         assertEquals(childTest.getNodeValue(), "test");
443
444         checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
445     }
446
447     @Test
448     public void toActionRequestContainerTopLevelTest() {
449         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
450         QName actionStartQName = QName.create(qname, "start");
451
452         List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
453         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
454
455         NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
456         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
457                 SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
458
459         Node childAction = checkBasePartOfActionRequest(actionRequest);
460
461         Node childDevice = childAction.getFirstChild();
462         checkNode(childDevice, "device", "device", qname.getNamespace().toString());
463
464         checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
465     }
466
467     @Test
468     public void toActionRequestContainerInContainerTest() {
469         QName boxOutQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
470         QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
471         QName actionOpenQName = QName.create(boxOutQName, "open");
472
473         List<PathArgument> nodeIdentifiers = new ArrayList<>();
474         nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
475         nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
476
477         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
478
479         NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
480         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
481                 SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
482
483         Node childAction = checkBasePartOfActionRequest(actionRequest);
484
485         Node childBoxOut = childAction.getFirstChild();
486         checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
487
488         Node childBoxIn = childBoxOut.getFirstChild();
489         checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
490
491         Node action = childBoxIn.getFirstChild();
492         checkNode(action, null, actionOpenQName.getLocalName(), null);
493     }
494
495     @Test
496     public void toActionRequestListInContainerTest() {
497         QName qnameDevice = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
498         QName qnameInterface = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "interface");
499         QName nameQname = QName.create(qnameInterface, "name");
500         QName actionEnableQName = QName.create(qnameInterface, "enable");
501
502         List<PathArgument> nodeIdentifiers = new ArrayList<>();
503         nodeIdentifiers.add(NodeIdentifier.create(qnameDevice));
504         nodeIdentifiers.add(NodeIdentifier.create(qnameInterface));
505         nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameInterface, nameQname, "test"));
506
507         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
508
509         NormalizedNode<?, ?> payload = initEmptyInputAction(qnameInterface);
510         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
511                 SchemaPath.create(true, actionEnableQName), domDataTreeIdentifier, payload);
512
513         Node childAction = checkBasePartOfActionRequest(actionRequest);
514
515         Node childDevice = childAction.getFirstChild();
516         checkNode(childDevice, "device", "device", qnameDevice.getNamespace().toString());
517
518         Node childInterface = childDevice.getFirstChild();
519         checkNode(childInterface, "interface", "interface", qnameInterface.getNamespace().toString());
520
521         Node childName = childInterface.getFirstChild();
522         checkNode(childName, "name", "name", nameQname.getNamespace().toString());
523
524         Node childTest = childName.getFirstChild();
525         assertEquals(childTest.getNodeValue(), "test");
526
527         Node action = childInterface.getLastChild();
528         checkNode(action, null, actionEnableQName.getLocalName(), null);
529     }
530
531     @Test
532     public void toActionRequestListInContainerAugmentedIntoListTest() {
533         QName qnameServer = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
534         QName serverNameQname = QName.create(qnameServer, "name");
535         QName qnameAppliactions = QName.create(URN_EXAMPLE_SERVER_FARM_2,
536                 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
537         QName qnameAppliaction = QName.create(URN_EXAMPLE_SERVER_FARM_2,
538                 REVISION_EXAMPLE_SERVER_FARM_2, "application");
539         QName applicationNameQname = QName.create(qnameAppliaction, "name");
540         QName actionKillQName = QName.create(qnameAppliaction, "kill");
541
542         List<PathArgument> nodeIdentifiers = new ArrayList<>();
543         nodeIdentifiers.add(NodeIdentifier.create(qnameServer));
544         nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameServer, serverNameQname, "testServer"));
545         nodeIdentifiers.add(new YangInstanceIdentifier
546                 .AugmentationIdentifier(Collections.singleton(qnameAppliactions)));
547         nodeIdentifiers.add(NodeIdentifier.create(qnameAppliactions));
548         nodeIdentifiers.add(NodeIdentifier.create(qnameAppliaction));
549         nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameAppliaction,
550                 applicationNameQname, "testApplication"));
551
552         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
553
554         NormalizedNode<?, ?> payload = initEmptyInputAction(qnameAppliaction);
555         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
556                 SchemaPath.create(true, actionKillQName), domDataTreeIdentifier, payload);
557
558         Node childAction = checkBasePartOfActionRequest(actionRequest);
559
560         Node childServer = childAction.getFirstChild();
561         checkNode(childServer, "server", "server", qnameServer.getNamespace().toString());
562
563         Node childServerName = childServer.getFirstChild();
564         checkNode(childServerName, "name", "name", serverNameQname.getNamespace().toString());
565
566         Node childServerNameTest = childServerName.getFirstChild();
567         assertEquals(childServerNameTest.getNodeValue(), "testServer");
568
569         Node childApplications = childServer.getLastChild();
570         checkNode(childApplications, "applications", "applications", qnameAppliactions.getNamespace().toString());
571
572         Node childApplication = childApplications.getFirstChild();
573         checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
574
575         Node childApplicationName = childApplication.getFirstChild();
576         checkNode(childApplicationName, "name", "name", applicationNameQname.getNamespace().toString());
577
578         Node childApplicationNameTest = childApplicationName.getFirstChild();
579         assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
580
581         Node childKillAction = childApplication.getLastChild();
582         checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
583         checkNode(childKillAction, null, actionKillQName.getLocalName(), null);
584     }
585
586     @SuppressWarnings({ "rawtypes", "unchecked" })
587     @Test
588     public void toActionResultTest() throws Exception {
589         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
590
591         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
592                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
593                 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
594                 + "now"
595                 + "</reset-finished-at>"
596                 + "</rpc-reply>"));
597         DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(
598                 SchemaPath.create(true, qname), message);
599         assertNotNull(actionResult);
600         ContainerNode containerNode = actionResult.getOutput().get();
601         assertNotNull(containerNode);
602         LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
603         assertEquals("now", leaf.getValue());
604     }
605
606     private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
607             final String inputNodeName, final String inputValue) {
608         checkNode(action, null, actionQname.getLocalName(), null);
609
610         Node childResetAt = action.getFirstChild();
611         checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
612
613         Node firstChild = childResetAt.getFirstChild();
614         assertEquals(firstChild.getNodeValue(), inputValue);
615     }
616
617     private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
618         Node baseRpc = actionRequest.getDocument().getFirstChild();
619         checkNode(baseRpc, "rpc", "rpc", NetconfMessageTransformUtil.NETCONF_QNAME.getNamespace().toString());
620         assertTrue(baseRpc.getLocalName().equals("rpc"));
621         assertTrue(baseRpc.getNodeName().equals("rpc"));
622
623         Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
624         assertNotNull(messageId);
625         assertTrue(messageId.getNodeValue().contains("m-"));
626
627         Node childAction = baseRpc.getFirstChild();
628         checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
629         return childAction;
630     }
631
632     private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
633         YangInstanceIdentifier yangInstanceIdentifier =
634                 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
635         DOMDataTreeIdentifier domDataTreeIdentifier =
636                 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
637                         yangInstanceIdentifier);
638         return domDataTreeIdentifier;
639     }
640
641     private static ContainerNode initInputAction(final QName qname, final String value) {
642         ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
643         DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
644                 NodeIdentifier.create(qname)).withValue(value).build();
645         ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
646                 QName.create(qname, "input"))).withChild(build).build();
647         return data;
648     }
649
650     private static ContainerNode initEmptyInputAction(final QName qname) {
651         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
652                 QName.create(qname, "input"))).build();
653     }
654
655     private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
656             final String expectedNamespace) {
657         assertNotNull(childServer);
658         assertEquals(childServer.getLocalName(), expectedLocalName);
659         assertEquals(childServer.getNodeName(), expectedNodeName);
660         assertEquals(childServer.getNamespaceURI(), expectedNamespace);
661     }
662
663     private static SchemaContext getActionSchema() {
664         return YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
665                 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang");
666     }
667
668     private static NetconfMessageTransformer getActionMessageTransformer() {
669         return new NetconfMessageTransformer(getActionSchema(), true);
670     }
671 }