Bump upstreams for Silicon
[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.ImmutableList;
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.Collections;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.Set;
43 import javax.xml.transform.dom.DOMSource;
44 import org.custommonkey.xmlunit.Diff;
45 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
46 import org.custommonkey.xmlunit.XMLUnit;
47 import org.hamcrest.CoreMatchers;
48 import org.junit.AfterClass;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
53 import org.opendaylight.mdsal.dom.api.DOMActionResult;
54 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
55 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
56 import org.opendaylight.netconf.api.NetconfMessage;
57 import org.opendaylight.netconf.api.xml.XmlUtil;
58 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
59 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
60 import org.opendaylight.netconf.sal.connect.netconf.util.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.Schemas;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
68 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
69 import org.opendaylight.yangtools.yang.common.QName;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
76 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
79 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
82 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
83 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
84 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
86 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
87 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
88 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
89 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
90 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
91 import org.w3c.dom.Node;
92 import org.xml.sax.SAXException;
93
94 public class NetconfMessageTransformerTest extends AbstractBaseSchemasTest {
95
96     private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
97     private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
98
99     private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
100     private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
101
102     private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
103
104     private static final String URN_EXAMPLE_AUGMENTED_ACTION = "urn:example:augmented-action";
105
106     private static final String URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS = "urn:example:rpcs-actions-outputs";
107
108     private static final QName SERVER_QNAME =
109             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
110     private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
111     private static final Absolute RESET_SERVER_PATH = Absolute.of(SERVER_QNAME, RESET_QNAME);
112     private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
113             REVISION_EXAMPLE_SERVER_FARM_2, "applications");
114     private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
115     private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
116     private static final Absolute KILL_SERVER_APP_PATH =
117             Absolute.of(SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
118
119     private static final QName DEVICE_QNAME =
120             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
121     private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
122     private static final Absolute START_DEVICE_PATH = Absolute.of(DEVICE_QNAME, START_QNAME);
123     private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
124     private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
125     private static final Absolute ENABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
126
127     private static final QName DISABLE_QNAME = QName.create(URN_EXAMPLE_AUGMENTED_ACTION, "disable");
128     private static final Absolute DISABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, DISABLE_QNAME);
129
130     private static final QName CHECK_WITH_OUTPUT_QNAME =
131             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-with-output");
132     private static final Absolute CHECK_WITH_OUTPUT_INTERFACE_PATH =
133             Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITH_OUTPUT_QNAME);
134     private static final QName CHECK_WITHOUT_OUTPUT_QNAME =
135             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-without-output");
136     private static final Absolute CHECK_WITHOUT_OUTPUT_INTERFACE_PATH =
137             Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITHOUT_OUTPUT_QNAME);
138     private static final QName RPC_WITH_OUTPUT_QNAME =
139             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-with-output");
140     private static final QName RPC_WITHOUT_OUTPUT_QNAME =
141             QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-without-output");
142
143     private static final QName BOX_OUT_QNAME =
144             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
145     private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
146     private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
147     private static final Absolute OPEN_BOXES_PATH = Absolute.of(BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
148
149     private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
150     private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
151     private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
152     private static final Absolute XYZZY_FOO_PATH = Absolute.of(FOO_QNAME, XYZZY_QNAME);
153     private static final Absolute XYZZY_BAR_PATH = Absolute.of(BAR_QNAME, XYZZY_QNAME);
154
155     private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
156     private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
157     private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
158     private static final Absolute CHOICE_ACTION_PATH =
159             Absolute.of(CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
160
161     private static EffectiveModelContext PARTIAL_SCHEMA;
162     private static EffectiveModelContext SCHEMA;
163     private static EffectiveModelContext ACTION_SCHEMA;
164
165     private NetconfMessageTransformer actionNetconfMessageTransformer;
166     private NetconfMessageTransformer netconfMessageTransformer;
167
168     @BeforeClass
169     public static void beforeClass() {
170         PARTIAL_SCHEMA = BindingRuntimeHelpers.createEffectiveModel(NetconfState.class);
171         SCHEMA = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class);
172         ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
173             "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
174             "/schemas/conflicting-actions.yang", "/schemas/augmented-action.yang",
175             "/schemas/rpcs-actions-outputs.yang");
176     }
177
178     @AfterClass
179     public static void afterClass() {
180         PARTIAL_SCHEMA = null;
181         SCHEMA = null;
182         ACTION_SCHEMA = null;
183     }
184
185     @Before
186     public void setUp() throws Exception {
187         XMLUnit.setIgnoreWhitespace(true);
188         XMLUnit.setIgnoreAttributeOrder(true);
189         XMLUnit.setIgnoreComments(true);
190
191         netconfMessageTransformer = getTransformer(SCHEMA);
192         actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
193             true, BASE_SCHEMAS.getBaseSchema());
194     }
195
196     @Test
197     public void testLockRequestBaseSchemaNotPresent() throws Exception {
198         final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
199         final NetconfMessage netconfMessage = transformer.toRpcRequest(NETCONF_LOCK_QNAME,
200                 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
201
202         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
203         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
204     }
205
206     @Test
207     public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
208         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
209             true, BASE_SCHEMAS.getBaseSchemaWithNotifications());
210         NetconfMessage netconfMessage = transformer.toRpcRequest(CREATE_SUBSCRIPTION_RPC_QNAME,
211                 CREATE_SUBSCRIPTION_RPC_CONTENT);
212         String documentString = XmlUtil.toString(netconfMessage.getDocument());
213         assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
214         assertThat(documentString, CoreMatchers.containsString("<rpc"));
215     }
216
217     @Test
218     public void tesLockSchemaRequest() throws Exception {
219         final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
220         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
221
222         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), NETCONF_LOCK_QNAME);
223     }
224
225     @Test
226     public void testRpcEmptyBodyWithOutputDefinedSchemaResult() throws Exception {
227         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
228
229         DOMRpcResult domRpcResult = actionNetconfMessageTransformer
230                 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITH_OUTPUT_QNAME);
231         assertNotNull(domRpcResult);
232     }
233
234     @Test
235     public void testRpcEmptyBodyWithoutOutputDefinedSchemaResult() 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_WITHOUT_OUTPUT_QNAME);
240         assertNotNull(domRpcResult);
241     }
242
243     @Test
244     public void testDiscardChangesRequest() throws Exception {
245         final NetconfMessage netconfMessage =
246                 netconfMessageTransformer.toRpcRequest(NETCONF_DISCARD_CHANGES_QNAME, null);
247         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
248         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
249         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
250     }
251
252     @Test
253     public void testGetSchemaRequest() throws Exception {
254         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(GET_SCHEMA_QNAME,
255                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
256         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
257                 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
258                 + "<format>yang</format>\n"
259                 + "<identifier>module</identifier>\n"
260                 + "<version>2012-12-12</version>\n"
261                 + "</get-schema>\n"
262                 + "</rpc>");
263     }
264
265     @Test
266     public void testGetSchemaResponse() throws Exception {
267         final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
268         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
269                 "<rpc-reply message-id=\"101\"\n"
270                         + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
271                         + "<data\n"
272                         + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
273                         + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
274                         + "Random YANG SCHEMA\n"
275                         + "</xs:schema>\n"
276                         + "</data>\n"
277                         + "</rpc-reply>"
278         ));
279         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, GET_SCHEMA_QNAME);
280         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
281         assertNotNull(compositeNodeRpcResult.getResult());
282         final DOMSource schemaContent = ((DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
283                 .getValue().iterator().next()).getValue();
284         assertThat(schemaContent.getNode().getTextContent(),
285                 CoreMatchers.containsString("Random YANG SCHEMA"));
286     }
287
288     @Test
289     public void testGetConfigResponse() throws Exception {
290         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
291                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
292                 + "<data>\n"
293                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
294                 + "<schemas>\n"
295                 + "<schema>\n"
296                 + "<identifier>module</identifier>\n"
297                 + "<version>2012-12-12</version>\n"
298                 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
299                 + "</schema>\n"
300                 + "</schemas>\n"
301                 + "</netconf-state>\n"
302                 + "</data>\n"
303                 + "</rpc-reply>"));
304
305         final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
306         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, NETCONF_GET_CONFIG_QNAME);
307         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
308         assertNotNull(compositeNodeRpcResult.getResult());
309
310         final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
311                 NetconfRemoteSchemaYangSourceProvider
312                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
313
314         final Map<QName, Object> keys = new HashMap<>();
315         for (final DataContainerChild<?, ?> value : values) {
316             keys.put(value.getNodeType(), value.getValue());
317         }
318
319         final NodeIdentifierWithPredicates identifierWithPredicates =
320                 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
321         final MapEntryNode schemaNode =
322                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
323
324         final DOMSourceAnyxmlNode data = (DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
325                 .getChild(toId(NETCONF_DATA_QNAME)).get();
326
327         NormalizedNodeResult nodeResult =
328                 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
329         ContainerNode result = (ContainerNode) nodeResult.getResult();
330         final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
331         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
332         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
333         assertEquals(1, Iterables.size(schemaParent.getValue()));
334
335         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
336     }
337
338     @Test
339     public void testGetConfigLeafRequest() throws Exception {
340         final DataContainerChild<?, ?> filter = toFilterStructure(
341                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
342                     NodeIdentifierWithPredicates.of(Schema.QNAME),
343                     toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
344
345         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
346
347         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
348                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
349
350         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
351                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
352                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
353                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
354                 + "<schemas>\n"
355                 + "<schema>\n"
356                 + "<version/>\n"
357                 + "</schema>\n"
358                 + "</schemas>\n"
359                 + "</netconf-state>\n"
360                 + "</filter>\n"
361                 + "<source>\n"
362                 + "<running/>\n"
363                 + "</source>\n"
364                 + "</get-config>\n"
365                 + "</rpc>");
366     }
367
368     @Test
369     public void testGetConfigRequest() throws Exception {
370         final DataContainerChild<?, ?> filter = toFilterStructure(
371                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
372
373         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
374
375         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
376                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
377
378         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
379                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
380                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
381                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
382                 + "<schemas/>\n"
383                 + "</netconf-state>"
384                 + "</filter>\n"
385                 + "<source>\n"
386                 + "<running/>\n"
387                 + "</source>\n"
388                 + "</get-config>"
389                 + "</rpc>");
390     }
391
392     @Test
393     public void testEditConfigRequest() throws Exception {
394         final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
395                 NetconfRemoteSchemaYangSourceProvider
396                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
397
398         final Map<QName, Object> keys = new HashMap<>();
399         for (final DataContainerChild<?, ?> value : values) {
400             keys.put(value.getNodeType(), value.getValue());
401         }
402
403         final NodeIdentifierWithPredicates identifierWithPredicates =
404                 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
405         final MapEntryNode schemaNode =
406                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
407
408         final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
409                 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
410                 .nodeWithKey(Schema.QNAME, keys).build();
411         final DataContainerChild<?, ?> editConfigStructure =
412                 createEditConfigStructure(BASE_SCHEMAS.getBaseSchemaWithNotifications().getEffectiveModelContext(), id,
413                     Optional.empty(), Optional.ofNullable(schemaNode));
414
415         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
416
417         final ContainerNode wrap =
418                 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
419         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_EDIT_CONFIG_QNAME, wrap);
420
421         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
422                 + "<edit-config>\n"
423                 + "<target>\n"
424                 + "<candidate/>\n"
425                 + "</target>\n"
426                 + "<config>\n"
427                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
428                 + "<schemas>\n"
429                 + "<schema>\n"
430                 + "<identifier>module</identifier>\n"
431                 + "<version>2012-12-12</version>\n"
432                 + "<format>yang</format>\n"
433                 + "</schema>\n"
434                 + "</schemas>\n"
435                 + "</netconf-state>\n"
436                 + "</config>\n"
437                 + "</edit-config>\n"
438                 + "</rpc>");
439     }
440
441     private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
442             throws SAXException, IOException {
443         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
444         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
445         assertTrue(diff.toString(), diff.similar());
446     }
447
448     @Test
449     public void testGetRequest() throws Exception {
450
451         final QName capability = QName.create(Capabilities.QNAME, "capability");
452         final DataContainerChild<?, ?> filter = toFilterStructure(
453                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
454                     new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
455
456         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
457                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
458
459         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
460                 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
461                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
462                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
463                 + "<capabilities>\n"
464                 + "<capability>a:b:c</capability>\n"
465                 + "</capabilities>\n"
466                 + "</netconf-state>"
467                 + "</filter>\n"
468                 + "</get>"
469                 + "</rpc>");
470     }
471
472     private static NetconfMessageTransformer getTransformer(final EffectiveModelContext schema) {
473         return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true, BASE_SCHEMAS.getBaseSchema());
474     }
475
476     @Test
477     public void testCommitResponse() throws Exception {
478         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
479                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
480         ));
481         final DOMRpcResult compositeNodeRpcResult =
482                 netconfMessageTransformer.toRpcResult(response, NETCONF_COMMIT_QNAME);
483         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
484         assertNull(compositeNodeRpcResult.getResult());
485     }
486
487     @Test
488     public void getActionsTest() {
489         Set<Absolute> schemaPaths = new HashSet<>();
490         schemaPaths.add(RESET_SERVER_PATH);
491         schemaPaths.add(START_DEVICE_PATH);
492         schemaPaths.add(ENABLE_INTERFACE_PATH);
493         schemaPaths.add(OPEN_BOXES_PATH);
494         schemaPaths.add(KILL_SERVER_APP_PATH);
495         schemaPaths.add(XYZZY_FOO_PATH);
496         schemaPaths.add(XYZZY_BAR_PATH);
497         schemaPaths.add(CHOICE_ACTION_PATH);
498         schemaPaths.add(DISABLE_INTERFACE_PATH);
499         schemaPaths.add(CHECK_WITH_OUTPUT_INTERFACE_PATH);
500         schemaPaths.add(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH);
501
502         List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
503         assertEquals(schemaPaths.size(), actions.size());
504         for (ActionDefinition actionDefinition : actions) {
505             Absolute path = Absolute.of(ImmutableList.copyOf(actionDefinition.getPath().getPathFromRoot()));
506             assertTrue(schemaPaths.remove(path));
507         }
508     }
509
510     @Test
511     public void toActionRequestListTopLevelTest() {
512         QName nameQname = QName.create(SERVER_QNAME, "name");
513         List<PathArgument> nodeIdentifiers = new ArrayList<>();
514         nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
515         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
516         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
517
518         ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
519
520         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
521                 RESET_SERVER_PATH, domDataTreeIdentifier, data);
522
523         Node childAction = checkBasePartOfActionRequest(actionRequest);
524
525         Node childServer = childAction.getFirstChild();
526         checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
527
528         Node childName = childServer.getFirstChild();
529         checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
530
531         Node childTest = childName.getFirstChild();
532         assertEquals(childTest.getNodeValue(), "test");
533
534         checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
535     }
536
537     @Test
538     public void toActionRequestContainerTopLevelTest() {
539         List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(DEVICE_QNAME));
540         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
541
542         NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
543         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
544                 START_DEVICE_PATH, domDataTreeIdentifier, payload);
545
546         Node childAction = checkBasePartOfActionRequest(actionRequest);
547
548         Node childDevice = childAction.getFirstChild();
549         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
550
551         checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
552     }
553
554     @Test
555     public void toActionRequestContainerInContainerTest() {
556         List<PathArgument> nodeIdentifiers = new ArrayList<>();
557         nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
558         nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
559
560         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
561
562         NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
563         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
564                 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
565
566         Node childAction = checkBasePartOfActionRequest(actionRequest);
567
568         Node childBoxOut = childAction.getFirstChild();
569         checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
570
571         Node childBoxIn = childBoxOut.getFirstChild();
572         checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
573
574         Node action = childBoxIn.getFirstChild();
575         checkNode(action, OPEN_QNAME.getLocalName(), OPEN_QNAME.getLocalName(), OPEN_QNAME.getNamespace().toString());
576     }
577
578     @Test
579     public void toActionRequestListInContainerTest() {
580         QName nameQname = QName.create(INTERFACE_QNAME, "name");
581
582         List<PathArgument> nodeIdentifiers = new ArrayList<>();
583         nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
584         nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
585         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
586
587         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
588
589         NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
590         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
591                 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
592
593         Node childAction = checkBasePartOfActionRequest(actionRequest);
594
595         Node childDevice = childAction.getFirstChild();
596         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
597
598         Node childInterface = childDevice.getFirstChild();
599         checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
600
601         Node childName = childInterface.getFirstChild();
602         checkNode(childName, "name", "name", nameQname.getNamespace().toString());
603
604         Node childTest = childName.getFirstChild();
605         assertEquals(childTest.getNodeValue(), "test");
606
607         Node action = childInterface.getLastChild();
608         checkNode(action, ENABLE_QNAME.getLocalName(), ENABLE_QNAME.getLocalName(),
609                 ENABLE_QNAME.getNamespace().toString());
610     }
611
612     @Test
613     public void toActionRequestListInContainerAugmentedIntoListTest() {
614         QName serverNameQname = QName.create(SERVER_QNAME, "name");
615         QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
616
617         List<PathArgument> nodeIdentifiers = new ArrayList<>();
618         nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
619         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
620         nodeIdentifiers.add(new AugmentationIdentifier(Collections.singleton(APPLICATIONS_QNAME)));
621         nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
622         nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
623         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
624                 applicationNameQname, "testApplication"));
625
626         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
627
628         NormalizedNode<?, ?> payload = initEmptyInputAction(APPLICATION_QNAME);
629         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
630                 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
631
632         Node childAction = checkBasePartOfActionRequest(actionRequest);
633
634         Node childServer = childAction.getFirstChild();
635         checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
636
637         Node childServerName = childServer.getFirstChild();
638         checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
639
640         Node childServerNameTest = childServerName.getFirstChild();
641         assertEquals(childServerNameTest.getNodeValue(), "testServer");
642
643         Node childApplications = childServer.getLastChild();
644         checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
645
646         Node childApplication = childApplications.getFirstChild();
647         checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
648
649         Node childApplicationName = childApplication.getFirstChild();
650         checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
651
652         Node childApplicationNameTest = childApplicationName.getFirstChild();
653         assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
654
655         Node childKillAction = childApplication.getLastChild();
656         checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
657         checkNode(childKillAction, KILL_QNAME.getLocalName(), KILL_QNAME.getLocalName(),
658                 KILL_QNAME.getNamespace().toString());
659     }
660
661     @Test
662     public void toActionRequestConflictingInListTest() {
663         QName barInputQname = QName.create(BAR_QNAME, "bar");
664         QName barIdQname = QName.create(BAR_QNAME, "bar-id");
665         Byte barInput = 1;
666
667         List<PathArgument> nodeIdentifiers = new ArrayList<>();
668         nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
669         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
670
671         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
672
673         ImmutableLeafNodeBuilder<Byte> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
674         DataContainerChild<NodeIdentifier, Byte> build = immutableLeafNodeBuilder.withNodeIdentifier(
675                 NodeIdentifier.create(barInputQname)).withValue(barInput).build();
676         NormalizedNode<?, ?> payload = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
677                 QName.create(barInputQname, "input"))).withChild(build).build();
678
679         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
680                 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
681
682         Node childAction = checkBasePartOfActionRequest(actionRequest);
683
684         Node childBar = childAction.getFirstChild();
685         checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
686
687         Node childBarId = childBar.getFirstChild();
688         checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
689
690         Node childTest = childBarId.getFirstChild();
691         assertEquals(childTest.getNodeValue(), "test");
692
693         Node action = childBar.getLastChild();
694         checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
695                 XYZZY_QNAME.getNamespace().toString());
696     }
697
698     @Test
699     public void toActionRequestConflictingInContainerTest() {
700         QName fooInputQname = QName.create(FOO_QNAME, "foo");
701
702         List<PathArgument> nodeIdentifiers = new ArrayList<>();
703         nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
704         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
705         NormalizedNode<?, ?> payload = initInputAction(fooInputQname, "test");
706
707         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
708                 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
709
710         Node childAction = checkBasePartOfActionRequest(actionRequest);
711
712         Node childBar = childAction.getFirstChild();
713         checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
714
715         Node action = childBar.getLastChild();
716         checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
717                 XYZZY_QNAME.getNamespace().toString());
718     }
719
720     @Test
721     public void toActionRequestChoiceTest() {
722         List<PathArgument> nodeIdentifiers = new ArrayList<>();
723         nodeIdentifiers.add(NodeIdentifier.create(CONFLICT_CHOICE_QNAME));
724         nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
725         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
726         NormalizedNode<?, ?> payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
727
728         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
729                 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
730
731         Node childAction = checkBasePartOfActionRequest(actionRequest);
732
733         Node childChoiceCont = childAction.getFirstChild();
734         checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
735
736         Node action = childChoiceCont.getLastChild();
737         checkNode(action, CHOICE_ACTION_QNAME.getLocalName(), CHOICE_ACTION_QNAME.getLocalName(),
738                 CHOICE_ACTION_QNAME.getNamespace().toString());
739     }
740
741     @Test
742     public void toAugmentedActionRequestListInContainerTest() {
743         QName nameQname = QName.create(INTERFACE_QNAME, "name");
744
745         List<PathArgument> nodeIdentifiers = new ArrayList<>();
746         nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
747         nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
748         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
749
750         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
751
752         NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
753         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
754                 DISABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
755
756         Node childAction = checkBasePartOfActionRequest(actionRequest);
757
758         Node childDevice = childAction.getFirstChild();
759         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
760
761         Node childInterface = childDevice.getFirstChild();
762         checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
763
764         Node childName = childInterface.getFirstChild();
765         checkNode(childName, "name", "name", nameQname.getNamespace().toString());
766
767         Node childTest = childName.getFirstChild();
768         assertEquals(childTest.getNodeValue(), "test");
769
770         Node action = childInterface.getLastChild();
771         checkNode(action, DISABLE_QNAME.getLocalName(), DISABLE_QNAME.getLocalName(),
772                 DISABLE_QNAME.getNamespace().toString());
773     }
774
775     @SuppressWarnings({ "rawtypes", "unchecked" })
776     @Test
777     public void toActionResultTest() throws Exception {
778         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
779                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
780                 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
781                 + "now"
782                 + "</reset-finished-at>"
783                 + "</rpc-reply>"));
784         DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
785         assertNotNull(actionResult);
786         ContainerNode containerNode = actionResult.getOutput().get();
787         assertNotNull(containerNode);
788         LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
789         assertEquals("now", leaf.getValue());
790     }
791
792     @Test
793     public void toActionEmptyBodyWithOutputDefinedResultTest() throws Exception {
794         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
795                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
796                 + "<ok/>"
797                 + "</rpc-reply>"));
798         DOMActionResult actionResult =
799                 actionNetconfMessageTransformer.toActionResult(CHECK_WITH_OUTPUT_INTERFACE_PATH, message);
800         assertNotNull(actionResult);
801         assertTrue(actionResult.getOutput().isEmpty());
802     }
803
804     @Test
805     public void toActionEmptyBodyWithoutOutputDefinedResultTest() throws Exception {
806         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
807                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
808                 + "<ok/>"
809                 + "</rpc-reply>"));
810         DOMActionResult actionResult =
811                 actionNetconfMessageTransformer.toActionResult(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH, message);
812         assertNotNull(actionResult);
813         assertTrue(actionResult.getOutput().isEmpty());
814     }
815
816     private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
817             final String inputNodeName, final String inputValue) {
818         checkNode(action, actionQname.getLocalName(), actionQname.getLocalName(),
819                 actionQname.getNamespace().toString());
820
821         Node childResetAt = action.getFirstChild();
822         checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
823
824         Node firstChild = childResetAt.getFirstChild();
825         assertEquals(firstChild.getNodeValue(), inputValue);
826     }
827
828     private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
829         Node baseRpc = actionRequest.getDocument().getFirstChild();
830         checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
831         assertTrue(baseRpc.getLocalName().equals("rpc"));
832         assertTrue(baseRpc.getNodeName().equals("rpc"));
833
834         Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
835         assertNotNull(messageId);
836         assertTrue(messageId.getNodeValue().contains("m-"));
837
838         Node childAction = baseRpc.getFirstChild();
839         checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
840         return childAction;
841     }
842
843     private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
844         YangInstanceIdentifier yangInstanceIdentifier =
845                 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
846         DOMDataTreeIdentifier domDataTreeIdentifier =
847                 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
848                         yangInstanceIdentifier);
849         return domDataTreeIdentifier;
850     }
851
852     private static ContainerNode initInputAction(final QName qname, final String value) {
853         ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
854         DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
855                 NodeIdentifier.create(qname)).withValue(value).build();
856         ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
857                 QName.create(qname, "input"))).withChild(build).build();
858         return data;
859     }
860
861     private static ContainerNode initEmptyInputAction(final QName qname) {
862         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
863                 QName.create(qname, "input"))).build();
864     }
865
866     private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
867             final String expectedNamespace) {
868         assertNotNull(childServer);
869         assertEquals(childServer.getLocalName(), expectedLocalName);
870         assertEquals(childServer.getNodeName(), expectedNodeName);
871         assertEquals(childServer.getNamespaceURI(), expectedNamespace);
872     }
873 }