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.Iterables;
33 import com.google.common.collect.Lists;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.List;
41 import java.util.Optional;
43 import javax.xml.transform.dom.DOMSource;
44 import org.custommonkey.xmlunit.Diff;
45 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
46 import org.custommonkey.xmlunit.XMLUnit;
47 import org.hamcrest.CoreMatchers;
48 import org.junit.AfterClass;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
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.netconf.util.NetconfUtil;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
67 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
68 import org.opendaylight.yangtools.yang.common.QName;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.NodeWithValue;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
75 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
78 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
82 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
83 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
84 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
86 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
87 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
88 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
89 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
90 import org.w3c.dom.Node;
91 import org.xml.sax.SAXException;
93 public class NetconfMessageTransformerTest {
95 private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
96 private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
98 private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
99 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
101 private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
103 private static final QName SERVER_QNAME =
104 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
105 private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
106 private static final SchemaPath RESET_SERVER_PATH = SchemaPath.create(true, SERVER_QNAME, RESET_QNAME);
107 private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
108 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
109 private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
110 private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
111 private static final SchemaPath KILL_SERVER_APP_PATH =
112 SchemaPath.create(true, SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
114 private static final QName DEVICE_QNAME =
115 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
116 private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
117 private static final SchemaPath START_DEVICE_PATH = SchemaPath.create(true, DEVICE_QNAME, START_QNAME);
118 private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
119 private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
120 private static final SchemaPath ENABLE_INTERFACE_PATH =
121 SchemaPath.create(true, DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
123 private static final QName BOX_OUT_QNAME =
124 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
125 private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
126 private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
127 private static final SchemaPath OPEN_BOXES_PATH =
128 SchemaPath.create(true, BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
130 private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
131 private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
132 private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
133 private static final SchemaPath XYZZY_FOO_PATH = SchemaPath.create(true, FOO_QNAME, XYZZY_QNAME);
134 private static final SchemaPath XYZZY_BAR_PATH = SchemaPath.create(true, BAR_QNAME, XYZZY_QNAME);
136 private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
137 private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
138 private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
139 private static final SchemaPath CHOICE_ACTION_PATH =
140 SchemaPath.create(true, CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
142 private static SchemaContext PARTIAL_SCHEMA;
143 private static SchemaContext SCHEMA;
144 private static SchemaContext ACTION_SCHEMA;
146 private NetconfMessageTransformer actionNetconfMessageTransformer;
147 private NetconfMessageTransformer netconfMessageTransformer;
150 public static void beforeClass() {
151 final ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
152 context.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
153 .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
154 PARTIAL_SCHEMA = context.tryToCreateSchemaContext().get();
156 context.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
157 SCHEMA = context.tryToCreateSchemaContext().get();
159 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
160 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
161 "/schemas/conflicting-actions.yang");
165 public static void afterClass() {
166 PARTIAL_SCHEMA = null;
168 ACTION_SCHEMA = null;
172 public void setUp() throws Exception {
173 XMLUnit.setIgnoreWhitespace(true);
174 XMLUnit.setIgnoreAttributeOrder(true);
175 XMLUnit.setIgnoreComments(true);
177 netconfMessageTransformer = getTransformer(SCHEMA);
178 actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
183 public void testLockRequestBaseSchemaNotPresent() throws Exception {
184 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
185 final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
186 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
188 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
189 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
193 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
194 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
195 true, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS);
196 NetconfMessage netconfMessage = transformer.toRpcRequest(
197 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
198 CREATE_SUBSCRIPTION_RPC_CONTENT
200 String documentString = XmlUtil.toString(netconfMessage.getDocument());
201 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
202 assertThat(documentString, CoreMatchers.containsString("<rpc"));
206 public void tesLockSchemaRequest() throws Exception {
207 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
208 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
210 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
214 public void testDiscardChangesRequest() throws Exception {
215 final NetconfMessage netconfMessage =
216 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
217 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
218 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
219 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
223 public void testGetSchemaRequest() throws Exception {
224 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
225 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
226 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
227 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
228 + "<format>yang</format>\n"
229 + "<identifier>module</identifier>\n"
230 + "<version>2012-12-12</version>\n"
236 public void testGetSchemaResponse() throws Exception {
237 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
238 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
239 "<rpc-reply message-id=\"101\"\n"
240 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
242 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
243 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
244 + "Random YANG SCHEMA\n"
249 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
250 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
251 assertNotNull(compositeNodeRpcResult.getResult());
252 final DOMSource schemaContent =
253 ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
254 assertThat(schemaContent.getNode().getTextContent(),
255 CoreMatchers.containsString("Random YANG SCHEMA"));
259 public void testGetConfigResponse() throws Exception {
260 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
261 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
263 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
266 + "<identifier>module</identifier>\n"
267 + "<version>2012-12-12</version>\n"
268 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
271 + "</netconf-state>\n"
275 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
276 final DOMRpcResult compositeNodeRpcResult =
277 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
278 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
279 assertNotNull(compositeNodeRpcResult.getResult());
281 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
282 NetconfRemoteSchemaYangSourceProvider
283 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
285 final Map<QName, Object> keys = new HashMap<>();
286 for (final DataContainerChild<?, ?> value : values) {
287 keys.put(value.getNodeType(), value.getValue());
290 final NodeIdentifierWithPredicates identifierWithPredicates =
291 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
292 final MapEntryNode schemaNode =
293 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
295 final AnyXmlNode data = (AnyXmlNode) ((ContainerNode) compositeNodeRpcResult
296 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
298 NormalizedNodeResult nodeResult =
299 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
300 ContainerNode result = (ContainerNode) nodeResult.getResult();
301 final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
302 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
303 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
304 assertEquals(1, Iterables.size(schemaParent.getValue()));
306 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
310 public void testGetConfigLeafRequest() throws Exception {
311 final DataContainerChild<?, ?> filter = toFilterStructure(
312 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
313 NodeIdentifierWithPredicates.of(Schema.QNAME),
314 toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
316 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
318 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
319 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
321 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
322 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
323 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
324 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
330 + "</netconf-state>\n"
340 public void testGetConfigRequest() throws Exception {
341 final DataContainerChild<?, ?> filter = toFilterStructure(
342 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
344 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
346 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
347 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
349 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
350 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
351 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
352 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
364 public void testEditConfigRequest() throws Exception {
365 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
366 NetconfRemoteSchemaYangSourceProvider
367 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
369 final Map<QName, Object> keys = new HashMap<>();
370 for (final DataContainerChild<?, ?> value : values) {
371 keys.put(value.getNodeType(), value.getValue());
374 final NodeIdentifierWithPredicates identifierWithPredicates =
375 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
376 final MapEntryNode schemaNode =
377 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
379 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
380 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
381 .nodeWithKey(Schema.QNAME, keys).build();
382 final DataContainerChild<?, ?> editConfigStructure =
383 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
384 Optional.empty(), Optional.ofNullable(schemaNode));
386 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
388 final ContainerNode wrap =
389 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
390 final NetconfMessage netconfMessage =
391 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
393 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
399 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
402 + "<identifier>module</identifier>\n"
403 + "<version>2012-12-12</version>\n"
404 + "<format>yang</format>\n"
407 + "</netconf-state>\n"
413 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
414 throws SAXException, IOException {
415 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
416 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
417 assertTrue(diff.toString(), diff.similar());
421 public void testGetRequest() throws Exception {
423 final QName capability = QName.create(Capabilities.QNAME, "capability");
424 final DataContainerChild<?, ?> filter = toFilterStructure(
425 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
426 new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
428 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
429 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
431 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
432 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
433 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
434 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
436 + "<capability>a:b:c</capability>\n"
437 + "</capabilities>\n"
444 private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
445 return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true);
449 public void testCommitResponse() throws Exception {
450 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
451 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
453 final DOMRpcResult compositeNodeRpcResult =
454 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
455 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
456 assertNull(compositeNodeRpcResult.getResult());
460 public void getActionsTest() {
461 Set<SchemaPath> schemaPaths = new HashSet<>();
462 schemaPaths.add(RESET_SERVER_PATH);
463 schemaPaths.add(START_DEVICE_PATH);
464 schemaPaths.add(ENABLE_INTERFACE_PATH);
465 schemaPaths.add(OPEN_BOXES_PATH);
466 schemaPaths.add(KILL_SERVER_APP_PATH);
467 schemaPaths.add(XYZZY_FOO_PATH);
468 schemaPaths.add(XYZZY_BAR_PATH);
469 schemaPaths.add(CHOICE_ACTION_PATH);
471 List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
472 assertEquals(schemaPaths.size(), actions.size());
473 for (ActionDefinition actionDefinition : actions) {
474 SchemaPath path = actionDefinition.getPath();
475 assertTrue(schemaPaths.remove(path));
480 public void toActionRequestListTopLevelTest() {
481 QName nameQname = QName.create(SERVER_QNAME, "name");
482 List<PathArgument> nodeIdentifiers = new ArrayList<>();
483 nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
484 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
485 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
487 ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
489 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
490 RESET_SERVER_PATH, domDataTreeIdentifier, data);
492 Node childAction = checkBasePartOfActionRequest(actionRequest);
494 Node childServer = childAction.getFirstChild();
495 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
497 Node childName = childServer.getFirstChild();
498 checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
500 Node childTest = childName.getFirstChild();
501 assertEquals(childTest.getNodeValue(), "test");
503 checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
507 public void toActionRequestContainerTopLevelTest() {
508 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(DEVICE_QNAME));
509 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
511 NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
512 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
513 START_DEVICE_PATH, domDataTreeIdentifier, payload);
515 Node childAction = checkBasePartOfActionRequest(actionRequest);
517 Node childDevice = childAction.getFirstChild();
518 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
520 checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
524 public void toActionRequestContainerInContainerTest() {
525 List<PathArgument> nodeIdentifiers = new ArrayList<>();
526 nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
527 nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
529 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
531 NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
532 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
533 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
535 Node childAction = checkBasePartOfActionRequest(actionRequest);
537 Node childBoxOut = childAction.getFirstChild();
538 checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
540 Node childBoxIn = childBoxOut.getFirstChild();
541 checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
543 Node action = childBoxIn.getFirstChild();
544 checkNode(action, null, OPEN_QNAME.getLocalName(), null);
548 public void toActionRequestListInContainerTest() {
549 QName nameQname = QName.create(INTERFACE_QNAME, "name");
551 List<PathArgument> nodeIdentifiers = new ArrayList<>();
552 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
553 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
554 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
556 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
558 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
559 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
560 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
562 Node childAction = checkBasePartOfActionRequest(actionRequest);
564 Node childDevice = childAction.getFirstChild();
565 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
567 Node childInterface = childDevice.getFirstChild();
568 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
570 Node childName = childInterface.getFirstChild();
571 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
573 Node childTest = childName.getFirstChild();
574 assertEquals(childTest.getNodeValue(), "test");
576 Node action = childInterface.getLastChild();
577 checkNode(action, null, ENABLE_QNAME.getLocalName(), null);
581 public void toActionRequestListInContainerAugmentedIntoListTest() {
582 QName serverNameQname = QName.create(SERVER_QNAME, "name");
583 QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
585 List<PathArgument> nodeIdentifiers = new ArrayList<>();
586 nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
587 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
588 nodeIdentifiers.add(new AugmentationIdentifier(Collections.singleton(APPLICATIONS_QNAME)));
589 nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
590 nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
591 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
592 applicationNameQname, "testApplication"));
594 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
596 NormalizedNode<?, ?> payload = initEmptyInputAction(APPLICATION_QNAME);
597 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
598 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
600 Node childAction = checkBasePartOfActionRequest(actionRequest);
602 Node childServer = childAction.getFirstChild();
603 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
605 Node childServerName = childServer.getFirstChild();
606 checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
608 Node childServerNameTest = childServerName.getFirstChild();
609 assertEquals(childServerNameTest.getNodeValue(), "testServer");
611 Node childApplications = childServer.getLastChild();
612 checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
614 Node childApplication = childApplications.getFirstChild();
615 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
617 Node childApplicationName = childApplication.getFirstChild();
618 checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
620 Node childApplicationNameTest = childApplicationName.getFirstChild();
621 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
623 Node childKillAction = childApplication.getLastChild();
624 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
625 checkNode(childKillAction, null, KILL_QNAME.getLocalName(), null);
629 public void toActionRequestConflictingInListTest() {
630 QName barInputQname = QName.create(BAR_QNAME, "bar");
631 QName barIdQname = QName.create(BAR_QNAME, "bar-id");
632 Byte barInput = new Byte("1");
634 List<PathArgument> nodeIdentifiers = new ArrayList<>();
635 nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
636 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
638 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
640 ImmutableLeafNodeBuilder<Byte> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
641 DataContainerChild<NodeIdentifier, Byte> build = immutableLeafNodeBuilder.withNodeIdentifier(
642 NodeIdentifier.create(barInputQname)).withValue(barInput).build();
643 NormalizedNode<?, ?> payload = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
644 QName.create(barInputQname, "input"))).withChild(build).build();
646 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
647 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
649 Node childAction = checkBasePartOfActionRequest(actionRequest);
651 Node childBar = childAction.getFirstChild();
652 checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
654 Node childBarId = childBar.getFirstChild();
655 checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
657 Node childTest = childBarId.getFirstChild();
658 assertEquals(childTest.getNodeValue(), "test");
660 Node action = childBar.getLastChild();
661 checkNode(action, null, XYZZY_QNAME.getLocalName(), null);
665 public void toActionRequestConflictingInContainerTest() {
666 QName fooInputQname = QName.create(FOO_QNAME, "foo");
668 List<PathArgument> nodeIdentifiers = new ArrayList<>();
669 nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
670 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
671 NormalizedNode<?, ?> payload = initInputAction(fooInputQname, "test");
673 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
674 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
676 Node childAction = checkBasePartOfActionRequest(actionRequest);
678 Node childBar = childAction.getFirstChild();
679 checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
681 Node action = childBar.getLastChild();
682 checkNode(action, null, XYZZY_QNAME.getLocalName(), null);
686 public void toActionRequestChoiceTest() {
687 List<PathArgument> nodeIdentifiers = new ArrayList<>();
688 nodeIdentifiers.add(NodeIdentifier.create(CONFLICT_CHOICE_QNAME));
689 nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
690 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
691 NormalizedNode<?, ?> payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
693 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
694 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
696 Node childAction = checkBasePartOfActionRequest(actionRequest);
698 Node childChoiceCont = childAction.getFirstChild();
699 checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
701 Node action = childChoiceCont.getLastChild();
702 checkNode(action, null, CHOICE_ACTION_QNAME.getLocalName(), null);
705 @SuppressWarnings({ "rawtypes", "unchecked" })
707 public void toActionResultTest() throws Exception {
708 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
709 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
710 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
712 + "</reset-finished-at>"
714 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
715 assertNotNull(actionResult);
716 ContainerNode containerNode = actionResult.getOutput().get();
717 assertNotNull(containerNode);
718 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
719 assertEquals("now", leaf.getValue());
722 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
723 final String inputNodeName, final String inputValue) {
724 checkNode(action, null, actionQname.getLocalName(), null);
726 Node childResetAt = action.getFirstChild();
727 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
729 Node firstChild = childResetAt.getFirstChild();
730 assertEquals(firstChild.getNodeValue(), inputValue);
733 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
734 Node baseRpc = actionRequest.getDocument().getFirstChild();
735 checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
736 assertTrue(baseRpc.getLocalName().equals("rpc"));
737 assertTrue(baseRpc.getNodeName().equals("rpc"));
739 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
740 assertNotNull(messageId);
741 assertTrue(messageId.getNodeValue().contains("m-"));
743 Node childAction = baseRpc.getFirstChild();
744 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
748 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
749 YangInstanceIdentifier yangInstanceIdentifier =
750 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
751 DOMDataTreeIdentifier domDataTreeIdentifier =
752 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
753 yangInstanceIdentifier);
754 return domDataTreeIdentifier;
757 private static ContainerNode initInputAction(final QName qname, final String value) {
758 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
759 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
760 NodeIdentifier.create(qname)).withValue(value).build();
761 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
762 QName.create(qname, "input"))).withChild(build).build();
766 private static ContainerNode initEmptyInputAction(final QName qname) {
767 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
768 QName.create(qname, "input"))).build();
771 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
772 final String expectedNamespace) {
773 assertNotNull(childServer);
774 assertEquals(childServer.getLocalName(), expectedLocalName);
775 assertEquals(childServer.getNodeName(), expectedNodeName);
776 assertEquals(childServer.getNamespaceURI(), expectedNamespace);