2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.schema.mapping;
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_DATA_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.ImmutableMap;
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;
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;
88 public class NetconfMessageTransformerTest {
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";
93 private NetconfMessageTransformer actionNetconfMessageTransformer;
94 private NetconfMessageTransformer netconfMessageTransformer;
95 private SchemaContext schema;
98 public void setUp() throws Exception {
99 XMLUnit.setIgnoreWhitespace(true);
100 XMLUnit.setIgnoreAttributeOrder(true);
101 XMLUnit.setIgnoreComments(true);
103 schema = getSchema(true);
104 netconfMessageTransformer = getTransformer(schema);
105 actionNetconfMessageTransformer = getActionMessageTransformer();
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));
115 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
116 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
120 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
121 final SchemaContext partialSchema = getSchema(true);
122 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(
125 BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
127 NetconfMessage netconfMessage = transformer.toRpcRequest(
128 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
129 CREATE_SUBSCRIPTION_RPC_CONTENT
131 String documentString = XmlUtil.toString(netconfMessage.getDocument());
132 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
133 assertThat(documentString, CoreMatchers.containsString("<rpc"));
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>";
142 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
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"));
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"
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"
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"
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"));
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"
195 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\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"
203 + "</netconf-state>\n"
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());
213 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
214 NetconfRemoteSchemaYangSourceProvider
215 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
217 final Map<QName, Object> keys = Maps.newHashMap();
218 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
219 keys.put(value.getNodeType(), value.getValue());
222 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
223 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
224 final MapEntryNode schemaNode =
225 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
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()));
234 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
238 public void testGetConfigLeafRequest() throws Exception {
239 final DataContainerChild<?, ?> filter = toFilterStructure(
240 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
241 new NodeIdentifierWithPredicates(Schema.QNAME, ImmutableMap.of()),
242 toId(QName.create(Schemas.QNAME, "version"))), schema);
244 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
246 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
247 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
249 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
250 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
251 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
252 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
258 + "</netconf-state>\n"
268 public void testGetConfigRequest() throws Exception {
269 final DataContainerChild<?, ?> filter = toFilterStructure(
270 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
272 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
274 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
275 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
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"
292 public void testEditConfigRequest() throws Exception {
293 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
294 NetconfRemoteSchemaYangSourceProvider
295 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
297 final Map<QName, Object> keys = Maps.newHashMap();
298 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
299 keys.put(value.getNodeType(), value.getValue());
302 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
303 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
304 final MapEntryNode schemaNode =
305 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
307 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
308 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
309 .nodeWithKey(Schema.QNAME, keys).build();
310 final DataContainerChild<?, ?> editConfigStructure =
311 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
312 Optional.absent(), Optional.fromNullable(schemaNode));
314 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
316 final ContainerNode wrap =
317 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
318 final NetconfMessage netconfMessage =
319 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
321 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
327 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
330 + "<identifier>module</identifier>\n"
331 + "<version>2012-12-12</version>\n"
332 + "<format>yang</format>\n"
335 + "</netconf-state>\n"
341 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
342 throws SAXException, IOException {
343 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
344 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
345 assertTrue(diff.toString(), diff.similar());
349 public void testGetRequest() throws Exception {
351 final QName capability = QName.create(Capabilities.QNAME, "capability");
352 final DataContainerChild<?, ?> filter = toFilterStructure(
353 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
354 new YangInstanceIdentifier.NodeWithValue<>(capability, "a:b:c")), schema);
356 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
357 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
359 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
360 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
361 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
362 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
364 + "<capability>a:b:c</capability>\n"
365 + "</capabilities>\n"
372 private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
373 return new NetconfMessageTransformer(schema, true);
377 public void testCommitResponse() throws Exception {
378 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
379 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
381 final DOMRpcResult compositeNodeRpcResult =
382 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
383 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
384 assertNull(compositeNodeRpcResult.getResult());
387 public SchemaContext getSchema(final boolean addBase) {
388 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
390 moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
392 moduleInfoBackedContext
393 .addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
394 .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
395 return moduleInfoBackedContext.tryToCreateSchemaContext().get();
399 public void getActionsTest() {
400 QName reset = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
401 QName start = QName.create(reset, "start");
402 QName open = QName.create(start, "open");
403 Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open));
404 Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
405 assertTrue(!actions.isEmpty());
406 for (ActionDefinition actionDefinition : actions) {
407 QName qname = actionDefinition.getQName();
408 assertTrue(qnames.contains(qname));
409 qnames.remove(qname);
414 public void toActionRequestListTopLevelTest() {
415 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
416 QName nameQname = QName.create(qname, "name");
417 QName actionResetQName = QName.create(qname, "reset");
419 Set<PathArgument> nodeIdentifiers =
420 Collections.singleton(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
421 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
423 ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
425 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
426 SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
428 Node childAction = checkBasePartOfActionRequest(actionRequest);
430 Node childServer = childAction.getFirstChild();
431 checkNode(childServer, "server", "server", qname.getNamespace().toString());
433 Node childName = childServer.getFirstChild();
434 checkNode(childName, "name", "name", qname.getNamespace().toString());
436 TextImpl childTest = (TextImpl) childName.getFirstChild();
437 assertEquals(childTest.getData(), "test");
439 checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
443 public void toActionRequestContainerTopLevelTest() {
444 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
445 QName actionStartQName = QName.create(qname, "start");
447 Set<PathArgument> nodeIdentifiers = Collections.singleton(NodeIdentifier.create(qname));
448 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
450 NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
451 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
452 SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
454 Node childAction = checkBasePartOfActionRequest(actionRequest);
456 Node childDevice = childAction.getFirstChild();
457 checkNode(childDevice, "device", "device", qname.getNamespace().toString());
459 checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
463 public void toActionRequestContainerInContainerTest() {
464 QName boxOutQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
465 QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
466 QName actionOpenQName = QName.create(boxOutQName, "open");
468 Set<PathArgument> nodeIdentifiers = new HashSet<>();
469 nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
470 nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
472 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
474 NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
475 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
476 SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
478 Node childAction = checkBasePartOfActionRequest(actionRequest);
480 Node childBoxOut = childAction.getFirstChild();
481 checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
483 Node childBoxIn = childBoxOut.getFirstChild();
484 checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
486 Node action = childBoxIn.getFirstChild();
487 checkNode(action, null, actionOpenQName.getLocalName(), null);
490 @SuppressWarnings({ "rawtypes", "unchecked" })
492 public void toActionResultTest() throws Exception {
493 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
495 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
496 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
497 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
499 + "</reset-finished-at>"
501 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(
502 SchemaPath.create(true, qname), message);
503 assertNotNull(actionResult);
504 ContainerNode containerNode = actionResult.getOutput().get();
505 assertNotNull(containerNode);
506 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
507 assertEquals("now", leaf.getValue());
510 private void checkAction(final QName actionQname, final Node action , final String inputLocalName,
511 final String inputNodeName, final String inputValue) {
512 checkNode(action, null, actionQname.getLocalName(), null);
514 Node childResetAt = action.getFirstChild();
515 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
517 TextImpl firstChild = (TextImpl) childResetAt.getFirstChild();
518 assertEquals(firstChild.getData(), inputValue);
521 private Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
522 Node baseRpc = actionRequest.getDocument().getFirstChild();
523 checkNode(baseRpc, "rpc", "rpc", NetconfMessageTransformUtil.NETCONF_QNAME.getNamespace().toString());
524 assertTrue(baseRpc.getLocalName().equals("rpc"));
525 assertTrue(baseRpc.getNodeName().equals("rpc"));
527 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
528 assertNotNull(messageId);
529 assertTrue(messageId.getNodeValue().contains("m-"));
531 Node childAction = baseRpc.getFirstChild();
532 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
536 private DOMDataTreeIdentifier prepareDataTreeId(final Set<PathArgument> nodeIdentifiers) {
537 YangInstanceIdentifier yangInstanceIdentifier =
538 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
539 DOMDataTreeIdentifier domDataTreeIdentifier =
540 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
541 yangInstanceIdentifier);
542 return domDataTreeIdentifier;
545 private ContainerNode initInputAction(final QName qname, final String value) {
546 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
547 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
548 NodeIdentifier.create(qname)).withValue(value).build();
549 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
550 QName.create(qname, "input"))).withChild(build).build();
554 private void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
555 final String expectedNamespace) {
556 assertNotNull(childServer);
557 assertEquals(childServer.getLocalName(), expectedLocalName);
558 assertEquals(childServer.getNodeName(), expectedNodeName);
559 assertEquals(childServer.getNamespaceURI(), expectedNamespace);
562 private SchemaContext getActionSchema() {
563 return YangParserTestUtils.parseYangResource("/schemas/example-server-farm.yang");
566 private NetconfMessageTransformer getActionMessageTransformer() {
567 return new NetconfMessageTransformer(getActionSchema(), true);