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.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.Before;
51 import org.junit.Test;
52 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
53 import org.opendaylight.mdsal.dom.api.DOMActionResult;
54 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
55 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
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 = new HashMap<>();
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 = new HashMap<>();
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.empty(), Optional.ofNullable(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 QName enable = QName.create(open, "enable");
404 Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open, enable));
405 Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
406 assertTrue(!actions.isEmpty());
407 for (ActionDefinition actionDefinition : actions) {
408 QName qname = actionDefinition.getQName();
409 assertTrue(qnames.contains(qname));
410 qnames.remove(qname);
415 public void toActionRequestListTopLevelTest() {
416 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
417 QName nameQname = QName.create(qname, "name");
418 QName actionResetQName = QName.create(qname, "reset");
419 List<PathArgument> nodeIdentifiers = new ArrayList<>();
420 nodeIdentifiers.add(new NodeIdentifier(qname));
421 nodeIdentifiers.add(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
422 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
424 ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
426 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
427 SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
429 Node childAction = checkBasePartOfActionRequest(actionRequest);
431 Node childServer = childAction.getFirstChild();
432 checkNode(childServer, "server", "server", qname.getNamespace().toString());
434 Node childName = childServer.getFirstChild();
435 checkNode(childName, "name", "name", qname.getNamespace().toString());
437 Node childTest = childName.getFirstChild();
438 assertEquals(childTest.getNodeValue(), "test");
440 checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
444 public void toActionRequestContainerTopLevelTest() {
445 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
446 QName actionStartQName = QName.create(qname, "start");
448 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
449 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
451 NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
452 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
453 SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
455 Node childAction = checkBasePartOfActionRequest(actionRequest);
457 Node childDevice = childAction.getFirstChild();
458 checkNode(childDevice, "device", "device", qname.getNamespace().toString());
460 checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
464 public void toActionRequestContainerInContainerTest() {
465 QName boxOutQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
466 QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
467 QName actionOpenQName = QName.create(boxOutQName, "open");
469 List<PathArgument> nodeIdentifiers = new ArrayList<>();
470 nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
471 nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
473 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
475 NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
476 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
477 SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
479 Node childAction = checkBasePartOfActionRequest(actionRequest);
481 Node childBoxOut = childAction.getFirstChild();
482 checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
484 Node childBoxIn = childBoxOut.getFirstChild();
485 checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
487 Node action = childBoxIn.getFirstChild();
488 checkNode(action, null, actionOpenQName.getLocalName(), null);
492 public void toActionRequestListInContainerTest() {
493 QName qnameDevice = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
494 QName qnameInterface = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "interface");
495 QName nameQname = QName.create(qnameInterface, "name");
496 QName actionEnableQName = QName.create(qnameInterface, "enable");
498 List<PathArgument> nodeIdentifiers = new ArrayList<>();
499 nodeIdentifiers.add(NodeIdentifier.create(qnameDevice));
500 nodeIdentifiers.add(NodeIdentifier.create(qnameInterface));
501 nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameInterface, nameQname, "test"));
503 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
505 NormalizedNode<?, ?> payload = initEmptyInputAction(qnameInterface);
506 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
507 SchemaPath.create(true, actionEnableQName), domDataTreeIdentifier, payload);
509 Node childAction = checkBasePartOfActionRequest(actionRequest);
511 Node childDevice = childAction.getFirstChild();
512 checkNode(childDevice, "device", "device", qnameDevice.getNamespace().toString());
514 Node childInterface = childDevice.getFirstChild();
515 checkNode(childInterface, "interface", "interface", qnameInterface.getNamespace().toString());
517 Node childName = childInterface.getFirstChild();
518 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
520 Node childTest = childName.getFirstChild();
521 assertEquals(childTest.getNodeValue(), "test");
523 Node action = childInterface.getLastChild();
524 checkNode(action, null, actionEnableQName.getLocalName(), null);
527 @SuppressWarnings({ "rawtypes", "unchecked" })
529 public void toActionResultTest() throws Exception {
530 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
532 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
533 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
534 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
536 + "</reset-finished-at>"
538 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(
539 SchemaPath.create(true, qname), message);
540 assertNotNull(actionResult);
541 ContainerNode containerNode = actionResult.getOutput().get();
542 assertNotNull(containerNode);
543 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
544 assertEquals("now", leaf.getValue());
547 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
548 final String inputNodeName, final String inputValue) {
549 checkNode(action, null, actionQname.getLocalName(), null);
551 Node childResetAt = action.getFirstChild();
552 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
554 Node firstChild = childResetAt.getFirstChild();
555 assertEquals(firstChild.getNodeValue(), inputValue);
558 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
559 Node baseRpc = actionRequest.getDocument().getFirstChild();
560 checkNode(baseRpc, "rpc", "rpc", NetconfMessageTransformUtil.NETCONF_QNAME.getNamespace().toString());
561 assertTrue(baseRpc.getLocalName().equals("rpc"));
562 assertTrue(baseRpc.getNodeName().equals("rpc"));
564 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
565 assertNotNull(messageId);
566 assertTrue(messageId.getNodeValue().contains("m-"));
568 Node childAction = baseRpc.getFirstChild();
569 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
573 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
574 YangInstanceIdentifier yangInstanceIdentifier =
575 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
576 DOMDataTreeIdentifier domDataTreeIdentifier =
577 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
578 yangInstanceIdentifier);
579 return domDataTreeIdentifier;
582 private static ContainerNode initInputAction(final QName qname, final String value) {
583 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
584 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
585 NodeIdentifier.create(qname)).withValue(value).build();
586 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
587 QName.create(qname, "input"))).withChild(build).build();
591 private static ContainerNode initEmptyInputAction(final QName qname) {
592 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
593 QName.create(qname, "input"))).build();
596 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
597 final String expectedNamespace) {
598 assertNotNull(childServer);
599 assertEquals(childServer.getLocalName(), expectedLocalName);
600 assertEquals(childServer.getNodeName(), expectedNodeName);
601 assertEquals(childServer.getNamespaceURI(), expectedNamespace);
604 private static SchemaContext getActionSchema() {
605 return YangParserTestUtils.parseYangResource("/schemas/example-server-farm.yang");
608 private static NetconfMessageTransformer getActionMessageTransformer() {
609 return new NetconfMessageTransformer(getActionSchema(), true);