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