Prepare netconf to support YANG 1.1 actions
[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
9 package org.opendaylight.netconf.sal.connect.netconf.schema.mapping;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
31 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
32
33 import com.google.common.base.Optional;
34 import com.google.common.collect.Iterables;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Maps;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Set;
44 import javax.xml.transform.dom.DOMSource;
45 import org.apache.xerces.dom.TextImpl;
46 import org.custommonkey.xmlunit.Diff;
47 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
48 import org.custommonkey.xmlunit.XMLUnit;
49 import org.hamcrest.CoreMatchers;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
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.netconf.api.NetconfMessage;
57 import org.opendaylight.netconf.api.xml.XmlUtil;
58 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
59 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
60 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
66 import org.opendaylight.yangtools.yang.common.QName;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
68 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
71 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
74 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
78 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
79 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
80 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
81 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
82 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
83 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
84 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
85 import org.w3c.dom.Node;
86 import org.xml.sax.SAXException;
87
88 public class NetconfMessageTransformerTest {
89
90     private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
91     private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
92
93     private NetconfMessageTransformer actionNetconfMessageTransformer;
94     private NetconfMessageTransformer netconfMessageTransformer;
95     private SchemaContext schema;
96
97     @Before
98     public void setUp() throws Exception {
99         XMLUnit.setIgnoreWhitespace(true);
100         XMLUnit.setIgnoreAttributeOrder(true);
101         XMLUnit.setIgnoreComments(true);
102
103         schema = getSchema(true);
104         netconfMessageTransformer = getTransformer(schema);
105         actionNetconfMessageTransformer = getActionMessageTransformer();
106     }
107
108     @Test
109     public void testLockRequestBaseSchemaNotPresent() throws Exception {
110         final SchemaContext partialSchema = getSchema(false);
111         final NetconfMessageTransformer transformer = getTransformer(partialSchema);
112         final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
113                 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
114
115         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
116         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
117     }
118
119     @Test
120     public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
121         final SchemaContext partialSchema = getSchema(true);
122         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(
123                 partialSchema,
124                 true,
125                 BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
126         );
127         NetconfMessage netconfMessage = transformer.toRpcRequest(
128                 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
129                 CREATE_SUBSCRIPTION_RPC_CONTENT
130         );
131         String documentString = XmlUtil.toString(netconfMessage.getDocument());
132         assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
133         assertThat(documentString, CoreMatchers.containsString("<rpc"));
134     }
135
136     @Test
137     public void tesLockSchemaRequest() throws Exception {
138         final SchemaContext partialSchema = getSchema(false);
139         final NetconfMessageTransformer transformer = getTransformer(partialSchema);
140         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
141
142         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
143     }
144
145     @Test
146     public void testDiscardChangesRequest() throws Exception {
147         final NetconfMessage netconfMessage =
148                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
149         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
150         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
151         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
152     }
153
154     @Test
155     public void testGetSchemaRequest() throws Exception {
156         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
157                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
158         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
159                 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
160                 + "<format>yang</format>\n"
161                 + "<identifier>module</identifier>\n"
162                 + "<version>2012-12-12</version>\n"
163                 + "</get-schema>\n"
164                 + "</rpc>");
165     }
166
167     @Test
168     public void testGetSchemaResponse() throws Exception {
169         final NetconfMessageTransformer transformer = getTransformer(getSchema(true));
170         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
171                 "<rpc-reply message-id=\"101\"\n"
172                         + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
173                         + "<data\n"
174                         + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
175                         + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
176                         + "Random YANG SCHEMA\n"
177                         + "</xs:schema>\n"
178                         + "</data>\n"
179                         + "</rpc-reply>"
180         ));
181         final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
182         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
183         assertNotNull(compositeNodeRpcResult.getResult());
184         final DOMSource schemaContent =
185             ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
186         assertThat(schemaContent.getNode().getTextContent(),
187                 CoreMatchers.containsString("Random YANG SCHEMA"));
188     }
189
190     @Test
191     public void testGetConfigResponse() throws Exception {
192         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
193                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
194                 + "<data>\n"
195                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
196                 + "<schemas>\n"
197                 + "<schema>\n"
198                 + "<identifier>module</identifier>\n"
199                 + "<version>2012-12-12</version>\n"
200                 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
201                 + "</schema>\n"
202                 + "</schemas>\n"
203                 + "</netconf-state>\n"
204                 + "</data>\n"
205                 + "</rpc-reply>"));
206
207         final NetconfMessageTransformer transformer = getTransformer(getSchema(true));
208         final DOMRpcResult compositeNodeRpcResult =
209                 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
210         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
211         assertNotNull(compositeNodeRpcResult.getResult());
212
213         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
214                 NetconfRemoteSchemaYangSourceProvider
215                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
216
217         final Map<QName, Object> keys = Maps.newHashMap();
218         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
219             keys.put(value.getNodeType(), value.getValue());
220         }
221
222         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
223                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
224         final MapEntryNode schemaNode =
225                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
226
227         final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult
228                 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
229         final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
230         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
231         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
232         assertEquals(1, Iterables.size(schemaParent.getValue()));
233
234         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
235     }
236
237     @Test
238     public void testGetConfigRequest() throws Exception {
239         final DataContainerChild<?, ?> filter = toFilterStructure(
240                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
241
242         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
243
244         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
245                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
246
247         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
248                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
249                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
250                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
251                 + "<schemas/>\n"
252                 + "</netconf-state>"
253                 + "</filter>\n"
254                 + "<source>\n"
255                 + "<running/>\n"
256                 + "</source>\n"
257                 + "</get-config>"
258                 + "</rpc>");
259     }
260
261     @Test
262     public void testEditConfigRequest() throws Exception {
263         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
264                 NetconfRemoteSchemaYangSourceProvider
265                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
266
267         final Map<QName, Object> keys = Maps.newHashMap();
268         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
269             keys.put(value.getNodeType(), value.getValue());
270         }
271
272         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
273                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
274         final MapEntryNode schemaNode =
275                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
276
277         final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
278                 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
279                 .nodeWithKey(Schema.QNAME, keys).build();
280         final DataContainerChild<?, ?> editConfigStructure =
281                 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
282                         Optional.absent(), Optional.fromNullable(schemaNode));
283
284         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
285
286         final ContainerNode wrap =
287                 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
288         final NetconfMessage netconfMessage =
289                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
290
291         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
292                 + "<edit-config>\n"
293                 + "<target>\n"
294                 + "<candidate/>\n"
295                 + "</target>\n"
296                 + "<config>\n"
297                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
298                 + "<schemas>\n"
299                 + "<schema>\n"
300                 + "<identifier>module</identifier>\n"
301                 + "<version>2012-12-12</version>\n"
302                 + "<format>yang</format>\n"
303                 + "</schema>\n"
304                 + "</schemas>\n"
305                 + "</netconf-state>\n"
306                 + "</config>\n"
307                 + "</edit-config>\n"
308                 + "</rpc>");
309     }
310
311     private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
312             throws SAXException, IOException {
313         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
314         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
315         assertTrue(diff.toString(), diff.similar());
316     }
317
318     @Test
319     public void testGetRequest() throws Exception {
320
321         final QName capability = QName.create(Capabilities.QNAME, "capability");
322         final DataContainerChild<?, ?> filter = toFilterStructure(
323                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
324                     new YangInstanceIdentifier.NodeWithValue<>(capability, "a:b:c")), schema);
325
326         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
327                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
328
329         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
330                 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
331                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
332                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
333                 + "<capabilities>\n"
334                 + "<capability>a:b:c</capability>\n"
335                 + "</capabilities>\n"
336                 + "</netconf-state>"
337                 + "</filter>\n"
338                 + "</get>"
339                 + "</rpc>");
340     }
341
342     private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
343         return new NetconfMessageTransformer(schema, true);
344     }
345
346     @Test
347     public void testCommitResponse() throws Exception {
348         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
349                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
350         ));
351         final DOMRpcResult compositeNodeRpcResult =
352                 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
353         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
354         assertNull(compositeNodeRpcResult.getResult());
355     }
356
357     public SchemaContext getSchema(final boolean addBase) {
358         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
359         if (addBase) {
360             moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
361         }
362         moduleInfoBackedContext
363                 .addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
364                         .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
365         return moduleInfoBackedContext.tryToCreateSchemaContext().get();
366     }
367
368     @Test
369     public void getActionsTest() {
370         QName reset = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
371         QName start = QName.create(reset, "start");
372         QName open = QName.create(start, "open");
373         Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open));
374         Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
375         assertTrue(!actions.isEmpty());
376         for (ActionDefinition actionDefinition : actions) {
377             QName qname = actionDefinition.getQName();
378             assertTrue(qnames.contains(qname));
379             qnames.remove(qname);
380         }
381     }
382
383     @Test
384     public void toActionRequestListTopLevelTest() {
385         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
386         QName nameQname = QName.create(qname, "name");
387         QName actionResetQName = QName.create(qname, "reset");
388
389         Set<PathArgument> nodeIdentifiers =
390                 Collections.singleton(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
391         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
392
393         ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
394
395         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
396                 SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
397
398         Node childAction = checkBasePartOfActionRequest(actionRequest);
399
400         Node childServer = childAction.getFirstChild();
401         checkNode(childServer, "server", "server", qname.getNamespace().toString());
402
403         Node childName = childServer.getFirstChild();
404         checkNode(childName, "name", "name", qname.getNamespace().toString());
405
406         TextImpl childTest = (TextImpl) childName.getFirstChild();
407         assertEquals(childTest.getData(), "test");
408
409         checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
410     }
411
412     @Test
413     public void toActionRequestContainerTopLevelTest() {
414         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
415         QName actionStartQName = QName.create(qname, "start");
416
417         Set<PathArgument> nodeIdentifiers = Collections.singleton(NodeIdentifier.create(qname));
418         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
419
420         NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
421         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
422                 SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
423
424         Node childAction = checkBasePartOfActionRequest(actionRequest);
425
426         Node childDevice = childAction.getFirstChild();
427         checkNode(childDevice, "device", "device", qname.getNamespace().toString());
428
429         checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
430     }
431
432     @Test
433     public void toActionRequestContainerInContainerTest() {
434         QName boxOutQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
435         QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
436         QName actionOpenQName = QName.create(boxOutQName, "open");
437
438         Set<PathArgument> nodeIdentifiers = new HashSet<>();
439         nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
440         nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
441
442         DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
443
444         NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
445         NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
446                 SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
447
448         Node childAction = checkBasePartOfActionRequest(actionRequest);
449
450         Node childBoxOut = childAction.getFirstChild();
451         checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
452
453         Node childBoxIn = childBoxOut.getFirstChild();
454         checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
455
456         Node action = childBoxIn.getFirstChild();
457         checkNode(action, null, actionOpenQName.getLocalName(), null);
458     }
459
460     @SuppressWarnings({ "rawtypes", "unchecked" })
461     @Test
462     public void toActionResultTest() throws Exception {
463         QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
464
465         NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
466                 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
467                 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
468                 + "now"
469                 + "</reset-finished-at>"
470                 + "</rpc-reply>"));
471         DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(
472                 SchemaPath.create(true, qname), message);
473         assertNotNull(actionResult);
474         ContainerNode containerNode = actionResult.getOutput().get();
475         assertNotNull(containerNode);
476         LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
477         assertEquals("now", leaf.getValue());
478     }
479
480     private void checkAction(QName actionQname, Node action , String inputLocalName, String inputNodeName,
481             String inputValue) {
482         checkNode(action, null, actionQname.getLocalName(), null);
483
484         Node childResetAt = action.getFirstChild();
485         checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
486
487         TextImpl firstChild = (TextImpl) childResetAt.getFirstChild();
488         assertEquals(firstChild.getData(), inputValue);
489     }
490
491     private Node checkBasePartOfActionRequest(NetconfMessage actionRequest) {
492         Node baseRpc = actionRequest.getDocument().getFirstChild();
493         checkNode(baseRpc, "rpc", "rpc", NetconfMessageTransformUtil.NETCONF_QNAME.getNamespace().toString());
494         assertTrue(baseRpc.getLocalName().equals("rpc"));
495         assertTrue(baseRpc.getNodeName().equals("rpc"));
496
497         Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
498         assertNotNull(messageId);
499         assertTrue(messageId.getNodeValue().contains("m-"));
500
501         Node childAction = baseRpc.getFirstChild();
502         checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
503         return childAction;
504     }
505
506     private DOMDataTreeIdentifier prepareDataTreeId(Set<PathArgument> nodeIdentifiers) {
507         YangInstanceIdentifier yangInstanceIdentifier =
508                 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
509         DOMDataTreeIdentifier domDataTreeIdentifier =
510                 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
511                         yangInstanceIdentifier);
512         return domDataTreeIdentifier;
513     }
514
515     private ContainerNode initInputAction(QName qname, String value) {
516         ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
517         DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
518                 NodeIdentifier.create(qname)).withValue(value).build();
519         ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
520                 QName.create(qname, "input"))).withChild(build).build();
521         return data;
522     }
523
524     private void checkNode(Node childServer, String expectedLocalName, String expectedNodeName,
525             String expectedNamespace) {
526         assertNotNull(childServer);
527         assertEquals(childServer.getLocalName(), expectedLocalName);
528         assertEquals(childServer.getNodeName(), expectedNodeName);
529         assertEquals(childServer.getNamespaceURI(), expectedNamespace);
530     }
531
532     private SchemaContext getActionSchema() {
533         return YangParserTestUtils.parseYangResource("/schemas/example-server-farm.yang");
534     }
535
536     private NetconfMessageTransformer getActionMessageTransformer() {
537         return new NetconfMessageTransformer(getActionSchema(), true);
538     }
539 }