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