Update NodeIdentifierWithPredicates construction
[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.yang.common.QName;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.NodeWithValue;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
75 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
78 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
82 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
83 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
84 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
86 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
87 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
88 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
89 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
90 import org.w3c.dom.Node;
91 import org.xml.sax.SAXException;
92
93 public class NetconfMessageTransformerTest {
94
95     private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
96     private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
97
98     private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
99     private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
100
101     private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
102
103     private static final QName SERVER_QNAME =
104             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
105     private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
106     private static final SchemaPath RESET_SERVER_PATH = SchemaPath.create(true, SERVER_QNAME, RESET_QNAME);
107     private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
108             REVISION_EXAMPLE_SERVER_FARM_2, "applications");
109     private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
110     private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
111     private static final SchemaPath KILL_SERVER_APP_PATH =
112             SchemaPath.create(true, SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
113
114     private static final QName DEVICE_QNAME =
115             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
116     private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
117     private static final SchemaPath START_DEVICE_PATH = SchemaPath.create(true, DEVICE_QNAME, START_QNAME);
118     private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
119     private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
120     private static final SchemaPath ENABLE_INTERFACE_PATH =
121             SchemaPath.create(true, DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
122
123     private static final QName BOX_OUT_QNAME =
124             QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
125     private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
126     private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
127     private static final SchemaPath OPEN_BOXES_PATH =
128             SchemaPath.create(true, BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
129
130     private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
131     private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
132     private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
133     private static final SchemaPath XYZZY_FOO_PATH = SchemaPath.create(true, FOO_QNAME, XYZZY_QNAME);
134     private static final SchemaPath XYZZY_BAR_PATH = SchemaPath.create(true, BAR_QNAME, XYZZY_QNAME);
135
136     private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
137     private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
138     private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
139     private static final SchemaPath CHOICE_ACTION_PATH =
140             SchemaPath.create(true, CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
141
142     private static SchemaContext PARTIAL_SCHEMA;
143     private static SchemaContext SCHEMA;
144     private static SchemaContext ACTION_SCHEMA;
145
146     private NetconfMessageTransformer actionNetconfMessageTransformer;
147     private NetconfMessageTransformer netconfMessageTransformer;
148
149     @BeforeClass
150     public static void beforeClass() {
151         final ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
152         context.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
153             .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
154         PARTIAL_SCHEMA = context.tryToCreateSchemaContext().get();
155
156         context.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
157         SCHEMA = context.tryToCreateSchemaContext().get();
158
159         ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
160             "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
161             "/schemas/conflicting-actions.yang");
162     }
163
164     @AfterClass
165     public static void afterClass() {
166         PARTIAL_SCHEMA = null;
167         SCHEMA = null;
168         ACTION_SCHEMA = null;
169     }
170
171     @Before
172     public void setUp() throws Exception {
173         XMLUnit.setIgnoreWhitespace(true);
174         XMLUnit.setIgnoreAttributeOrder(true);
175         XMLUnit.setIgnoreComments(true);
176
177         netconfMessageTransformer = getTransformer(SCHEMA);
178         actionNetconfMessageTransformer = new NetconfMessageTransformer(ACTION_SCHEMA, true);
179     }
180
181     @Test
182     public void testLockRequestBaseSchemaNotPresent() throws Exception {
183         final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
184         final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
185                 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
186
187         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
188         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
189     }
190
191     @Test
192     public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
193         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(SCHEMA, true,
194                 BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS);
195         NetconfMessage netconfMessage = transformer.toRpcRequest(
196                 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
197                 CREATE_SUBSCRIPTION_RPC_CONTENT
198         );
199         String documentString = XmlUtil.toString(netconfMessage.getDocument());
200         assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
201         assertThat(documentString, CoreMatchers.containsString("<rpc"));
202     }
203
204     @Test
205     public void tesLockSchemaRequest() throws Exception {
206         final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
207         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
208
209         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
210     }
211
212     @Test
213     public void testDiscardChangesRequest() throws Exception {
214         final NetconfMessage netconfMessage =
215                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
216         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
217         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
218         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
219     }
220
221     @Test
222     public void testGetSchemaRequest() throws Exception {
223         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
224                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
225         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
226                 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
227                 + "<format>yang</format>\n"
228                 + "<identifier>module</identifier>\n"
229                 + "<version>2012-12-12</version>\n"
230                 + "</get-schema>\n"
231                 + "</rpc>");
232     }
233
234     @Test
235     public void testGetSchemaResponse() throws Exception {
236         final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
237         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
238                 "<rpc-reply message-id=\"101\"\n"
239                         + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
240                         + "<data\n"
241                         + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
242                         + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
243                         + "Random YANG SCHEMA\n"
244                         + "</xs:schema>\n"
245                         + "</data>\n"
246                         + "</rpc-reply>"
247         ));
248         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
249         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
250         assertNotNull(compositeNodeRpcResult.getResult());
251         final DOMSource schemaContent =
252             ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
253         assertThat(schemaContent.getNode().getTextContent(),
254                 CoreMatchers.containsString("Random YANG SCHEMA"));
255     }
256
257     @Test
258     public void testGetConfigResponse() throws Exception {
259         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
260                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
261                 + "<data>\n"
262                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
263                 + "<schemas>\n"
264                 + "<schema>\n"
265                 + "<identifier>module</identifier>\n"
266                 + "<version>2012-12-12</version>\n"
267                 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
268                 + "</schema>\n"
269                 + "</schemas>\n"
270                 + "</netconf-state>\n"
271                 + "</data>\n"
272                 + "</rpc-reply>"));
273
274         final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
275         final DOMRpcResult compositeNodeRpcResult =
276                 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
277         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
278         assertNotNull(compositeNodeRpcResult.getResult());
279
280         final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
281                 NetconfRemoteSchemaYangSourceProvider
282                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
283
284         final Map<QName, Object> keys = new HashMap<>();
285         for (final DataContainerChild<?, ?> value : values) {
286             keys.put(value.getNodeType(), value.getValue());
287         }
288
289         final NodeIdentifierWithPredicates identifierWithPredicates =
290                 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
291         final MapEntryNode schemaNode =
292                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
293
294         final AnyXmlNode data = (AnyXmlNode) ((ContainerNode) compositeNodeRpcResult
295                 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
296
297         NormalizedNodeResult nodeResult =
298                 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
299         ContainerNode result = (ContainerNode) nodeResult.getResult();
300         final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
301         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
302         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
303         assertEquals(1, Iterables.size(schemaParent.getValue()));
304
305         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
306     }
307
308     @Test
309     public void testGetConfigLeafRequest() throws Exception {
310         final DataContainerChild<?, ?> filter = toFilterStructure(
311                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
312                     NodeIdentifierWithPredicates.of(Schema.QNAME),
313                     toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
314
315         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
316
317         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
318                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
319
320         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
321                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
322                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
323                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
324                 + "<schemas>\n"
325                 + "<schema>\n"
326                 + "<version/>\n"
327                 + "</schema>\n"
328                 + "</schemas>\n"
329                 + "</netconf-state>\n"
330                 + "</filter>\n"
331                 + "<source>\n"
332                 + "<running/>\n"
333                 + "</source>\n"
334                 + "</get-config>\n"
335                 + "</rpc>");
336     }
337
338     @Test
339     public void testGetConfigRequest() throws Exception {
340         final DataContainerChild<?, ?> filter = toFilterStructure(
341                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
342
343         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
344
345         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
346                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
347
348         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
349                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
350                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
351                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
352                 + "<schemas/>\n"
353                 + "</netconf-state>"
354                 + "</filter>\n"
355                 + "<source>\n"
356                 + "<running/>\n"
357                 + "</source>\n"
358                 + "</get-config>"
359                 + "</rpc>");
360     }
361
362     @Test
363     public void testEditConfigRequest() throws Exception {
364         final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
365                 NetconfRemoteSchemaYangSourceProvider
366                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
367
368         final Map<QName, Object> keys = new HashMap<>();
369         for (final DataContainerChild<?, ?> value : values) {
370             keys.put(value.getNodeType(), value.getValue());
371         }
372
373         final NodeIdentifierWithPredicates identifierWithPredicates =
374                 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
375         final MapEntryNode schemaNode =
376                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
377
378         final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
379                 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
380                 .nodeWithKey(Schema.QNAME, keys).build();
381         final DataContainerChild<?, ?> editConfigStructure =
382                 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
383                     Optional.empty(), Optional.ofNullable(schemaNode));
384
385         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
386
387         final ContainerNode wrap =
388                 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
389         final NetconfMessage netconfMessage =
390                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
391
392         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
393                 + "<edit-config>\n"
394                 + "<target>\n"
395                 + "<candidate/>\n"
396                 + "</target>\n"
397                 + "<config>\n"
398                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
399                 + "<schemas>\n"
400                 + "<schema>\n"
401                 + "<identifier>module</identifier>\n"
402                 + "<version>2012-12-12</version>\n"
403                 + "<format>yang</format>\n"
404                 + "</schema>\n"
405                 + "</schemas>\n"
406                 + "</netconf-state>\n"
407                 + "</config>\n"
408                 + "</edit-config>\n"
409                 + "</rpc>");
410     }
411
412     private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
413             throws SAXException, IOException {
414         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
415         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
416         assertTrue(diff.toString(), diff.similar());
417     }
418
419     @Test
420     public void testGetRequest() throws Exception {
421
422         final QName capability = QName.create(Capabilities.QNAME, "capability");
423         final DataContainerChild<?, ?> filter = toFilterStructure(
424                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
425                     new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
426
427         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
428                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
429
430         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
431                 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
432                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
433                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
434                 + "<capabilities>\n"
435                 + "<capability>a:b:c</capability>\n"
436                 + "</capabilities>\n"
437                 + "</netconf-state>"
438                 + "</filter>\n"
439                 + "</get>"
440                 + "</rpc>");
441     }
442
443     private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
444         return new NetconfMessageTransformer(schema, true);
445     }
446
447     @Test
448     public void testCommitResponse() throws Exception {
449         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
450                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
451         ));
452         final DOMRpcResult compositeNodeRpcResult =
453                 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
454         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
455         assertNull(compositeNodeRpcResult.getResult());
456     }
457
458     @Test
459     public void getActionsTest() {
460         Set<SchemaPath> schemaPaths = new HashSet<>();
461         schemaPaths.add(RESET_SERVER_PATH);
462         schemaPaths.add(START_DEVICE_PATH);
463         schemaPaths.add(ENABLE_INTERFACE_PATH);
464         schemaPaths.add(OPEN_BOXES_PATH);
465         schemaPaths.add(KILL_SERVER_APP_PATH);
466         schemaPaths.add(XYZZY_FOO_PATH);
467         schemaPaths.add(XYZZY_BAR_PATH);
468         schemaPaths.add(CHOICE_ACTION_PATH);
469
470         List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
471         assertEquals(schemaPaths.size(), actions.size());
472         for (ActionDefinition actionDefinition : actions) {
473             SchemaPath path = actionDefinition.getPath();
474             assertTrue(schemaPaths.remove(path));
475         }
476     }
477
478     @Test
479     public void toActionRequestListTopLevelTest() {
480         QName nameQname = QName.create(SERVER_QNAME, "name");
481         List<PathArgument> nodeIdentifiers = new ArrayList<>();
482         nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
483         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
484         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
485
486         ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
487
488         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
489                 RESET_SERVER_PATH, domDataTreeIdentifier, data);
490
491         Node childAction = checkBasePartOfActionRequest(actionRequest);
492
493         Node childServer = childAction.getFirstChild();
494         checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
495
496         Node childName = childServer.getFirstChild();
497         checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
498
499         Node childTest = childName.getFirstChild();
500         assertEquals(childTest.getNodeValue(), "test");
501
502         checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
503     }
504
505     @Test
506     public void toActionRequestContainerTopLevelTest() {
507         List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(DEVICE_QNAME));
508         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
509
510         NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
511         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
512                 START_DEVICE_PATH, domDataTreeIdentifier, payload);
513
514         Node childAction = checkBasePartOfActionRequest(actionRequest);
515
516         Node childDevice = childAction.getFirstChild();
517         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
518
519         checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
520     }
521
522     @Test
523     public void toActionRequestContainerInContainerTest() {
524         List<PathArgument> nodeIdentifiers = new ArrayList<>();
525         nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
526         nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
527
528         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
529
530         NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
531         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
532                 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
533
534         Node childAction = checkBasePartOfActionRequest(actionRequest);
535
536         Node childBoxOut = childAction.getFirstChild();
537         checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
538
539         Node childBoxIn = childBoxOut.getFirstChild();
540         checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
541
542         Node action = childBoxIn.getFirstChild();
543         checkNode(action, null, OPEN_QNAME.getLocalName(), null);
544     }
545
546     @Test
547     public void toActionRequestListInContainerTest() {
548         QName nameQname = QName.create(INTERFACE_QNAME, "name");
549
550         List<PathArgument> nodeIdentifiers = new ArrayList<>();
551         nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
552         nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
553         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
554
555         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
556
557         NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
558         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
559                 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
560
561         Node childAction = checkBasePartOfActionRequest(actionRequest);
562
563         Node childDevice = childAction.getFirstChild();
564         checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
565
566         Node childInterface = childDevice.getFirstChild();
567         checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
568
569         Node childName = childInterface.getFirstChild();
570         checkNode(childName, "name", "name", nameQname.getNamespace().toString());
571
572         Node childTest = childName.getFirstChild();
573         assertEquals(childTest.getNodeValue(), "test");
574
575         Node action = childInterface.getLastChild();
576         checkNode(action, null, ENABLE_QNAME.getLocalName(), null);
577     }
578
579     @Test
580     public void toActionRequestListInContainerAugmentedIntoListTest() {
581         QName serverNameQname = QName.create(SERVER_QNAME, "name");
582         QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
583
584         List<PathArgument> nodeIdentifiers = new ArrayList<>();
585         nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
586         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
587         nodeIdentifiers.add(new AugmentationIdentifier(Collections.singleton(APPLICATIONS_QNAME)));
588         nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
589         nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
590         nodeIdentifiers.add(NodeIdentifierWithPredicates.of(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(NodeIdentifierWithPredicates.of(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 }