Bump upstreams to SNAPSHOTs
[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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_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.util.NetconfUtil.NETCONF_DATA_QNAME;
31
32 import com.google.common.collect.Iterables;
33 import com.google.common.collect.Lists;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.Set;
42 import javax.xml.transform.dom.DOMSource;
43 import org.custommonkey.xmlunit.Diff;
44 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
45 import org.custommonkey.xmlunit.XMLUnit;
46 import org.hamcrest.CoreMatchers;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
52 import org.opendaylight.mdsal.dom.api.DOMActionResult;
53 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
54 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
55 import org.opendaylight.netconf.api.NetconfMessage;
56 import org.opendaylight.netconf.api.xml.XmlUtil;
57 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
58 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
59 import org.opendaylight.netconf.sal.connect.netconf.util.FieldsFilter;
60 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
61 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
62 import org.opendaylight.netconf.util.NetconfUtil;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Datastores;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Statistics;
70 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.Datastore;
71 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.datastore.Locks;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.datastore.locks.lock.type.partial.lock.PartialLock;
73 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
74 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
75 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfConfigChange;
76 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.netconf.config.change.Edit;
77 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
78 import org.opendaylight.yangtools.yang.common.QName;
79 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
80 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
81 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
83 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
84 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
85 import org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode;
86 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
89 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
90 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
91 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
92 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
93 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
94 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
95 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
96 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
97 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
98 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
99 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
100 import org.w3c.dom.Node;
101 import org.xml.sax.SAXException;
102
103 public class NetconfMessageTransformerTest extends AbstractBaseSchemasTest {
104
105     private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
106     private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
107
108     private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
109     private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
110
111     private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
112
113     private static final String URN_EXAMPLE_AUGMENTED_ACTION = "urn:example:augmented-action";
114
115     private static final String URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS = "urn:example:rpcs-actions-outputs";
116
117     private static final QName SERVER_QNAME =
118             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
119     private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
120     private static final Absolute RESET_SERVER_PATH = Absolute.of(SERVER_QNAME, RESET_QNAME);
121     private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
122             REVISION_EXAMPLE_SERVER_FARM_2, "applications");
123     private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
124     private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
125     private static final Absolute KILL_SERVER_APP_PATH =
126             Absolute.of(SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
127
128     private static final QName DEVICE_QNAME =
129             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
130     private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
131     private static final Absolute START_DEVICE_PATH = Absolute.of(DEVICE_QNAME, START_QNAME);
132     private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
133     private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
134     private static final Absolute ENABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
135
136     private static final QName DISABLE_QNAME = QName.create(URN_EXAMPLE_AUGMENTED_ACTION, "disable");
137     private static final Absolute DISABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, DISABLE_QNAME);
138
139     private static final QName CHECK_WITH_OUTPUT_QNAME =
140             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-with-output");
141     private static final Absolute CHECK_WITH_OUTPUT_INTERFACE_PATH =
142             Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITH_OUTPUT_QNAME);
143     private static final QName CHECK_WITHOUT_OUTPUT_QNAME =
144             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-without-output");
145     private static final Absolute CHECK_WITHOUT_OUTPUT_INTERFACE_PATH =
146             Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITHOUT_OUTPUT_QNAME);
147     private static final QName RPC_WITH_OUTPUT_QNAME =
148             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-with-output");
149     private static final QName RPC_WITHOUT_OUTPUT_QNAME =
150             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-without-output");
151
152     private static final QName BOX_OUT_QNAME =
153             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
154     private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
155     private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
156     private static final Absolute OPEN_BOXES_PATH = Absolute.of(BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
157
158     private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
159     private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
160     private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
161     private static final Absolute XYZZY_FOO_PATH = Absolute.of(FOO_QNAME, XYZZY_QNAME);
162     private static final Absolute XYZZY_BAR_PATH = Absolute.of(BAR_QNAME, XYZZY_QNAME);
163
164     private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
165     private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
166     private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
167     private static final Absolute CHOICE_ACTION_PATH =
168             Absolute.of(CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
169
170     private static EffectiveModelContext PARTIAL_SCHEMA;
171     private static EffectiveModelContext SCHEMA;
172     private static EffectiveModelContext ACTION_SCHEMA;
173
174     private NetconfMessageTransformer actionNetconfMessageTransformer;
175     private NetconfMessageTransformer netconfMessageTransformer;
176
177     @BeforeClass
178     public static void beforeClass() {
179         PARTIAL_SCHEMA = BindingRuntimeHelpers.createEffectiveModel(NetconfState.class);
180         SCHEMA = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class,
181             NetconfConfigChange.class);
182         ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
183             "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
184             "/schemas/conflicting-actions.yang", "/schemas/augmented-action.yang",
185             "/schemas/rpcs-actions-outputs.yang");
186     }
187
188     @AfterClass
189     public static void afterClass() {
190         PARTIAL_SCHEMA = null;
191         SCHEMA = null;
192         ACTION_SCHEMA = null;
193     }
194
195     @Before
196     public void setUp() throws Exception {
197         XMLUnit.setIgnoreWhitespace(true);
198         XMLUnit.setIgnoreAttributeOrder(true);
199         XMLUnit.setIgnoreComments(true);
200
201         netconfMessageTransformer = getTransformer(SCHEMA);
202         actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
203             true, BASE_SCHEMAS.getBaseSchema());
204     }
205
206     @Test
207     public void testLockRequestBaseSchemaNotPresent() throws Exception {
208         final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
209         final NetconfMessage netconfMessage = transformer.toRpcRequest(NETCONF_LOCK_QNAME,
210                 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
211
212         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
213         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
214     }
215
216     @Test
217     public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
218         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
219             true, BASE_SCHEMAS.getBaseSchemaWithNotifications());
220         NetconfMessage netconfMessage = transformer.toRpcRequest(CREATE_SUBSCRIPTION_RPC_QNAME,
221                 CREATE_SUBSCRIPTION_RPC_CONTENT);
222         String documentString = XmlUtil.toString(netconfMessage.getDocument());
223         assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
224         assertThat(documentString, CoreMatchers.containsString("<rpc"));
225     }
226
227     @Test
228     public void tesLockSchemaRequest() throws Exception {
229         final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
230         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
231
232         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), NETCONF_LOCK_QNAME);
233     }
234
235     @Test
236     public void testRpcEmptyBodyWithOutputDefinedSchemaResult() throws Exception {
237         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
238
239         DOMRpcResult domRpcResult = actionNetconfMessageTransformer
240                 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITH_OUTPUT_QNAME);
241         assertNotNull(domRpcResult);
242     }
243
244     @Test
245     public void testRpcEmptyBodyWithoutOutputDefinedSchemaResult() throws Exception {
246         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
247
248         DOMRpcResult domRpcResult = actionNetconfMessageTransformer
249                 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITHOUT_OUTPUT_QNAME);
250         assertNotNull(domRpcResult);
251     }
252
253     @Test
254     public void testDiscardChangesRequest() throws Exception {
255         final NetconfMessage netconfMessage =
256                 netconfMessageTransformer.toRpcRequest(NETCONF_DISCARD_CHANGES_QNAME, null);
257         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
258         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
259         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
260     }
261
262     @Test
263     public void testGetSchemaRequest() throws Exception {
264         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(GET_SCHEMA_QNAME,
265                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
266         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
267                 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
268                 + "<format>yang</format>\n"
269                 + "<identifier>module</identifier>\n"
270                 + "<version>2012-12-12</version>\n"
271                 + "</get-schema>\n"
272                 + "</rpc>");
273     }
274
275     @Test
276     public void testGetSchemaResponse() throws Exception {
277         final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
278         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
279                 "<rpc-reply message-id=\"101\"\n"
280                         + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
281                         + "<data\n"
282                         + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
283                         + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
284                         + "Random YANG SCHEMA\n"
285                         + "</xs:schema>\n"
286                         + "</data>\n"
287                         + "</rpc-reply>"
288         ));
289         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, GET_SCHEMA_QNAME);
290         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
291         assertNotNull(compositeNodeRpcResult.getResult());
292         final DOMSource schemaContent = ((DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
293                 .body().iterator().next()).body();
294         assertThat(schemaContent.getNode().getTextContent(),
295                 CoreMatchers.containsString("Random YANG SCHEMA"));
296     }
297
298     @Test
299     public void testGetConfigResponse() throws Exception {
300         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
301                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
302                 + "<data>\n"
303                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
304                 + "<schemas>\n"
305                 + "<schema>\n"
306                 + "<identifier>module</identifier>\n"
307                 + "<version>2012-12-12</version>\n"
308                 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
309                 + "</schema>\n"
310                 + "</schemas>\n"
311                 + "</netconf-state>\n"
312                 + "</data>\n"
313                 + "</rpc-reply>"));
314
315         final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
316         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, NETCONF_GET_CONFIG_QNAME);
317         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
318         assertNotNull(compositeNodeRpcResult.getResult());
319
320         final List<DataContainerChild> values = Lists.newArrayList(
321                 NetconfRemoteSchemaYangSourceProvider
322                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).body());
323
324         final Map<QName, Object> keys = new HashMap<>();
325         for (final DataContainerChild value : values) {
326             keys.put(value.getIdentifier().getNodeType(), value.body());
327         }
328
329         final NodeIdentifierWithPredicates identifierWithPredicates =
330                 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
331         final MapEntryNode schemaNode =
332                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
333
334         final DOMSourceAnyxmlNode data = (DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
335                 .findChildByArg(toId(NETCONF_DATA_QNAME)).get();
336
337         NormalizedNodeResult nodeResult =
338                 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.body());
339         ContainerNode result = (ContainerNode) nodeResult.getResult();
340         final ContainerNode state = (ContainerNode) result.findChildByArg(toId(NetconfState.QNAME)).get();
341         final ContainerNode schemas = (ContainerNode) state.findChildByArg(toId(Schemas.QNAME)).get();
342         final MapNode schemaParent = (MapNode) schemas.findChildByArg(toId(Schema.QNAME)).get();
343         assertEquals(1, Iterables.size(schemaParent.body()));
344
345         assertEquals(schemaNode, schemaParent.body().iterator().next());
346     }
347
348     @Test
349     public void testGetConfigLeafRequest() throws Exception {
350         final AnyxmlNode<?> filter = toFilterStructure(
351                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
352                     NodeIdentifierWithPredicates.of(Schema.QNAME),
353                     toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
354
355         final ContainerNode source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
356
357         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
358                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
359
360         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
361                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
362                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
363                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
364                 + "<schemas>\n"
365                 + "<schema>\n"
366                 + "<version/>\n"
367                 + "</schema>\n"
368                 + "</schemas>\n"
369                 + "</netconf-state>\n"
370                 + "</filter>\n"
371                 + "<source>\n"
372                 + "<running/>\n"
373                 + "</source>\n"
374                 + "</get-config>\n"
375                 + "</rpc>");
376     }
377
378     @Test
379     public void testGetConfigRequest() throws Exception {
380         final AnyxmlNode<?> filter = toFilterStructure(
381                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
382
383         final ContainerNode source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
384
385         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
386                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
387
388         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
389                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
390                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
391                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
392                 + "<schemas/>\n"
393                 + "</netconf-state>"
394                 + "</filter>\n"
395                 + "<source>\n"
396                 + "<running/>\n"
397                 + "</source>\n"
398                 + "</get-config>"
399                 + "</rpc>");
400     }
401
402     @Test
403     public void testEditConfigRequest() throws Exception {
404         final List<DataContainerChild> values = Lists.newArrayList(
405                 NetconfRemoteSchemaYangSourceProvider
406                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).body());
407
408         final Map<QName, Object> keys = new HashMap<>();
409         for (final DataContainerChild value : values) {
410             keys.put(value.getIdentifier().getNodeType(), value.body());
411         }
412
413         final NodeIdentifierWithPredicates identifierWithPredicates =
414                 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
415         final MapEntryNode schemaNode =
416                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
417
418         final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
419                 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
420                 .nodeWithKey(Schema.QNAME, keys).build();
421         final DataContainerChild editConfigStructure =
422                 createEditConfigStructure(BASE_SCHEMAS.getBaseSchemaWithNotifications().getEffectiveModelContext(), id,
423                     Optional.empty(), Optional.ofNullable(schemaNode));
424
425         final DataContainerChild target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
426
427         final ContainerNode wrap =
428                 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
429         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_EDIT_CONFIG_QNAME, wrap);
430
431         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
432                 + "<edit-config>\n"
433                 + "<target>\n"
434                 + "<candidate/>\n"
435                 + "</target>\n"
436                 + "<config>\n"
437                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
438                 + "<schemas>\n"
439                 + "<schema>\n"
440                 + "<identifier>module</identifier>\n"
441                 + "<version>2012-12-12</version>\n"
442                 + "<format>yang</format>\n"
443                 + "</schema>\n"
444                 + "</schemas>\n"
445                 + "</netconf-state>\n"
446                 + "</config>\n"
447                 + "</edit-config>\n"
448                 + "</rpc>");
449     }
450
451     private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
452             throws SAXException, IOException {
453         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
454         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
455         assertTrue(diff.toString(), diff.similar());
456     }
457
458     @Test
459     public void testGetRequest() throws Exception {
460
461         final QName capability = QName.create(Capabilities.QNAME, "capability");
462         final DataContainerChild filter = toFilterStructure(
463                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
464                     new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
465
466         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
467                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
468
469         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
470                 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
471                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
472                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
473                 + "<capabilities>\n"
474                 + "<capability>a:b:c</capability>\n"
475                 + "</capabilities>\n"
476                 + "</netconf-state>"
477                 + "</filter>\n"
478                 + "</get>"
479                 + "</rpc>");
480     }
481
482     @Test
483     public void testGetLeafList() throws IOException, SAXException {
484         final YangInstanceIdentifier path = YangInstanceIdentifier.create(
485                 toId(NetconfState.QNAME),
486                 toId(Capabilities.QNAME),
487                 toId(QName.create(Capabilities.QNAME, "capability")));
488         final DataContainerChild filter = toFilterStructure(path, SCHEMA);
489         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
490                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filter));
491
492         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
493                 + "<get>\n"
494                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
495                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
496                 + "<capabilities>\n"
497                 + "<capability/>\n"
498                 + "</capabilities>\n"
499                 + "</netconf-state>\n"
500                 + "</filter>\n"
501                 + "</get>\n"
502                 + "</rpc>\n");
503     }
504
505     @Test
506     public void testGetList() throws IOException, SAXException {
507         final YangInstanceIdentifier path = YangInstanceIdentifier.create(
508                 toId(NetconfState.QNAME),
509                 toId(Datastores.QNAME),
510                 toId(QName.create(Datastores.QNAME, "datastore")));
511         final DataContainerChild filter = toFilterStructure(path, SCHEMA);
512         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
513                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filter));
514
515         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
516                 + "<get>\n"
517                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
518                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
519                 + "<datastores>\n"
520                 + "<datastore/>\n"
521                 + "</datastores>\n"
522                 + "</netconf-state>\n"
523                 + "</filter>\n"
524                 + "</get>\n"
525                 + "</rpc>\n");
526     }
527
528     private static NetconfMessageTransformer getTransformer(final EffectiveModelContext schema) {
529         return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true, BASE_SCHEMAS.getBaseSchema());
530     }
531
532     @Test
533     public void testCommitResponse() throws Exception {
534         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
535                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
536         ));
537         final DOMRpcResult compositeNodeRpcResult =
538                 netconfMessageTransformer.toRpcResult(response, NETCONF_COMMIT_QNAME);
539         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
540         assertNull(compositeNodeRpcResult.getResult());
541     }
542
543     @Test
544     public void getActionsTest() {
545         Set<Absolute> schemaPaths = new HashSet<>();
546         schemaPaths.add(RESET_SERVER_PATH);
547         schemaPaths.add(START_DEVICE_PATH);
548         schemaPaths.add(ENABLE_INTERFACE_PATH);
549         schemaPaths.add(OPEN_BOXES_PATH);
550         schemaPaths.add(KILL_SERVER_APP_PATH);
551         schemaPaths.add(XYZZY_FOO_PATH);
552         schemaPaths.add(XYZZY_BAR_PATH);
553         schemaPaths.add(CHOICE_ACTION_PATH);
554         schemaPaths.add(DISABLE_INTERFACE_PATH);
555         schemaPaths.add(CHECK_WITH_OUTPUT_INTERFACE_PATH);
556         schemaPaths.add(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH);
557
558         var actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
559         assertEquals(schemaPaths.size(), actions.size());
560
561         for (var path : schemaPaths) {
562             assertNotNull("Action for " + path + " not found", actions.get(path));
563         }
564     }
565
566     @Test
567     public void toActionRequestListTopLevelTest() {
568         QName nameQname = QName.create(SERVER_QNAME, "name");
569         List<PathArgument> nodeIdentifiers = new ArrayList<>();
570         nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
571         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
572         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
573
574         ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
575
576         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
577                 RESET_SERVER_PATH, domDataTreeIdentifier, data);
578
579         Node childAction = checkBasePartOfActionRequest(actionRequest);
580
581         Node childServer = childAction.getFirstChild();
582         checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
583
584         Node childName = childServer.getFirstChild();
585         checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
586
587         Node childTest = childName.getFirstChild();
588         assertEquals(childTest.getNodeValue(), "test");
589
590         checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
591     }
592
593     @Test
594     public void toActionRequestContainerTopLevelTest() {
595         List<PathArgument> nodeIdentifiers = List.of(NodeIdentifier.create(DEVICE_QNAME));
596         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
597
598         ContainerNode payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
599         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
600                 START_DEVICE_PATH, domDataTreeIdentifier, payload);
601
602         Node childAction = checkBasePartOfActionRequest(actionRequest);
603
604         Node childDevice = childAction.getFirstChild();
605         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
606
607         checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
608     }
609
610     @Test
611     public void toActionRequestContainerInContainerTest() {
612         List<PathArgument> nodeIdentifiers = new ArrayList<>();
613         nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
614         nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
615
616         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
617
618         ContainerNode payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
619         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
620                 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
621
622         Node childAction = checkBasePartOfActionRequest(actionRequest);
623
624         Node childBoxOut = childAction.getFirstChild();
625         checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
626
627         Node childBoxIn = childBoxOut.getFirstChild();
628         checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
629
630         Node action = childBoxIn.getFirstChild();
631         checkNode(action, OPEN_QNAME.getLocalName(), OPEN_QNAME.getLocalName(), OPEN_QNAME.getNamespace().toString());
632     }
633
634     @Test
635     public void toActionRequestListInContainerTest() {
636         QName nameQname = QName.create(INTERFACE_QNAME, "name");
637
638         List<PathArgument> nodeIdentifiers = new ArrayList<>();
639         nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
640         nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
641         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
642
643         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
644
645         ContainerNode payload = initEmptyInputAction(INTERFACE_QNAME);
646         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
647                 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
648
649         Node childAction = checkBasePartOfActionRequest(actionRequest);
650
651         Node childDevice = childAction.getFirstChild();
652         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
653
654         Node childInterface = childDevice.getFirstChild();
655         checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
656
657         Node childName = childInterface.getFirstChild();
658         checkNode(childName, "name", "name", nameQname.getNamespace().toString());
659
660         Node childTest = childName.getFirstChild();
661         assertEquals(childTest.getNodeValue(), "test");
662
663         Node action = childInterface.getLastChild();
664         checkNode(action, ENABLE_QNAME.getLocalName(), ENABLE_QNAME.getLocalName(),
665                 ENABLE_QNAME.getNamespace().toString());
666     }
667
668     @Test
669     public void toActionRequestListInContainerAugmentedIntoListTest() {
670         QName serverNameQname = QName.create(SERVER_QNAME, "name");
671         QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
672
673         List<PathArgument> nodeIdentifiers = new ArrayList<>();
674         nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
675         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
676         nodeIdentifiers.add(new AugmentationIdentifier(Set.of(APPLICATIONS_QNAME)));
677         nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
678         nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
679         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
680                 applicationNameQname, "testApplication"));
681
682         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
683
684         ContainerNode payload = initEmptyInputAction(APPLICATION_QNAME);
685         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
686                 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
687
688         Node childAction = checkBasePartOfActionRequest(actionRequest);
689
690         Node childServer = childAction.getFirstChild();
691         checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
692
693         Node childServerName = childServer.getFirstChild();
694         checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
695
696         Node childServerNameTest = childServerName.getFirstChild();
697         assertEquals(childServerNameTest.getNodeValue(), "testServer");
698
699         Node childApplications = childServer.getLastChild();
700         checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
701
702         Node childApplication = childApplications.getFirstChild();
703         checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
704
705         Node childApplicationName = childApplication.getFirstChild();
706         checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
707
708         Node childApplicationNameTest = childApplicationName.getFirstChild();
709         assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
710
711         Node childKillAction = childApplication.getLastChild();
712         checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
713         checkNode(childKillAction, KILL_QNAME.getLocalName(), KILL_QNAME.getLocalName(),
714                 KILL_QNAME.getNamespace().toString());
715     }
716
717     @Test
718     public void toActionRequestConflictingInListTest() {
719         QName barInputQname = QName.create(BAR_QNAME, "bar");
720         QName barIdQname = QName.create(BAR_QNAME, "bar-id");
721         Byte barInput = 1;
722
723         List<PathArgument> nodeIdentifiers = new ArrayList<>();
724         nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
725         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
726
727         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
728
729         ContainerNode payload = ImmutableContainerNodeBuilder.create()
730                 .withNodeIdentifier(NodeIdentifier.create(QName.create(barInputQname, "input")))
731                 .withChild(ImmutableNodes.leafNode(barInputQname, barInput))
732                 .build();
733
734         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
735                 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
736
737         Node childAction = checkBasePartOfActionRequest(actionRequest);
738
739         Node childBar = childAction.getFirstChild();
740         checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
741
742         Node childBarId = childBar.getFirstChild();
743         checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
744
745         Node childTest = childBarId.getFirstChild();
746         assertEquals(childTest.getNodeValue(), "test");
747
748         Node action = childBar.getLastChild();
749         checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
750                 XYZZY_QNAME.getNamespace().toString());
751     }
752
753     @Test
754     public void toActionRequestConflictingInContainerTest() {
755         QName fooInputQname = QName.create(FOO_QNAME, "foo");
756
757         List<PathArgument> nodeIdentifiers = new ArrayList<>();
758         nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
759         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
760         ContainerNode payload = initInputAction(fooInputQname, "test");
761
762         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
763                 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
764
765         Node childAction = checkBasePartOfActionRequest(actionRequest);
766
767         Node childBar = childAction.getFirstChild();
768         checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
769
770         Node action = childBar.getLastChild();
771         checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
772                 XYZZY_QNAME.getNamespace().toString());
773     }
774
775     @Test
776     public void toActionRequestChoiceTest() {
777         List<PathArgument> nodeIdentifiers = new ArrayList<>();
778         nodeIdentifiers.add(NodeIdentifier.create(CONFLICT_CHOICE_QNAME));
779         nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
780         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
781         NormalizedNode payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
782
783         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
784                 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
785
786         Node childAction = checkBasePartOfActionRequest(actionRequest);
787
788         Node childChoiceCont = childAction.getFirstChild();
789         checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
790
791         Node action = childChoiceCont.getLastChild();
792         checkNode(action, CHOICE_ACTION_QNAME.getLocalName(), CHOICE_ACTION_QNAME.getLocalName(),
793                 CHOICE_ACTION_QNAME.getNamespace().toString());
794     }
795
796     @Test
797     public void toAugmentedActionRequestListInContainerTest() {
798         QName nameQname = QName.create(INTERFACE_QNAME, "name");
799
800         List<PathArgument> nodeIdentifiers = new ArrayList<>();
801         nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
802         nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
803         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
804
805         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
806
807         NormalizedNode payload = initEmptyInputAction(INTERFACE_QNAME);
808         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
809                 DISABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
810
811         Node childAction = checkBasePartOfActionRequest(actionRequest);
812
813         Node childDevice = childAction.getFirstChild();
814         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
815
816         Node childInterface = childDevice.getFirstChild();
817         checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
818
819         Node childName = childInterface.getFirstChild();
820         checkNode(childName, "name", "name", nameQname.getNamespace().toString());
821
822         Node childTest = childName.getFirstChild();
823         assertEquals(childTest.getNodeValue(), "test");
824
825         Node action = childInterface.getLastChild();
826         checkNode(action, DISABLE_QNAME.getLocalName(), DISABLE_QNAME.getLocalName(),
827                 DISABLE_QNAME.getNamespace().toString());
828     }
829
830     @Test
831     public void toActionResultTest() throws Exception {
832         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
833                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
834                 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
835                 + "now"
836                 + "</reset-finished-at>"
837                 + "</rpc-reply>"));
838         DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
839         assertNotNull(actionResult);
840         ContainerNode containerNode = actionResult.getOutput().get();
841         assertNotNull(containerNode);
842         assertEquals("now", containerNode.body().iterator().next().body());
843     }
844
845     @Test
846     public void toActionEmptyBodyWithOutputDefinedResultTest() throws Exception {
847         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
848                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
849                 + "<ok/>"
850                 + "</rpc-reply>"));
851         DOMActionResult actionResult =
852                 actionNetconfMessageTransformer.toActionResult(CHECK_WITH_OUTPUT_INTERFACE_PATH, message);
853         assertNotNull(actionResult);
854         assertTrue(actionResult.getOutput().isEmpty());
855     }
856
857     @Test
858     public void toActionEmptyBodyWithoutOutputDefinedResultTest() throws Exception {
859         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
860                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
861                 + "<ok/>"
862                 + "</rpc-reply>"));
863         DOMActionResult actionResult =
864                 actionNetconfMessageTransformer.toActionResult(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH, message);
865         assertNotNull(actionResult);
866         assertTrue(actionResult.getOutput().isEmpty());
867     }
868
869     @Test
870     public void getTwoNonOverlappingFieldsTest() throws IOException, SAXException {
871         // preparation of the fields
872         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
873         final YangInstanceIdentifier netconfStartTimeField = YangInstanceIdentifier.create(toId(Statistics.QNAME),
874                 toId(QName.create(Statistics.QNAME, "netconf-start-time")));
875         final YangInstanceIdentifier datastoresField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
876
877         // building filter structure and NETCONF message
878         final AnyxmlNode<?> filterStructure = toFilterStructure(
879             List.of(FieldsFilter.of(parentYiid, List.of(netconfStartTimeField, datastoresField))), SCHEMA);
880         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
881                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
882
883         // testing
884         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
885                 + "<get>\n"
886                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
887                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
888                 + "<statistics>\n"
889                 + "<netconf-start-time/>\n"
890                 + "</statistics>\n"
891                 + "<datastores/>\n"
892                 + "</netconf-state>\n"
893                 + "</filter>\n"
894                 + "</get>\n"
895                 + "</rpc>");
896     }
897
898     @Test
899     public void getOverlappingFieldsTest() throws IOException, SAXException {
900         // preparation of the fields
901         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
902         final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
903         final YangInstanceIdentifier capabilityField = YangInstanceIdentifier.create(toId(Capabilities.QNAME),
904                 toId(QName.create(Capabilities.QNAME, "capability").intern()));
905         final YangInstanceIdentifier datastoreField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
906         final YangInstanceIdentifier locksFields = YangInstanceIdentifier.create(toId(Datastores.QNAME),
907                 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME));
908
909         // building filter structure and NETCONF message
910         final AnyxmlNode<?> filterStructure = toFilterStructure(
911                 List.of(FieldsFilter.of(parentYiid,
912                     List.of(capabilitiesField, capabilityField, datastoreField, locksFields))),
913                 SCHEMA);
914         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
915                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
916
917         // testing
918         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
919                 + "<get>\n"
920                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
921                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
922                 + "<capabilities/>\n"
923                 + "<datastores/>\n"
924                 + "</netconf-state>\n"
925                 + "</filter>\n"
926                 + "</get>\n"
927                 + "</rpc>");
928     }
929
930     @Test
931     public void getOverlappingFieldsWithEmptyFieldTest() throws IOException, SAXException {
932         // preparation of the fields
933         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
934         final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
935
936         // building filter structure and NETCONF message
937         final AnyxmlNode<?> filterStructure = toFilterStructure(
938                 List.of(FieldsFilter.of(parentYiid, List.of(capabilitiesField, YangInstanceIdentifier.empty()))),
939                 SCHEMA);
940         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
941                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
942
943         // testing
944         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
945                 + "<get>\n"
946                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
947                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"/>\n"
948                 + "</filter>\n"
949                 + "</get>\n"
950                 + "</rpc>");
951     }
952
953     @Test
954     public void getSpecificFieldsUnderListTest() throws IOException, SAXException {
955         // preparation of the fields
956         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
957                 toId(Schemas.QNAME), toId(Schema.QNAME), NodeIdentifierWithPredicates.of(Schema.QNAME));
958         final YangInstanceIdentifier versionField = YangInstanceIdentifier.create(
959                 toId(QName.create(Schema.QNAME, "version").intern()));
960         final YangInstanceIdentifier identifierField = YangInstanceIdentifier.create(
961                 toId(QName.create(Schema.QNAME, "namespace").intern()));
962
963         // building filter structure and NETCONF message
964         final AnyxmlNode<?> filterStructure = toFilterStructure(
965             List.of(FieldsFilter.of(parentYiid, List.of(versionField, identifierField))), SCHEMA);
966         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
967                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
968
969         // testing
970         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
971                 + "<get>\n"
972                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
973                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
974                 + "<schemas>\n"
975                 + "<schema>\n"
976                 + "<version/>\n"
977                 + "<namespace/>\n"
978                 // explicitly fetched list keys - identifier and format
979                 + "<identifier/>\n"
980                 + "<format/>\n"
981                 + "</schema>\n"
982                 + "</schemas>\n"
983                 + "</netconf-state>\n"
984                 + "</filter>\n"
985                 + "</get>\n"
986                 + "</rpc>");
987     }
988
989     @Test
990     public void getSpecificFieldsUnderMultipleLists() throws IOException, SAXException {
991         // preparation of the fields
992         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(
993                 toId(NetconfState.QNAME), toId(Datastores.QNAME));
994         final YangInstanceIdentifier partialLockYiid = YangInstanceIdentifier.create(toId(Datastore.QNAME),
995                 NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME),
996                 toId(QName.create(Locks.QNAME, "lock-type").intern()), toId(PartialLock.QNAME),
997                 NodeIdentifierWithPredicates.of(PartialLock.QNAME));
998         final YangInstanceIdentifier lockedTimeField = partialLockYiid.node(
999                 QName.create(Locks.QNAME, "locked-time").intern());
1000         final YangInstanceIdentifier lockedBySessionField = partialLockYiid.node(
1001                 QName.create(Locks.QNAME, "locked-by-session").intern());
1002
1003         // building filter structure and NETCONF message
1004         final AnyxmlNode<?> filterStructure = toFilterStructure(
1005             List.of(FieldsFilter.of(parentYiid, List.of(lockedTimeField, lockedBySessionField))),
1006             SCHEMA);
1007         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1008                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1009
1010         // testing
1011         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1012                 + "<get>\n"
1013                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1014                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1015                 + "<datastores>\n"
1016                 + "<datastore>\n"
1017                 + "<locks>\n"
1018                 + "<partial-lock>\n"
1019                 + "<locked-time/>\n"
1020                 + "<locked-by-session/>\n"
1021                 + "<lock-id/>\n"
1022                 + "</partial-lock>\n"
1023                 + "</locks>\n"
1024                 + "<name/>\n"
1025                 + "</datastore>\n"
1026                 + "</datastores>\n"
1027                 + "</netconf-state>\n"
1028                 + "</filter>\n"
1029                 + "</get>\n"
1030                 + "</rpc>");
1031     }
1032
1033     @Test
1034     public void getWholeListsUsingFieldsTest() throws IOException, SAXException {
1035         // preparation of the fields
1036         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
1037         final YangInstanceIdentifier datastoreListField = YangInstanceIdentifier.create(toId(Datastores.QNAME),
1038                 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME));
1039         final YangInstanceIdentifier sessionListField = YangInstanceIdentifier.create(toId(Sessions.QNAME),
1040                 toId(Session.QNAME), NodeIdentifierWithPredicates.of(Session.QNAME));
1041
1042         // building filter structure and NETCONF message
1043         final AnyxmlNode<?> filterStructure = toFilterStructure(
1044                 List.of(FieldsFilter.of(parentYiid, List.of(datastoreListField, sessionListField))), SCHEMA);
1045         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1046                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1047
1048         // testing
1049         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1050                 + "<get>\n"
1051                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1052                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1053                 + "<datastores>\n"
1054                 + "<datastore/>\n"
1055                 + "</datastores>\n"
1056                 + "<sessions>\n"
1057                 + "<session/>\n"
1058                 + "</sessions>\n"
1059                 + "</netconf-state>\n"
1060                 + "</filter>\n"
1061                 + "</get>\n"
1062                 + "</rpc>");
1063     }
1064
1065     @Test
1066     public void getSpecificListEntriesWithSpecificFieldsTest() throws IOException, SAXException {
1067         // preparation of the fields
1068         final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
1069                 toId(Sessions.QNAME));
1070         final QName sessionId = QName.create(Session.QNAME, "session-id").intern();
1071         final YangInstanceIdentifier session1Field = YangInstanceIdentifier.create(toId(Session.QNAME),
1072                 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 1));
1073         final YangInstanceIdentifier session2TransportField = YangInstanceIdentifier.create(toId(Session.QNAME),
1074                 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 2),
1075                 toId(QName.create(Session.QNAME, "transport").intern()));
1076
1077         // building filter structure and NETCONF message
1078         final AnyxmlNode<?> filterStructure = toFilterStructure(
1079                 List.of(FieldsFilter.of(parentYiid, List.of(session1Field, session2TransportField))), SCHEMA);
1080         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1081                 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1082
1083         // testing
1084         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1085                 + "<get>\n"
1086                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1087                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1088                 + "<sessions>\n"
1089                 + "<session>\n"
1090                 + "<session-id>1</session-id>\n"
1091                 + "</session>\n"
1092                 + "<session>\n"
1093                 + "<session-id>2</session-id>\n"
1094                 + "<transport/>\n"
1095                 + "</session>\n"
1096                 + "</sessions>\n"
1097                 + "</netconf-state>\n"
1098                 + "</filter>\n"
1099                 + "</get>\n"
1100                 + "</rpc>");
1101     }
1102
1103     @Test
1104     // Proof that YANGTOOLS-1362 works on DOM level
1105     public void testConfigChangeToNotification() throws SAXException, IOException {
1106         final var message = new NetconfMessage(XmlUtil.readXmlToDocument(
1107             "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
1108             + " <eventTime>2021-11-11T11:26:16Z</eventTime> \n"
1109             + "  <netconf-config-change xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-notifications\">\n"
1110             + "     <changed-by> \n"
1111             + "       <username>root</username> \n"
1112             + "       <session-id>3</session-id> \n"
1113             + "     </changed-by> \n"
1114             + "     <datastore>running</datastore> \n"
1115             + "     <edit> \n"
1116             + "        <target xmlns:ncm=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">/ncm:netconf-state"
1117             + "/ncm:datastores/ncm:datastore[ncm:name='running']</target>\n"
1118             + "        <operation>replace</operation> \n"
1119             + "     </edit> \n"
1120             + "  </netconf-config-change> \n"
1121             + "</notification>"));
1122
1123         final var change = netconfMessageTransformer.toNotification(message).getBody();
1124         final var editList = change.getChildByArg(new NodeIdentifier(Edit.QNAME));
1125         assertThat(editList, instanceOf(UnkeyedListNode.class));
1126         final var edits = ((UnkeyedListNode) editList).body();
1127         assertEquals(1, edits.size());
1128         final var edit = edits.iterator().next();
1129         final var target = edit.getChildByArg(new NodeIdentifier(QName.create(Edit.QNAME, "target"))).body();
1130         assertThat(target, instanceOf(YangInstanceIdentifier.class));
1131
1132         final var args = ((YangInstanceIdentifier) target).getPathArguments();
1133         assertEquals(4, args.size());
1134     }
1135
1136     private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
1137             final String inputNodeName, final String inputValue) {
1138         checkNode(action, actionQname.getLocalName(), actionQname.getLocalName(),
1139                 actionQname.getNamespace().toString());
1140
1141         Node childResetAt = action.getFirstChild();
1142         checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
1143
1144         Node firstChild = childResetAt.getFirstChild();
1145         assertEquals(firstChild.getNodeValue(), inputValue);
1146     }
1147
1148     private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
1149         Node baseRpc = actionRequest.getDocument().getFirstChild();
1150         checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
1151         assertTrue(baseRpc.getLocalName().equals("rpc"));
1152         assertTrue(baseRpc.getNodeName().equals("rpc"));
1153
1154         Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
1155         assertNotNull(messageId);
1156         assertTrue(messageId.getNodeValue().contains("m-"));
1157         Node childAction = baseRpc.getFirstChild();
1158
1159         checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
1160         return childAction;
1161     }
1162
1163     private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
1164         YangInstanceIdentifier yangInstanceIdentifier =
1165                 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
1166         DOMDataTreeIdentifier domDataTreeIdentifier =
1167                 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
1168                         yangInstanceIdentifier);
1169         return domDataTreeIdentifier;
1170     }
1171
1172     private static ContainerNode initInputAction(final QName qname, final String value) {
1173         return ImmutableContainerNodeBuilder.create()
1174             .withNodeIdentifier(NodeIdentifier.create(QName.create(qname, "input")))
1175             .withChild(ImmutableNodes.leafNode(qname, value))
1176             .build();
1177     }
1178
1179     private static ContainerNode initEmptyInputAction(final QName qname) {
1180         return ImmutableContainerNodeBuilder.create()
1181             .withNodeIdentifier(NodeIdentifier.create(QName.create(qname, "input")))
1182             .build();
1183     }
1184
1185     private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
1186             final String expectedNamespace) {
1187         assertNotNull(childServer);
1188         assertEquals(childServer.getLocalName(), expectedLocalName);
1189         assertEquals(childServer.getNodeName(), expectedNodeName);
1190         assertEquals(childServer.getNamespaceURI(), expectedNamespace);
1191     }
1192 }