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_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;
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;
43 import java.util.Optional;
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;
92 public class NetconfMessageTransformerTest {
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";
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";
100 private static SchemaContext PARTIAL_SCHEMA;
101 private static SchemaContext SCHEMA;
102 private static SchemaContext ACTION_SCHEMA;
104 private NetconfMessageTransformer actionNetconfMessageTransformer;
105 private NetconfMessageTransformer netconfMessageTransformer;
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();
114 context.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
115 SCHEMA = context.tryToCreateSchemaContext().get();
117 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
118 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang");
122 public static void afterClass() {
123 PARTIAL_SCHEMA = null;
125 ACTION_SCHEMA = null;
129 public void setUp() throws Exception {
130 XMLUnit.setIgnoreWhitespace(true);
131 XMLUnit.setIgnoreAttributeOrder(true);
132 XMLUnit.setIgnoreComments(true);
134 netconfMessageTransformer = getTransformer(SCHEMA);
135 actionNetconfMessageTransformer = new NetconfMessageTransformer(ACTION_SCHEMA, true);
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));
144 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
145 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
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
156 String documentString = XmlUtil.toString(netconfMessage.getDocument());
157 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
158 assertThat(documentString, CoreMatchers.containsString("<rpc"));
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>";
166 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
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"));
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"
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"
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"
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"));
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"
219 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\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"
227 + "</netconf-state>\n"
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());
237 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
238 NetconfRemoteSchemaYangSourceProvider
239 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
241 final Map<QName, Object> keys = new HashMap<>();
242 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
243 keys.put(value.getNodeType(), value.getValue());
246 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
247 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
248 final MapEntryNode schemaNode =
249 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
251 final AnyXmlNode data = (AnyXmlNode) ((ContainerNode) compositeNodeRpcResult
252 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
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()));
262 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
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);
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"
286 + "</netconf-state>\n"
296 public void testGetConfigRequest() throws Exception {
297 final DataContainerChild<?, ?> filter = toFilterStructure(
298 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
300 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
302 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
303 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
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"
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());
325 final Map<QName, Object> keys = new HashMap<>();
326 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
327 keys.put(value.getNodeType(), value.getValue());
330 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
331 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
332 final MapEntryNode schemaNode =
333 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
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));
342 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
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);
349 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
355 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
358 + "<identifier>module</identifier>\n"
359 + "<version>2012-12-12</version>\n"
360 + "<format>yang</format>\n"
363 + "</netconf-state>\n"
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());
377 public void testGetRequest() throws Exception {
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);
384 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
385 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
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"
392 + "<capability>a:b:c</capability>\n"
393 + "</capabilities>\n"
400 private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
401 return new NetconfMessageTransformer(schema, true);
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>"
409 final DOMRpcResult compositeNodeRpcResult =
410 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
411 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
412 assertNull(compositeNodeRpcResult.getResult());
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);
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);
442 ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
444 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
445 SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
447 Node childAction = checkBasePartOfActionRequest(actionRequest);
449 Node childServer = childAction.getFirstChild();
450 checkNode(childServer, "server", "server", qname.getNamespace().toString());
452 Node childName = childServer.getFirstChild();
453 checkNode(childName, "name", "name", qname.getNamespace().toString());
455 Node childTest = childName.getFirstChild();
456 assertEquals(childTest.getNodeValue(), "test");
458 checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
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");
466 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
467 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
469 NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
470 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
471 SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
473 Node childAction = checkBasePartOfActionRequest(actionRequest);
475 Node childDevice = childAction.getFirstChild();
476 checkNode(childDevice, "device", "device", qname.getNamespace().toString());
478 checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
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");
487 List<PathArgument> nodeIdentifiers = new ArrayList<>();
488 nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
489 nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
491 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
493 NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
494 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
495 SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
497 Node childAction = checkBasePartOfActionRequest(actionRequest);
499 Node childBoxOut = childAction.getFirstChild();
500 checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
502 Node childBoxIn = childBoxOut.getFirstChild();
503 checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
505 Node action = childBoxIn.getFirstChild();
506 checkNode(action, null, actionOpenQName.getLocalName(), null);
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");
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"));
521 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
523 NormalizedNode<?, ?> payload = initEmptyInputAction(qnameInterface);
524 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
525 SchemaPath.create(true, actionEnableQName), domDataTreeIdentifier, payload);
527 Node childAction = checkBasePartOfActionRequest(actionRequest);
529 Node childDevice = childAction.getFirstChild();
530 checkNode(childDevice, "device", "device", qnameDevice.getNamespace().toString());
532 Node childInterface = childDevice.getFirstChild();
533 checkNode(childInterface, "interface", "interface", qnameInterface.getNamespace().toString());
535 Node childName = childInterface.getFirstChild();
536 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
538 Node childTest = childName.getFirstChild();
539 assertEquals(childTest.getNodeValue(), "test");
541 Node action = childInterface.getLastChild();
542 checkNode(action, null, actionEnableQName.getLocalName(), null);
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");
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"));
566 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
568 NormalizedNode<?, ?> payload = initEmptyInputAction(qnameAppliaction);
569 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
570 SchemaPath.create(true, actionKillQName), domDataTreeIdentifier, payload);
572 Node childAction = checkBasePartOfActionRequest(actionRequest);
574 Node childServer = childAction.getFirstChild();
575 checkNode(childServer, "server", "server", qnameServer.getNamespace().toString());
577 Node childServerName = childServer.getFirstChild();
578 checkNode(childServerName, "name", "name", serverNameQname.getNamespace().toString());
580 Node childServerNameTest = childServerName.getFirstChild();
581 assertEquals(childServerNameTest.getNodeValue(), "testServer");
583 Node childApplications = childServer.getLastChild();
584 checkNode(childApplications, "applications", "applications", qnameAppliactions.getNamespace().toString());
586 Node childApplication = childApplications.getFirstChild();
587 checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
589 Node childApplicationName = childApplication.getFirstChild();
590 checkNode(childApplicationName, "name", "name", applicationNameQname.getNamespace().toString());
592 Node childApplicationNameTest = childApplicationName.getFirstChild();
593 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
595 Node childKillAction = childApplication.getLastChild();
596 checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
597 checkNode(childKillAction, null, actionKillQName.getLocalName(), null);
600 @SuppressWarnings({ "rawtypes", "unchecked" })
602 public void toActionResultTest() throws Exception {
603 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
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\">"
609 + "</reset-finished-at>"
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());
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);
624 Node childResetAt = action.getFirstChild();
625 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
627 Node firstChild = childResetAt.getFirstChild();
628 assertEquals(firstChild.getNodeValue(), inputValue);
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"));
637 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
638 assertNotNull(messageId);
639 assertTrue(messageId.getNodeValue().contains("m-"));
641 Node childAction = baseRpc.getFirstChild();
642 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
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;
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();
664 private static ContainerNode initEmptyInputAction(final QName qname) {
665 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
666 QName.create(qname, "input"))).build();
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);