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