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