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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.netconf.common.mdsal.NormalizedDataUtil.NETCONF_DATA_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
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.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
40 import java.util.Optional;
42 import javax.xml.transform.dom.DOMSource;
43 import org.custommonkey.xmlunit.Diff;
44 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
45 import org.custommonkey.xmlunit.XMLUnit;
46 import org.hamcrest.CoreMatchers;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
52 import org.opendaylight.mdsal.dom.api.DOMActionResult;
53 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
54 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
55 import org.opendaylight.netconf.api.NetconfMessage;
56 import org.opendaylight.netconf.api.xml.XmlUtil;
57 import org.opendaylight.netconf.common.mdsal.NormalizedDataUtil;
58 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
59 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
60 import org.opendaylight.netconf.sal.connect.netconf.util.FieldsFilter;
61 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
62 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Datastores;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Statistics;
70 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.Datastore;
71 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.datastore.Locks;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.datastore.locks.lock.type.partial.lock.PartialLock;
73 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
74 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
75 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfConfigChange;
76 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.netconf.config.change.Edit;
77 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
78 import org.opendaylight.yangtools.yang.common.QName;
79 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
80 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
81 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
83 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
84 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
85 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
86 import org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
89 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
90 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
91 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
92 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
93 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
94 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
95 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
96 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
97 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
98 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
99 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
100 import org.w3c.dom.Node;
101 import org.xml.sax.SAXException;
103 public class NetconfMessageTransformerTest extends AbstractBaseSchemasTest {
105 private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
106 private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
108 private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
109 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
111 private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
113 private static final String URN_EXAMPLE_AUGMENTED_ACTION = "urn:example:augmented-action";
115 private static final String URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS = "urn:example:rpcs-actions-outputs";
117 private static final QName SERVER_QNAME =
118 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
119 private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
120 private static final Absolute RESET_SERVER_PATH = Absolute.of(SERVER_QNAME, RESET_QNAME);
121 private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
122 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
123 private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
124 private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
125 private static final Absolute KILL_SERVER_APP_PATH =
126 Absolute.of(SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
128 private static final QName DEVICE_QNAME =
129 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
130 private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
131 private static final Absolute START_DEVICE_PATH = Absolute.of(DEVICE_QNAME, START_QNAME);
132 private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
133 private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
134 private static final Absolute ENABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
136 private static final QName DISABLE_QNAME = QName.create(URN_EXAMPLE_AUGMENTED_ACTION, "disable");
137 private static final Absolute DISABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, DISABLE_QNAME);
139 private static final QName CHECK_WITH_OUTPUT_QNAME =
140 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-with-output");
141 private static final Absolute CHECK_WITH_OUTPUT_INTERFACE_PATH =
142 Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITH_OUTPUT_QNAME);
143 private static final QName CHECK_WITHOUT_OUTPUT_QNAME =
144 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-without-output");
145 private static final Absolute CHECK_WITHOUT_OUTPUT_INTERFACE_PATH =
146 Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITHOUT_OUTPUT_QNAME);
147 private static final QName RPC_WITH_OUTPUT_QNAME =
148 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-with-output");
149 private static final QName RPC_WITHOUT_OUTPUT_QNAME =
150 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-without-output");
152 private static final QName BOX_OUT_QNAME =
153 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
154 private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
155 private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
156 private static final Absolute OPEN_BOXES_PATH = Absolute.of(BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
158 private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
159 private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
160 private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
161 private static final Absolute XYZZY_FOO_PATH = Absolute.of(FOO_QNAME, XYZZY_QNAME);
162 private static final Absolute XYZZY_BAR_PATH = Absolute.of(BAR_QNAME, XYZZY_QNAME);
164 private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
165 private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
166 private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
167 private static final Absolute CHOICE_ACTION_PATH =
168 Absolute.of(CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
170 private static EffectiveModelContext PARTIAL_SCHEMA;
171 private static EffectiveModelContext SCHEMA;
172 private static EffectiveModelContext ACTION_SCHEMA;
174 private NetconfMessageTransformer actionNetconfMessageTransformer;
175 private NetconfMessageTransformer netconfMessageTransformer;
178 public static void beforeClass() {
179 PARTIAL_SCHEMA = BindingRuntimeHelpers.createEffectiveModel(NetconfState.class);
180 SCHEMA = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class,
181 NetconfConfigChange.class);
182 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
183 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
184 "/schemas/conflicting-actions.yang", "/schemas/augmented-action.yang",
185 "/schemas/rpcs-actions-outputs.yang");
189 public static void afterClass() {
190 PARTIAL_SCHEMA = null;
192 ACTION_SCHEMA = null;
196 public void setUp() throws Exception {
197 XMLUnit.setIgnoreWhitespace(true);
198 XMLUnit.setIgnoreAttributeOrder(true);
199 XMLUnit.setIgnoreComments(true);
201 netconfMessageTransformer = getTransformer(SCHEMA);
202 actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
203 true, BASE_SCHEMAS.getBaseSchema());
207 public void testLockRequestBaseSchemaNotPresent() throws Exception {
208 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
209 final NetconfMessage netconfMessage = transformer.toRpcRequest(NETCONF_LOCK_QNAME,
210 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_NODEID));
212 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
213 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
217 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
218 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
219 true, BASE_SCHEMAS.getBaseSchemaWithNotifications());
220 NetconfMessage netconfMessage = transformer.toRpcRequest(CREATE_SUBSCRIPTION_RPC_QNAME,
221 CREATE_SUBSCRIPTION_RPC_CONTENT);
222 String documentString = XmlUtil.toString(netconfMessage.getDocument());
223 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
224 assertThat(documentString, CoreMatchers.containsString("<rpc"));
228 public void tesLockSchemaRequest() throws Exception {
229 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
230 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
232 transformer.toRpcResult(
233 RpcResultBuilder.success(new NetconfMessage(XmlUtil.readXmlToDocument(result))).build(),
238 public void testRpcEmptyBodyWithOutputDefinedSchemaResult() throws Exception {
239 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
241 DOMRpcResult domRpcResult = actionNetconfMessageTransformer.toRpcResult(
242 RpcResultBuilder.success(new NetconfMessage(XmlUtil.readXmlToDocument(result))).build(),
243 RPC_WITH_OUTPUT_QNAME);
244 assertNotNull(domRpcResult);
248 public void testRpcEmptyBodyWithoutOutputDefinedSchemaResult() throws Exception {
249 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
251 DOMRpcResult domRpcResult = actionNetconfMessageTransformer.toRpcResult(
252 RpcResultBuilder.success(new NetconfMessage(XmlUtil.readXmlToDocument(result))).build(),
253 RPC_WITHOUT_OUTPUT_QNAME);
254 assertNotNull(domRpcResult);
258 public void testDiscardChangesRequest() throws Exception {
259 final NetconfMessage netconfMessage =
260 netconfMessageTransformer.toRpcRequest(NETCONF_DISCARD_CHANGES_QNAME, null);
261 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
262 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
263 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
267 public void testGetSchemaRequest() throws Exception {
268 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(GET_SCHEMA_QNAME,
269 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
270 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
271 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
272 + "<format>yang</format>\n"
273 + "<identifier>module</identifier>\n"
274 + "<version>2012-12-12</version>\n"
280 public void testGetSchemaResponse() throws Exception {
281 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
282 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
283 "<rpc-reply message-id=\"101\"\n"
284 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
286 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
287 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
288 + "Random YANG SCHEMA\n"
293 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(RpcResultBuilder.success(response).build(),
295 assertTrue(compositeNodeRpcResult.errors().isEmpty());
296 assertNotNull(compositeNodeRpcResult.value());
297 final DOMSource schemaContent = ((DOMSourceAnyxmlNode) compositeNodeRpcResult.value()
298 .body().iterator().next()).body();
299 assertThat(schemaContent.getNode().getTextContent(),
300 CoreMatchers.containsString("Random YANG SCHEMA"));
304 public void testGetConfigResponse() throws Exception {
305 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
306 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
308 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
311 + "<identifier>module</identifier>\n"
312 + "<version>2012-12-12</version>\n"
313 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
316 + "</netconf-state>\n"
320 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
321 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(RpcResultBuilder.success(response).build(),
322 NETCONF_GET_CONFIG_QNAME);
323 assertTrue(compositeNodeRpcResult.errors().isEmpty());
324 assertNotNull(compositeNodeRpcResult.value());
326 final List<DataContainerChild> values = Lists.newArrayList(
327 NetconfRemoteSchemaYangSourceProvider
328 .createGetSchemaRequest("module", Optional.of("2012-12-12")).body());
330 final Map<QName, Object> keys = new HashMap<>();
331 for (final DataContainerChild value : values) {
332 keys.put(value.getIdentifier().getNodeType(), value.body());
335 final NodeIdentifierWithPredicates identifierWithPredicates =
336 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
337 final MapEntryNode schemaNode =
338 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
340 final DOMSourceAnyxmlNode data = (DOMSourceAnyxmlNode) compositeNodeRpcResult.value()
341 .findChildByArg(toId(NETCONF_DATA_QNAME)).orElseThrow();
343 NormalizedNodeResult nodeResult = NormalizedDataUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.body());
344 ContainerNode result = (ContainerNode) nodeResult.getResult();
345 final ContainerNode state = (ContainerNode) result.getChildByArg(toId(NetconfState.QNAME));
346 final ContainerNode schemas = (ContainerNode) state.getChildByArg(toId(Schemas.QNAME));
347 final MapNode schemaParent = (MapNode) schemas.getChildByArg(toId(Schema.QNAME));
348 assertEquals(1, Iterables.size(schemaParent.body()));
350 assertEquals(schemaNode, schemaParent.body().iterator().next());
354 public void testGetConfigLeafRequest() throws Exception {
355 final AnyxmlNode<?> filter = toFilterStructure(
356 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
357 NodeIdentifierWithPredicates.of(Schema.QNAME),
358 toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
360 final ContainerNode source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_NODEID);
362 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
363 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
365 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
366 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
367 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
368 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
374 + "</netconf-state>\n"
384 public void testGetConfigRequest() throws Exception {
385 final AnyxmlNode<?> filter = toFilterStructure(
386 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
388 final ContainerNode source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_NODEID);
390 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
391 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
393 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
394 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
395 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
396 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
408 public void testEditConfigRequest() throws Exception {
409 final List<DataContainerChild> values = Lists.newArrayList(
410 NetconfRemoteSchemaYangSourceProvider
411 .createGetSchemaRequest("module", Optional.of("2012-12-12")).body());
413 final Map<QName, Object> keys = new HashMap<>();
414 for (final DataContainerChild value : values) {
415 keys.put(value.getIdentifier().getNodeType(), value.body());
418 final NodeIdentifierWithPredicates identifierWithPredicates =
419 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
420 final MapEntryNode schemaNode =
421 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
423 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
424 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
425 .nodeWithKey(Schema.QNAME, keys).build();
426 final DataContainerChild editConfigStructure =
427 createEditConfigStructure(BASE_SCHEMAS.getBaseSchemaWithNotifications().getEffectiveModelContext(), id,
428 Optional.empty(), Optional.ofNullable(schemaNode));
430 final DataContainerChild target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_NODEID);
432 final ContainerNode wrap =
433 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
434 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_EDIT_CONFIG_QNAME, wrap);
436 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
442 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
445 + "<identifier>module</identifier>\n"
446 + "<version>2012-12-12</version>\n"
447 + "<format>yang</format>\n"
450 + "</netconf-state>\n"
456 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
457 throws SAXException, IOException {
458 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
459 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
460 assertTrue(diff.toString(), diff.similar());
464 public void testGetRequest() throws Exception {
466 final QName capability = QName.create(Capabilities.QNAME, "capability");
467 final DataContainerChild filter = toFilterStructure(
468 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
469 new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
471 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
472 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
474 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
475 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
476 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
477 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
479 + "<capability>a:b:c</capability>\n"
480 + "</capabilities>\n"
488 public void testGetLeafList() throws IOException, SAXException {
489 final YangInstanceIdentifier path = YangInstanceIdentifier.create(
490 toId(NetconfState.QNAME),
491 toId(Capabilities.QNAME),
492 toId(QName.create(Capabilities.QNAME, "capability")));
493 final DataContainerChild filter = toFilterStructure(path, SCHEMA);
494 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
495 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filter));
497 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
499 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
500 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
503 + "</capabilities>\n"
504 + "</netconf-state>\n"
511 public void testGetList() throws IOException, SAXException {
512 final YangInstanceIdentifier path = YangInstanceIdentifier.create(
513 toId(NetconfState.QNAME),
514 toId(Datastores.QNAME),
515 toId(QName.create(Datastores.QNAME, "datastore")));
516 final DataContainerChild filter = toFilterStructure(path, SCHEMA);
517 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
518 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filter));
520 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
522 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
523 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
527 + "</netconf-state>\n"
533 private static NetconfMessageTransformer getTransformer(final EffectiveModelContext schema) {
534 return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true, BASE_SCHEMAS.getBaseSchema());
538 public void testCommitResponse() throws Exception {
539 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
540 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
542 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(
543 RpcResultBuilder.success(response).build(),
544 NETCONF_COMMIT_QNAME);
545 assertTrue(compositeNodeRpcResult.errors().isEmpty());
546 assertNull(compositeNodeRpcResult.value());
550 public void getActionsTest() {
551 Set<Absolute> schemaPaths = new HashSet<>();
552 schemaPaths.add(RESET_SERVER_PATH);
553 schemaPaths.add(START_DEVICE_PATH);
554 schemaPaths.add(ENABLE_INTERFACE_PATH);
555 schemaPaths.add(OPEN_BOXES_PATH);
556 schemaPaths.add(KILL_SERVER_APP_PATH);
557 schemaPaths.add(XYZZY_FOO_PATH);
558 schemaPaths.add(XYZZY_BAR_PATH);
559 schemaPaths.add(CHOICE_ACTION_PATH);
560 schemaPaths.add(DISABLE_INTERFACE_PATH);
561 schemaPaths.add(CHECK_WITH_OUTPUT_INTERFACE_PATH);
562 schemaPaths.add(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH);
564 var actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
565 assertEquals(schemaPaths.size(), actions.size());
567 for (var path : schemaPaths) {
568 assertNotNull("Action for " + path + " not found", actions.get(path));
573 public void toActionRequestListTopLevelTest() {
574 QName nameQname = QName.create(SERVER_QNAME, "name");
575 List<PathArgument> nodeIdentifiers = new ArrayList<>();
576 nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
577 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
578 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
580 ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
582 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
583 RESET_SERVER_PATH, domDataTreeIdentifier, data);
585 Node childAction = checkBasePartOfActionRequest(actionRequest);
587 Node childServer = childAction.getFirstChild();
588 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
590 Node childName = childServer.getFirstChild();
591 checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
593 Node childTest = childName.getFirstChild();
594 assertEquals(childTest.getNodeValue(), "test");
596 checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
600 public void toActionRequestContainerTopLevelTest() {
601 List<PathArgument> nodeIdentifiers = List.of(NodeIdentifier.create(DEVICE_QNAME));
602 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
604 ContainerNode payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
605 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
606 START_DEVICE_PATH, domDataTreeIdentifier, payload);
608 Node childAction = checkBasePartOfActionRequest(actionRequest);
610 Node childDevice = childAction.getFirstChild();
611 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
613 checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
617 public void toActionRequestContainerInContainerTest() {
618 List<PathArgument> nodeIdentifiers = new ArrayList<>();
619 nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
620 nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
622 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
624 ContainerNode payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
625 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
626 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
628 Node childAction = checkBasePartOfActionRequest(actionRequest);
630 Node childBoxOut = childAction.getFirstChild();
631 checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
633 Node childBoxIn = childBoxOut.getFirstChild();
634 checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
636 Node action = childBoxIn.getFirstChild();
637 checkNode(action, OPEN_QNAME.getLocalName(), OPEN_QNAME.getLocalName(), OPEN_QNAME.getNamespace().toString());
641 public void toActionRequestListInContainerTest() {
642 QName nameQname = QName.create(INTERFACE_QNAME, "name");
644 List<PathArgument> nodeIdentifiers = new ArrayList<>();
645 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
646 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
647 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
649 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
651 ContainerNode payload = initEmptyInputAction(INTERFACE_QNAME);
652 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
653 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
655 Node childAction = checkBasePartOfActionRequest(actionRequest);
657 Node childDevice = childAction.getFirstChild();
658 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
660 Node childInterface = childDevice.getFirstChild();
661 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
663 Node childName = childInterface.getFirstChild();
664 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
666 Node childTest = childName.getFirstChild();
667 assertEquals(childTest.getNodeValue(), "test");
669 Node action = childInterface.getLastChild();
670 checkNode(action, ENABLE_QNAME.getLocalName(), ENABLE_QNAME.getLocalName(),
671 ENABLE_QNAME.getNamespace().toString());
675 public void toActionRequestListInContainerAugmentedIntoListTest() {
676 QName serverNameQname = QName.create(SERVER_QNAME, "name");
677 QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
679 List<PathArgument> nodeIdentifiers = new ArrayList<>();
680 nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
681 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
682 nodeIdentifiers.add(new AugmentationIdentifier(Set.of(APPLICATIONS_QNAME)));
683 nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
684 nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
685 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
686 applicationNameQname, "testApplication"));
688 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
690 ContainerNode payload = initEmptyInputAction(APPLICATION_QNAME);
691 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
692 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
694 Node childAction = checkBasePartOfActionRequest(actionRequest);
696 Node childServer = childAction.getFirstChild();
697 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
699 Node childServerName = childServer.getFirstChild();
700 checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
702 Node childServerNameTest = childServerName.getFirstChild();
703 assertEquals(childServerNameTest.getNodeValue(), "testServer");
705 Node childApplications = childServer.getLastChild();
706 checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
708 Node childApplication = childApplications.getFirstChild();
709 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
711 Node childApplicationName = childApplication.getFirstChild();
712 checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
714 Node childApplicationNameTest = childApplicationName.getFirstChild();
715 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
717 Node childKillAction = childApplication.getLastChild();
718 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
719 checkNode(childKillAction, KILL_QNAME.getLocalName(), KILL_QNAME.getLocalName(),
720 KILL_QNAME.getNamespace().toString());
724 public void toActionRequestConflictingInListTest() {
725 QName barInputQname = QName.create(BAR_QNAME, "bar");
726 QName barIdQname = QName.create(BAR_QNAME, "bar-id");
729 List<PathArgument> nodeIdentifiers = new ArrayList<>();
730 nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
731 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
733 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
735 ContainerNode payload = Builders.containerBuilder()
736 .withNodeIdentifier(NodeIdentifier.create(QName.create(barInputQname, "input")))
737 .withChild(ImmutableNodes.leafNode(barInputQname, barInput))
740 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
741 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
743 Node childAction = checkBasePartOfActionRequest(actionRequest);
745 Node childBar = childAction.getFirstChild();
746 checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
748 Node childBarId = childBar.getFirstChild();
749 checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
751 Node childTest = childBarId.getFirstChild();
752 assertEquals(childTest.getNodeValue(), "test");
754 Node action = childBar.getLastChild();
755 checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
756 XYZZY_QNAME.getNamespace().toString());
760 public void toActionRequestConflictingInContainerTest() {
761 QName fooInputQname = QName.create(FOO_QNAME, "foo");
763 List<PathArgument> nodeIdentifiers = new ArrayList<>();
764 nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
765 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
766 ContainerNode payload = initInputAction(fooInputQname, "test");
768 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
769 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
771 Node childAction = checkBasePartOfActionRequest(actionRequest);
773 Node childBar = childAction.getFirstChild();
774 checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
776 Node action = childBar.getLastChild();
777 checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
778 XYZZY_QNAME.getNamespace().toString());
782 public void toActionRequestChoiceTest() {
783 List<PathArgument> nodeIdentifiers = new ArrayList<>();
784 nodeIdentifiers.add(NodeIdentifier.create(CONFLICT_CHOICE_QNAME));
785 nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
786 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
787 NormalizedNode payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
789 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
790 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
792 Node childAction = checkBasePartOfActionRequest(actionRequest);
794 Node childChoiceCont = childAction.getFirstChild();
795 checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
797 Node action = childChoiceCont.getLastChild();
798 checkNode(action, CHOICE_ACTION_QNAME.getLocalName(), CHOICE_ACTION_QNAME.getLocalName(),
799 CHOICE_ACTION_QNAME.getNamespace().toString());
803 public void toAugmentedActionRequestListInContainerTest() {
804 QName nameQname = QName.create(INTERFACE_QNAME, "name");
806 List<PathArgument> nodeIdentifiers = new ArrayList<>();
807 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
808 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
809 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
811 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
813 NormalizedNode payload = initEmptyInputAction(INTERFACE_QNAME);
814 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
815 DISABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
817 Node childAction = checkBasePartOfActionRequest(actionRequest);
819 Node childDevice = childAction.getFirstChild();
820 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
822 Node childInterface = childDevice.getFirstChild();
823 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
825 Node childName = childInterface.getFirstChild();
826 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
828 Node childTest = childName.getFirstChild();
829 assertEquals(childTest.getNodeValue(), "test");
831 Node action = childInterface.getLastChild();
832 checkNode(action, DISABLE_QNAME.getLocalName(), DISABLE_QNAME.getLocalName(),
833 DISABLE_QNAME.getNamespace().toString());
837 public void toActionResultTest() throws Exception {
838 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
839 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
840 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
842 + "</reset-finished-at>"
844 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
845 assertNotNull(actionResult);
846 ContainerNode containerNode = actionResult.getOutput().orElseThrow();
847 assertNotNull(containerNode);
848 assertEquals("now", containerNode.body().iterator().next().body());
852 public void toActionEmptyBodyWithOutputDefinedResultTest() throws Exception {
853 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
854 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
857 DOMActionResult actionResult =
858 actionNetconfMessageTransformer.toActionResult(CHECK_WITH_OUTPUT_INTERFACE_PATH, message);
859 assertNotNull(actionResult);
860 assertTrue(actionResult.getOutput().isEmpty());
864 public void toActionEmptyBodyWithoutOutputDefinedResultTest() throws Exception {
865 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
866 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
869 DOMActionResult actionResult =
870 actionNetconfMessageTransformer.toActionResult(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH, message);
871 assertNotNull(actionResult);
872 assertTrue(actionResult.getOutput().isEmpty());
876 public void getTwoNonOverlappingFieldsTest() throws IOException, SAXException {
877 // preparation of the fields
878 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
879 final YangInstanceIdentifier netconfStartTimeField = YangInstanceIdentifier.create(toId(Statistics.QNAME),
880 toId(QName.create(Statistics.QNAME, "netconf-start-time")));
881 final YangInstanceIdentifier datastoresField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
883 // building filter structure and NETCONF message
884 final AnyxmlNode<?> filterStructure = toFilterStructure(
885 List.of(FieldsFilter.of(parentYiid, List.of(netconfStartTimeField, datastoresField))), SCHEMA);
886 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
887 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
890 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
892 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
893 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
895 + "<netconf-start-time/>\n"
898 + "</netconf-state>\n"
905 public void getOverlappingFieldsTest() throws IOException, SAXException {
906 // preparation of the fields
907 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
908 final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
909 final YangInstanceIdentifier capabilityField = YangInstanceIdentifier.create(toId(Capabilities.QNAME),
910 toId(QName.create(Capabilities.QNAME, "capability").intern()));
911 final YangInstanceIdentifier datastoreField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
912 final YangInstanceIdentifier locksFields = YangInstanceIdentifier.create(toId(Datastores.QNAME),
913 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME));
915 // building filter structure and NETCONF message
916 final AnyxmlNode<?> filterStructure = toFilterStructure(
917 List.of(FieldsFilter.of(parentYiid,
918 List.of(capabilitiesField, capabilityField, datastoreField, locksFields))),
920 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
921 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
924 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
926 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
927 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
928 + "<capabilities/>\n"
930 + "</netconf-state>\n"
937 public void getOverlappingFieldsWithEmptyFieldTest() throws IOException, SAXException {
938 // preparation of the fields
939 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
940 final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
942 // building filter structure and NETCONF message
943 final AnyxmlNode<?> filterStructure = toFilterStructure(
944 List.of(FieldsFilter.of(parentYiid, List.of(capabilitiesField, YangInstanceIdentifier.empty()))),
946 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
947 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
950 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
952 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
953 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"/>\n"
960 public void getSpecificFieldsUnderListTest() throws IOException, SAXException {
961 // preparation of the fields
962 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
963 toId(Schemas.QNAME), toId(Schema.QNAME), NodeIdentifierWithPredicates.of(Schema.QNAME));
964 final YangInstanceIdentifier versionField = YangInstanceIdentifier.create(
965 toId(QName.create(Schema.QNAME, "version").intern()));
966 final YangInstanceIdentifier identifierField = YangInstanceIdentifier.create(
967 toId(QName.create(Schema.QNAME, "namespace").intern()));
969 // building filter structure and NETCONF message
970 final AnyxmlNode<?> filterStructure = toFilterStructure(
971 List.of(FieldsFilter.of(parentYiid, List.of(versionField, identifierField))), SCHEMA);
972 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
973 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
976 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
978 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
979 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
984 // explicitly fetched list keys - identifier and format
989 + "</netconf-state>\n"
996 public void getSpecificFieldsUnderMultipleLists() throws IOException, SAXException {
997 // preparation of the fields
998 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(
999 toId(NetconfState.QNAME), toId(Datastores.QNAME));
1000 final YangInstanceIdentifier partialLockYiid = YangInstanceIdentifier.create(toId(Datastore.QNAME),
1001 NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME),
1002 toId(QName.create(Locks.QNAME, "lock-type").intern()), toId(PartialLock.QNAME),
1003 NodeIdentifierWithPredicates.of(PartialLock.QNAME));
1004 final YangInstanceIdentifier lockedTimeField = partialLockYiid.node(
1005 QName.create(Locks.QNAME, "locked-time").intern());
1006 final YangInstanceIdentifier lockedBySessionField = partialLockYiid.node(
1007 QName.create(Locks.QNAME, "locked-by-session").intern());
1009 // building filter structure and NETCONF message
1010 final AnyxmlNode<?> filterStructure = toFilterStructure(
1011 List.of(FieldsFilter.of(parentYiid, List.of(lockedTimeField, lockedBySessionField))),
1013 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1014 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1017 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1019 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1020 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1024 + "<partial-lock>\n"
1025 + "<locked-time/>\n"
1026 + "<locked-by-session/>\n"
1028 + "</partial-lock>\n"
1033 + "</netconf-state>\n"
1040 public void getWholeListsUsingFieldsTest() throws IOException, SAXException {
1041 // preparation of the fields
1042 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
1043 final YangInstanceIdentifier datastoreListField = YangInstanceIdentifier.create(toId(Datastores.QNAME),
1044 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME));
1045 final YangInstanceIdentifier sessionListField = YangInstanceIdentifier.create(toId(Sessions.QNAME),
1046 toId(Session.QNAME), NodeIdentifierWithPredicates.of(Session.QNAME));
1048 // building filter structure and NETCONF message
1049 final AnyxmlNode<?> filterStructure = toFilterStructure(
1050 List.of(FieldsFilter.of(parentYiid, List.of(datastoreListField, sessionListField))), SCHEMA);
1051 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1052 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1055 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1057 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1058 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1065 + "</netconf-state>\n"
1072 public void getSpecificListEntriesWithSpecificFieldsTest() throws IOException, SAXException {
1073 // preparation of the fields
1074 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
1075 toId(Sessions.QNAME));
1076 final QName sessionId = QName.create(Session.QNAME, "session-id").intern();
1077 final YangInstanceIdentifier session1Field = YangInstanceIdentifier.create(toId(Session.QNAME),
1078 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 1));
1079 final YangInstanceIdentifier session2TransportField = YangInstanceIdentifier.create(toId(Session.QNAME),
1080 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 2),
1081 toId(QName.create(Session.QNAME, "transport").intern()));
1083 // building filter structure and NETCONF message
1084 final AnyxmlNode<?> filterStructure = toFilterStructure(
1085 List.of(FieldsFilter.of(parentYiid, List.of(session1Field, session2TransportField))), SCHEMA);
1086 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1087 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1090 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1092 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1093 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1096 + "<session-id>1</session-id>\n"
1099 + "<session-id>2</session-id>\n"
1103 + "</netconf-state>\n"
1110 // Proof that YANGTOOLS-1362 works on DOM level
1111 public void testConfigChangeToNotification() throws SAXException, IOException {
1112 final var message = new NetconfMessage(XmlUtil.readXmlToDocument(
1113 "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"
1114 + " <eventTime>2021-11-11T11:26:16Z</eventTime> \n"
1115 + " <netconf-config-change xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-notifications\">\n"
1116 + " <changed-by> \n"
1117 + " <username>root</username> \n"
1118 + " <session-id>3</session-id> \n"
1119 + " </changed-by> \n"
1120 + " <datastore>running</datastore> \n"
1122 + " <target xmlns:ncm=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">/ncm:netconf-state"
1123 + "/ncm:datastores/ncm:datastore[ncm:name='running']</target>\n"
1124 + " <operation>replace</operation> \n"
1126 + " </netconf-config-change> \n"
1127 + "</notification>"));
1129 final var change = netconfMessageTransformer.toNotification(message).getBody();
1130 final var editList = change.getChildByArg(new NodeIdentifier(Edit.QNAME));
1131 assertThat(editList, instanceOf(UnkeyedListNode.class));
1132 final var edits = ((UnkeyedListNode) editList).body();
1133 assertEquals(1, edits.size());
1134 final var edit = edits.iterator().next();
1135 final var target = edit.getChildByArg(new NodeIdentifier(QName.create(Edit.QNAME, "target"))).body();
1136 assertThat(target, instanceOf(YangInstanceIdentifier.class));
1138 final var args = ((YangInstanceIdentifier) target).getPathArguments();
1139 assertEquals(4, args.size());
1142 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
1143 final String inputNodeName, final String inputValue) {
1144 checkNode(action, actionQname.getLocalName(), actionQname.getLocalName(),
1145 actionQname.getNamespace().toString());
1147 Node childResetAt = action.getFirstChild();
1148 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
1150 Node firstChild = childResetAt.getFirstChild();
1151 assertEquals(firstChild.getNodeValue(), inputValue);
1154 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
1155 Node baseRpc = actionRequest.getDocument().getFirstChild();
1156 checkNode(baseRpc, "rpc", "rpc", NormalizedDataUtil.NETCONF_QNAME.getNamespace().toString());
1157 assertTrue(baseRpc.getLocalName().equals("rpc"));
1158 assertTrue(baseRpc.getNodeName().equals("rpc"));
1160 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
1161 assertNotNull(messageId);
1162 assertTrue(messageId.getNodeValue().contains("m-"));
1163 Node childAction = baseRpc.getFirstChild();
1165 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
1169 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
1170 YangInstanceIdentifier yangInstanceIdentifier =
1171 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
1172 DOMDataTreeIdentifier domDataTreeIdentifier =
1173 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
1174 yangInstanceIdentifier);
1175 return domDataTreeIdentifier;
1178 private static ContainerNode initInputAction(final QName qname, final String value) {
1179 return Builders.containerBuilder()
1180 .withNodeIdentifier(NodeIdentifier.create(QName.create(qname, "input")))
1181 .withChild(ImmutableNodes.leafNode(qname, value))
1185 private static ContainerNode initEmptyInputAction(final QName qname) {
1186 return Builders.containerBuilder()
1187 .withNodeIdentifier(NodeIdentifier.create(QName.create(qname, "input")))
1191 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
1192 final String expectedNamespace) {
1193 assertNotNull(childServer);
1194 assertEquals(childServer.getLocalName(), expectedLocalName);
1195 assertEquals(childServer.getNodeName(), expectedNodeName);
1196 assertEquals(childServer.getNamespaceURI(), expectedNamespace);