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