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