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