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