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.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.HashSet;
42 import java.util.List;
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.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 static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
94 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
96 private NetconfMessageTransformer actionNetconfMessageTransformer;
97 private NetconfMessageTransformer netconfMessageTransformer;
98 private SchemaContext schema;
101 public void setUp() throws Exception {
102 XMLUnit.setIgnoreWhitespace(true);
103 XMLUnit.setIgnoreAttributeOrder(true);
104 XMLUnit.setIgnoreComments(true);
106 schema = getSchema(true);
107 netconfMessageTransformer = getTransformer(schema);
108 actionNetconfMessageTransformer = getActionMessageTransformer();
112 public void testLockRequestBaseSchemaNotPresent() throws Exception {
113 final SchemaContext partialSchema = getSchema(false);
114 final NetconfMessageTransformer transformer = getTransformer(partialSchema);
115 final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
116 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
118 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
119 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
123 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
124 final SchemaContext partialSchema = getSchema(true);
125 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(
128 BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
130 NetconfMessage netconfMessage = transformer.toRpcRequest(
131 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
132 CREATE_SUBSCRIPTION_RPC_CONTENT
134 String documentString = XmlUtil.toString(netconfMessage.getDocument());
135 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
136 assertThat(documentString, CoreMatchers.containsString("<rpc"));
140 public void tesLockSchemaRequest() throws Exception {
141 final SchemaContext partialSchema = getSchema(false);
142 final NetconfMessageTransformer transformer = getTransformer(partialSchema);
143 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
145 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
149 public void testDiscardChangesRequest() throws Exception {
150 final NetconfMessage netconfMessage =
151 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
152 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
153 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
154 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
158 public void testGetSchemaRequest() throws Exception {
159 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
160 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
161 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
162 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
163 + "<format>yang</format>\n"
164 + "<identifier>module</identifier>\n"
165 + "<version>2012-12-12</version>\n"
171 public void testGetSchemaResponse() throws Exception {
172 final NetconfMessageTransformer transformer = getTransformer(getSchema(true));
173 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
174 "<rpc-reply message-id=\"101\"\n"
175 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
177 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
178 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
179 + "Random YANG SCHEMA\n"
184 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
185 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
186 assertNotNull(compositeNodeRpcResult.getResult());
187 final DOMSource schemaContent =
188 ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
189 assertThat(schemaContent.getNode().getTextContent(),
190 CoreMatchers.containsString("Random YANG SCHEMA"));
194 public void testGetConfigResponse() throws Exception {
195 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
196 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
198 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
201 + "<identifier>module</identifier>\n"
202 + "<version>2012-12-12</version>\n"
203 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
206 + "</netconf-state>\n"
210 final NetconfMessageTransformer transformer = getTransformer(getSchema(true));
211 final DOMRpcResult compositeNodeRpcResult =
212 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
213 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
214 assertNotNull(compositeNodeRpcResult.getResult());
216 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
217 NetconfRemoteSchemaYangSourceProvider
218 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
220 final Map<QName, Object> keys = Maps.newHashMap();
221 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
222 keys.put(value.getNodeType(), value.getValue());
225 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
226 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
227 final MapEntryNode schemaNode =
228 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
230 final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult
231 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
232 final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
233 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
234 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
235 assertEquals(1, Iterables.size(schemaParent.getValue()));
237 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
241 public void testGetConfigLeafRequest() throws Exception {
242 final DataContainerChild<?, ?> filter = toFilterStructure(
243 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
244 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, ImmutableMap.of()),
245 toId(QName.create(Schemas.QNAME, "version"))), schema);
247 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
249 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
250 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
252 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
253 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
254 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
255 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
261 + "</netconf-state>\n"
271 public void testGetConfigRequest() throws Exception {
272 final DataContainerChild<?, ?> filter = toFilterStructure(
273 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
275 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
277 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
278 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
280 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
281 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
282 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
283 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
295 public void testEditConfigRequest() throws Exception {
296 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
297 NetconfRemoteSchemaYangSourceProvider
298 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
300 final Map<QName, Object> keys = Maps.newHashMap();
301 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
302 keys.put(value.getNodeType(), value.getValue());
305 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
306 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
307 final MapEntryNode schemaNode =
308 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
310 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
311 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
312 .nodeWithKey(Schema.QNAME, keys).build();
313 final DataContainerChild<?, ?> editConfigStructure =
314 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
315 Optional.absent(), Optional.fromNullable(schemaNode));
317 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
319 final ContainerNode wrap =
320 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
321 final NetconfMessage netconfMessage =
322 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
324 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
330 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
333 + "<identifier>module</identifier>\n"
334 + "<version>2012-12-12</version>\n"
335 + "<format>yang</format>\n"
338 + "</netconf-state>\n"
344 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
345 throws SAXException, IOException {
346 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
347 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
348 assertTrue(diff.toString(), diff.similar());
352 public void testGetRequest() throws Exception {
354 final QName capability = QName.create(Capabilities.QNAME, "capability");
355 final DataContainerChild<?, ?> filter = toFilterStructure(
356 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
357 new YangInstanceIdentifier.NodeWithValue<>(capability, "a:b:c")), schema);
359 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
360 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
362 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
363 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
364 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
365 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
367 + "<capability>a:b:c</capability>\n"
368 + "</capabilities>\n"
375 private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
376 return new NetconfMessageTransformer(schema, true);
380 public void testCommitResponse() throws Exception {
381 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
382 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
384 final DOMRpcResult compositeNodeRpcResult =
385 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
386 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
387 assertNull(compositeNodeRpcResult.getResult());
390 public SchemaContext getSchema(final boolean addBase) {
391 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
393 moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
395 moduleInfoBackedContext
396 .addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
397 .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
398 return moduleInfoBackedContext.tryToCreateSchemaContext().get();
402 public void getActionsTest() {
403 QName reset = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
404 QName start = QName.create(reset, "start");
405 QName open = QName.create(start, "open");
406 QName enable = QName.create(open, "enable");
407 QName kill = QName.create(URN_EXAMPLE_SERVER_FARM_2, REVISION_EXAMPLE_SERVER_FARM_2, "kill");
408 Set<QName> qnames = new HashSet<>(Arrays.asList(reset, start, open, enable, kill));
409 Set<ActionDefinition> actions = actionNetconfMessageTransformer.getActions();
410 assertTrue(!actions.isEmpty());
411 for (ActionDefinition actionDefinition : actions) {
412 QName qname = actionDefinition.getQName();
413 assertTrue(qnames.contains(qname));
414 qnames.remove(qname);
419 public void toActionRequestListTopLevelTest() {
420 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
421 QName nameQname = QName.create(qname, "name");
422 QName actionResetQName = QName.create(qname, "reset");
423 List<PathArgument> nodeIdentifiers = new ArrayList<>();
424 nodeIdentifiers.add(new NodeIdentifier(qname));
425 nodeIdentifiers.add(new NodeIdentifierWithPredicates(qname, nameQname, "test"));
426 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
428 ContainerNode data = initInputAction(QName.create(qname, "reset-at"), "now");
430 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
431 SchemaPath.create(true, actionResetQName), domDataTreeIdentifier, data);
433 Node childAction = checkBasePartOfActionRequest(actionRequest);
435 Node childServer = childAction.getFirstChild();
436 checkNode(childServer, "server", "server", qname.getNamespace().toString());
438 Node childName = childServer.getFirstChild();
439 checkNode(childName, "name", "name", qname.getNamespace().toString());
441 Node childTest = childName.getFirstChild();
442 assertEquals(childTest.getNodeValue(), "test");
444 checkAction(actionResetQName, childName.getNextSibling(), "reset-at", "reset-at", "now");
448 public void toActionRequestContainerTopLevelTest() {
449 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
450 QName actionStartQName = QName.create(qname, "start");
452 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(qname));
453 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
455 NormalizedNode<?, ?> payload = initInputAction(QName.create(qname, "start-at"), "now");
456 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
457 SchemaPath.create(true, actionStartQName), domDataTreeIdentifier, payload);
459 Node childAction = checkBasePartOfActionRequest(actionRequest);
461 Node childDevice = childAction.getFirstChild();
462 checkNode(childDevice, "device", "device", qname.getNamespace().toString());
464 checkAction(actionStartQName, childDevice.getFirstChild(), "start-at", "start-at", "now");
468 public void toActionRequestContainerInContainerTest() {
469 QName boxOutQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
470 QName boxInQName = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-in");
471 QName actionOpenQName = QName.create(boxOutQName, "open");
473 List<PathArgument> nodeIdentifiers = new ArrayList<>();
474 nodeIdentifiers.add(NodeIdentifier.create(boxOutQName));
475 nodeIdentifiers.add(NodeIdentifier.create(boxInQName));
477 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
479 NormalizedNode<?, ?> payload = initInputAction(QName.create(boxOutQName, "start-at"), "now");
480 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
481 SchemaPath.create(true, actionOpenQName), domDataTreeIdentifier, payload);
483 Node childAction = checkBasePartOfActionRequest(actionRequest);
485 Node childBoxOut = childAction.getFirstChild();
486 checkNode(childBoxOut, "box-out", "box-out", boxOutQName.getNamespace().toString());
488 Node childBoxIn = childBoxOut.getFirstChild();
489 checkNode(childBoxIn, "box-in", "box-in", boxOutQName.getNamespace().toString());
491 Node action = childBoxIn.getFirstChild();
492 checkNode(action, null, actionOpenQName.getLocalName(), null);
496 public void toActionRequestListInContainerTest() {
497 QName qnameDevice = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
498 QName qnameInterface = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "interface");
499 QName nameQname = QName.create(qnameInterface, "name");
500 QName actionEnableQName = QName.create(qnameInterface, "enable");
502 List<PathArgument> nodeIdentifiers = new ArrayList<>();
503 nodeIdentifiers.add(NodeIdentifier.create(qnameDevice));
504 nodeIdentifiers.add(NodeIdentifier.create(qnameInterface));
505 nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameInterface, nameQname, "test"));
507 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
509 NormalizedNode<?, ?> payload = initEmptyInputAction(qnameInterface);
510 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
511 SchemaPath.create(true, actionEnableQName), domDataTreeIdentifier, payload);
513 Node childAction = checkBasePartOfActionRequest(actionRequest);
515 Node childDevice = childAction.getFirstChild();
516 checkNode(childDevice, "device", "device", qnameDevice.getNamespace().toString());
518 Node childInterface = childDevice.getFirstChild();
519 checkNode(childInterface, "interface", "interface", qnameInterface.getNamespace().toString());
521 Node childName = childInterface.getFirstChild();
522 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
524 Node childTest = childName.getFirstChild();
525 assertEquals(childTest.getNodeValue(), "test");
527 Node action = childInterface.getLastChild();
528 checkNode(action, null, actionEnableQName.getLocalName(), null);
532 public void toActionRequestListInContainerAugmentedIntoListTest() {
533 QName qnameServer = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
534 QName serverNameQname = QName.create(qnameServer, "name");
535 QName qnameAppliactions = QName.create(URN_EXAMPLE_SERVER_FARM_2,
536 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
537 QName qnameAppliaction = QName.create(URN_EXAMPLE_SERVER_FARM_2,
538 REVISION_EXAMPLE_SERVER_FARM_2, "application");
539 QName applicationNameQname = QName.create(qnameAppliaction, "name");
540 QName actionKillQName = QName.create(qnameAppliaction, "kill");
542 List<PathArgument> nodeIdentifiers = new ArrayList<>();
543 nodeIdentifiers.add(NodeIdentifier.create(qnameServer));
544 nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameServer, serverNameQname, "testServer"));
545 nodeIdentifiers.add(new YangInstanceIdentifier
546 .AugmentationIdentifier(Collections.singleton(qnameAppliactions)));
547 nodeIdentifiers.add(NodeIdentifier.create(qnameAppliactions));
548 nodeIdentifiers.add(NodeIdentifier.create(qnameAppliaction));
549 nodeIdentifiers.add(new NodeIdentifierWithPredicates(qnameAppliaction,
550 applicationNameQname, "testApplication"));
552 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
554 NormalizedNode<?, ?> payload = initEmptyInputAction(qnameAppliaction);
555 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
556 SchemaPath.create(true, actionKillQName), domDataTreeIdentifier, payload);
558 Node childAction = checkBasePartOfActionRequest(actionRequest);
560 Node childServer = childAction.getFirstChild();
561 checkNode(childServer, "server", "server", qnameServer.getNamespace().toString());
563 Node childServerName = childServer.getFirstChild();
564 checkNode(childServerName, "name", "name", serverNameQname.getNamespace().toString());
566 Node childServerNameTest = childServerName.getFirstChild();
567 assertEquals(childServerNameTest.getNodeValue(), "testServer");
569 Node childApplications = childServer.getLastChild();
570 checkNode(childApplications, "applications", "applications", qnameAppliactions.getNamespace().toString());
572 Node childApplication = childApplications.getFirstChild();
573 checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
575 Node childApplicationName = childApplication.getFirstChild();
576 checkNode(childApplicationName, "name", "name", applicationNameQname.getNamespace().toString());
578 Node childApplicationNameTest = childApplicationName.getFirstChild();
579 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
581 Node childKillAction = childApplication.getLastChild();
582 checkNode(childApplication, "application", "application", qnameAppliaction.getNamespace().toString());
583 checkNode(childKillAction, null, actionKillQName.getLocalName(), null);
586 @SuppressWarnings({ "rawtypes", "unchecked" })
588 public void toActionResultTest() throws Exception {
589 QName qname = QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "reset");
591 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
592 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
593 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
595 + "</reset-finished-at>"
597 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(
598 SchemaPath.create(true, qname), message);
599 assertNotNull(actionResult);
600 ContainerNode containerNode = actionResult.getOutput().get();
601 assertNotNull(containerNode);
602 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
603 assertEquals("now", leaf.getValue());
606 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
607 final String inputNodeName, final String inputValue) {
608 checkNode(action, null, actionQname.getLocalName(), null);
610 Node childResetAt = action.getFirstChild();
611 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
613 Node firstChild = childResetAt.getFirstChild();
614 assertEquals(firstChild.getNodeValue(), inputValue);
617 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
618 Node baseRpc = actionRequest.getDocument().getFirstChild();
619 checkNode(baseRpc, "rpc", "rpc", NetconfMessageTransformUtil.NETCONF_QNAME.getNamespace().toString());
620 assertTrue(baseRpc.getLocalName().equals("rpc"));
621 assertTrue(baseRpc.getNodeName().equals("rpc"));
623 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
624 assertNotNull(messageId);
625 assertTrue(messageId.getNodeValue().contains("m-"));
627 Node childAction = baseRpc.getFirstChild();
628 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
632 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
633 YangInstanceIdentifier yangInstanceIdentifier =
634 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
635 DOMDataTreeIdentifier domDataTreeIdentifier =
636 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
637 yangInstanceIdentifier);
638 return domDataTreeIdentifier;
641 private static ContainerNode initInputAction(final QName qname, final String value) {
642 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
643 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
644 NodeIdentifier.create(qname)).withValue(value).build();
645 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
646 QName.create(qname, "input"))).withChild(build).build();
650 private static ContainerNode initEmptyInputAction(final QName qname) {
651 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
652 QName.create(qname, "input"))).build();
655 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
656 final String expectedNamespace) {
657 assertNotNull(childServer);
658 assertEquals(childServer.getLocalName(), expectedLocalName);
659 assertEquals(childServer.getNodeName(), expectedNodeName);
660 assertEquals(childServer.getNamespaceURI(), expectedNamespace);
663 private static SchemaContext getActionSchema() {
664 return YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
665 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang");
668 private static NetconfMessageTransformer getActionMessageTransformer() {
669 return new NetconfMessageTransformer(getActionSchema(), true);