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