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