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.MatcherAssert.assertThat;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
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.util.NetconfUtil.NETCONF_DATA_QNAME;
31 import com.google.common.collect.Iterables;
32 import com.google.common.collect.Lists;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
39 import java.util.Optional;
41 import javax.xml.transform.dom.DOMSource;
42 import org.custommonkey.xmlunit.Diff;
43 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
44 import org.custommonkey.xmlunit.XMLUnit;
45 import org.hamcrest.CoreMatchers;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
51 import org.opendaylight.mdsal.dom.api.DOMActionResult;
52 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
53 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
54 import org.opendaylight.netconf.api.NetconfMessage;
55 import org.opendaylight.netconf.api.xml.XmlUtil;
56 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
57 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
58 import org.opendaylight.netconf.sal.connect.netconf.util.FieldsFilter;
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.IetfNetconfService;
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.Datastores;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Statistics;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.Datastore;
70 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.datastores.datastore.Locks;
71 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;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
73 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
74 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
75 import org.opendaylight.yangtools.yang.common.QName;
76 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
77 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
78 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
79 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
80 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
81 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
82 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
83 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
84 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
85 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
86 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
89 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
90 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
91 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
92 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
93 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
94 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
95 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
96 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
97 import org.w3c.dom.Node;
98 import org.xml.sax.SAXException;
100 public class NetconfMessageTransformerTest extends AbstractBaseSchemasTest {
102 private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
103 private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
105 private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
106 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
108 private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
110 private static final String URN_EXAMPLE_AUGMENTED_ACTION = "urn:example:augmented-action";
112 private static final String URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS = "urn:example:rpcs-actions-outputs";
114 private static final QName SERVER_QNAME =
115 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
116 private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
117 private static final Absolute RESET_SERVER_PATH = Absolute.of(SERVER_QNAME, RESET_QNAME);
118 private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
119 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
120 private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
121 private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
122 private static final Absolute KILL_SERVER_APP_PATH =
123 Absolute.of(SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
125 private static final QName DEVICE_QNAME =
126 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
127 private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
128 private static final Absolute START_DEVICE_PATH = Absolute.of(DEVICE_QNAME, START_QNAME);
129 private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
130 private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
131 private static final Absolute ENABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
133 private static final QName DISABLE_QNAME = QName.create(URN_EXAMPLE_AUGMENTED_ACTION, "disable");
134 private static final Absolute DISABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, DISABLE_QNAME);
136 private static final QName CHECK_WITH_OUTPUT_QNAME =
137 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-with-output");
138 private static final Absolute CHECK_WITH_OUTPUT_INTERFACE_PATH =
139 Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITH_OUTPUT_QNAME);
140 private static final QName CHECK_WITHOUT_OUTPUT_QNAME =
141 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-without-output");
142 private static final Absolute CHECK_WITHOUT_OUTPUT_INTERFACE_PATH =
143 Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITHOUT_OUTPUT_QNAME);
144 private static final QName RPC_WITH_OUTPUT_QNAME =
145 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-with-output");
146 private static final QName RPC_WITHOUT_OUTPUT_QNAME =
147 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-without-output");
149 private static final QName BOX_OUT_QNAME =
150 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
151 private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
152 private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
153 private static final Absolute OPEN_BOXES_PATH = Absolute.of(BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
155 private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
156 private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
157 private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
158 private static final Absolute XYZZY_FOO_PATH = Absolute.of(FOO_QNAME, XYZZY_QNAME);
159 private static final Absolute XYZZY_BAR_PATH = Absolute.of(BAR_QNAME, XYZZY_QNAME);
161 private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
162 private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
163 private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
164 private static final Absolute CHOICE_ACTION_PATH =
165 Absolute.of(CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
167 private static EffectiveModelContext PARTIAL_SCHEMA;
168 private static EffectiveModelContext SCHEMA;
169 private static EffectiveModelContext ACTION_SCHEMA;
171 private NetconfMessageTransformer actionNetconfMessageTransformer;
172 private NetconfMessageTransformer netconfMessageTransformer;
175 public static void beforeClass() {
176 PARTIAL_SCHEMA = BindingRuntimeHelpers.createEffectiveModel(NetconfState.class);
177 SCHEMA = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class);
178 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
179 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
180 "/schemas/conflicting-actions.yang", "/schemas/augmented-action.yang",
181 "/schemas/rpcs-actions-outputs.yang");
185 public static void afterClass() {
186 PARTIAL_SCHEMA = null;
188 ACTION_SCHEMA = null;
192 public void setUp() throws Exception {
193 XMLUnit.setIgnoreWhitespace(true);
194 XMLUnit.setIgnoreAttributeOrder(true);
195 XMLUnit.setIgnoreComments(true);
197 netconfMessageTransformer = getTransformer(SCHEMA);
198 actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
199 true, BASE_SCHEMAS.getBaseSchema());
203 public void testLockRequestBaseSchemaNotPresent() throws Exception {
204 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
205 final NetconfMessage netconfMessage = transformer.toRpcRequest(NETCONF_LOCK_QNAME,
206 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
208 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
209 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
213 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
214 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
215 true, BASE_SCHEMAS.getBaseSchemaWithNotifications());
216 NetconfMessage netconfMessage = transformer.toRpcRequest(CREATE_SUBSCRIPTION_RPC_QNAME,
217 CREATE_SUBSCRIPTION_RPC_CONTENT);
218 String documentString = XmlUtil.toString(netconfMessage.getDocument());
219 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
220 assertThat(documentString, CoreMatchers.containsString("<rpc"));
224 public void tesLockSchemaRequest() throws Exception {
225 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
226 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
228 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), NETCONF_LOCK_QNAME);
232 public void testRpcEmptyBodyWithOutputDefinedSchemaResult() throws Exception {
233 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
235 DOMRpcResult domRpcResult = actionNetconfMessageTransformer
236 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITH_OUTPUT_QNAME);
237 assertNotNull(domRpcResult);
241 public void testRpcEmptyBodyWithoutOutputDefinedSchemaResult() throws Exception {
242 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
244 DOMRpcResult domRpcResult = actionNetconfMessageTransformer
245 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITHOUT_OUTPUT_QNAME);
246 assertNotNull(domRpcResult);
250 public void testDiscardChangesRequest() throws Exception {
251 final NetconfMessage netconfMessage =
252 netconfMessageTransformer.toRpcRequest(NETCONF_DISCARD_CHANGES_QNAME, null);
253 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
254 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
255 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
259 public void testGetSchemaRequest() throws Exception {
260 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(GET_SCHEMA_QNAME,
261 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
262 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
263 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
264 + "<format>yang</format>\n"
265 + "<identifier>module</identifier>\n"
266 + "<version>2012-12-12</version>\n"
272 public void testGetSchemaResponse() throws Exception {
273 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
274 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
275 "<rpc-reply message-id=\"101\"\n"
276 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
278 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
279 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
280 + "Random YANG SCHEMA\n"
285 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, GET_SCHEMA_QNAME);
286 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
287 assertNotNull(compositeNodeRpcResult.getResult());
288 final DOMSource schemaContent = ((DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
289 .getValue().iterator().next()).getValue();
290 assertThat(schemaContent.getNode().getTextContent(),
291 CoreMatchers.containsString("Random YANG SCHEMA"));
295 public void testGetConfigResponse() throws Exception {
296 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
297 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
299 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
302 + "<identifier>module</identifier>\n"
303 + "<version>2012-12-12</version>\n"
304 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
307 + "</netconf-state>\n"
311 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
312 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, NETCONF_GET_CONFIG_QNAME);
313 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
314 assertNotNull(compositeNodeRpcResult.getResult());
316 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
317 NetconfRemoteSchemaYangSourceProvider
318 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
320 final Map<QName, Object> keys = new HashMap<>();
321 for (final DataContainerChild<?, ?> value : values) {
322 keys.put(value.getNodeType(), value.getValue());
325 final NodeIdentifierWithPredicates identifierWithPredicates =
326 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
327 final MapEntryNode schemaNode =
328 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
330 final DOMSourceAnyxmlNode data = (DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
331 .getChild(toId(NETCONF_DATA_QNAME)).get();
333 NormalizedNodeResult nodeResult =
334 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
335 ContainerNode result = (ContainerNode) nodeResult.getResult();
336 final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
337 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
338 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
339 assertEquals(1, Iterables.size(schemaParent.getValue()));
341 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
345 public void testGetConfigLeafRequest() throws Exception {
346 final DataContainerChild<?, ?> filter = toFilterStructure(
347 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
348 NodeIdentifierWithPredicates.of(Schema.QNAME),
349 toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
351 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
353 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
354 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
356 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
357 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
358 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
359 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
365 + "</netconf-state>\n"
375 public void testGetConfigRequest() throws Exception {
376 final DataContainerChild<?, ?> filter = toFilterStructure(
377 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
379 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
381 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
382 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
384 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
385 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
386 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
387 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
399 public void testEditConfigRequest() throws Exception {
400 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
401 NetconfRemoteSchemaYangSourceProvider
402 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
404 final Map<QName, Object> keys = new HashMap<>();
405 for (final DataContainerChild<?, ?> value : values) {
406 keys.put(value.getNodeType(), value.getValue());
409 final NodeIdentifierWithPredicates identifierWithPredicates =
410 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
411 final MapEntryNode schemaNode =
412 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
414 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
415 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
416 .nodeWithKey(Schema.QNAME, keys).build();
417 final DataContainerChild<?, ?> editConfigStructure =
418 createEditConfigStructure(BASE_SCHEMAS.getBaseSchemaWithNotifications().getEffectiveModelContext(), id,
419 Optional.empty(), Optional.ofNullable(schemaNode));
421 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
423 final ContainerNode wrap =
424 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
425 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_EDIT_CONFIG_QNAME, wrap);
427 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
433 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
436 + "<identifier>module</identifier>\n"
437 + "<version>2012-12-12</version>\n"
438 + "<format>yang</format>\n"
441 + "</netconf-state>\n"
447 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
448 throws SAXException, IOException {
449 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
450 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
451 assertTrue(diff.toString(), diff.similar());
455 public void testGetRequest() throws Exception {
457 final QName capability = QName.create(Capabilities.QNAME, "capability");
458 final DataContainerChild<?, ?> filter = toFilterStructure(
459 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
460 new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
462 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
463 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
465 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
466 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
467 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
468 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
470 + "<capability>a:b:c</capability>\n"
471 + "</capabilities>\n"
479 public void testGetLeafList() throws IOException, SAXException {
480 final YangInstanceIdentifier path = YangInstanceIdentifier.create(
481 toId(NetconfState.QNAME),
482 toId(Capabilities.QNAME),
483 toId(QName.create(Capabilities.QNAME, "capability")));
484 final DataContainerChild<?, ?> filter = toFilterStructure(path, SCHEMA);
485 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
486 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filter));
488 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
490 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
491 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
494 + "</capabilities>\n"
495 + "</netconf-state>\n"
502 public void testGetList() throws IOException, SAXException {
503 final YangInstanceIdentifier path = YangInstanceIdentifier.create(
504 toId(NetconfState.QNAME),
505 toId(Datastores.QNAME),
506 toId(QName.create(Datastores.QNAME, "datastore")));
507 final DataContainerChild<?, ?> filter = toFilterStructure(path, SCHEMA);
508 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
509 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filter));
511 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
513 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
514 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
518 + "</netconf-state>\n"
524 private static NetconfMessageTransformer getTransformer(final EffectiveModelContext schema) {
525 return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true, BASE_SCHEMAS.getBaseSchema());
529 public void testCommitResponse() throws Exception {
530 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
531 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
533 final DOMRpcResult compositeNodeRpcResult =
534 netconfMessageTransformer.toRpcResult(response, NETCONF_COMMIT_QNAME);
535 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
536 assertNull(compositeNodeRpcResult.getResult());
540 public void getActionsTest() {
541 Set<Absolute> schemaPaths = new HashSet<>();
542 schemaPaths.add(RESET_SERVER_PATH);
543 schemaPaths.add(START_DEVICE_PATH);
544 schemaPaths.add(ENABLE_INTERFACE_PATH);
545 schemaPaths.add(OPEN_BOXES_PATH);
546 schemaPaths.add(KILL_SERVER_APP_PATH);
547 schemaPaths.add(XYZZY_FOO_PATH);
548 schemaPaths.add(XYZZY_BAR_PATH);
549 schemaPaths.add(CHOICE_ACTION_PATH);
550 schemaPaths.add(DISABLE_INTERFACE_PATH);
551 schemaPaths.add(CHECK_WITH_OUTPUT_INTERFACE_PATH);
552 schemaPaths.add(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH);
554 List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
555 assertEquals(schemaPaths.size(), actions.size());
556 for (ActionDefinition actionDefinition : actions) {
557 Absolute path = actionDefinition.getPath().asAbsolute();
558 assertTrue(schemaPaths.remove(path));
563 public void toActionRequestListTopLevelTest() {
564 QName nameQname = QName.create(SERVER_QNAME, "name");
565 List<PathArgument> nodeIdentifiers = new ArrayList<>();
566 nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
567 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
568 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
570 ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
572 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
573 RESET_SERVER_PATH, domDataTreeIdentifier, data);
575 Node childAction = checkBasePartOfActionRequest(actionRequest);
577 Node childServer = childAction.getFirstChild();
578 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
580 Node childName = childServer.getFirstChild();
581 checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
583 Node childTest = childName.getFirstChild();
584 assertEquals(childTest.getNodeValue(), "test");
586 checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
590 public void toActionRequestContainerTopLevelTest() {
591 List<PathArgument> nodeIdentifiers = List.of(NodeIdentifier.create(DEVICE_QNAME));
592 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
594 NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
595 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
596 START_DEVICE_PATH, domDataTreeIdentifier, payload);
598 Node childAction = checkBasePartOfActionRequest(actionRequest);
600 Node childDevice = childAction.getFirstChild();
601 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
603 checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
607 public void toActionRequestContainerInContainerTest() {
608 List<PathArgument> nodeIdentifiers = new ArrayList<>();
609 nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
610 nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
612 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
614 NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
615 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
616 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
618 Node childAction = checkBasePartOfActionRequest(actionRequest);
620 Node childBoxOut = childAction.getFirstChild();
621 checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
623 Node childBoxIn = childBoxOut.getFirstChild();
624 checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
626 Node action = childBoxIn.getFirstChild();
627 checkNode(action, OPEN_QNAME.getLocalName(), OPEN_QNAME.getLocalName(), OPEN_QNAME.getNamespace().toString());
631 public void toActionRequestListInContainerTest() {
632 QName nameQname = QName.create(INTERFACE_QNAME, "name");
634 List<PathArgument> nodeIdentifiers = new ArrayList<>();
635 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
636 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
637 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
639 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
641 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
642 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
643 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
645 Node childAction = checkBasePartOfActionRequest(actionRequest);
647 Node childDevice = childAction.getFirstChild();
648 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
650 Node childInterface = childDevice.getFirstChild();
651 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
653 Node childName = childInterface.getFirstChild();
654 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
656 Node childTest = childName.getFirstChild();
657 assertEquals(childTest.getNodeValue(), "test");
659 Node action = childInterface.getLastChild();
660 checkNode(action, ENABLE_QNAME.getLocalName(), ENABLE_QNAME.getLocalName(),
661 ENABLE_QNAME.getNamespace().toString());
665 public void toActionRequestListInContainerAugmentedIntoListTest() {
666 QName serverNameQname = QName.create(SERVER_QNAME, "name");
667 QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
669 List<PathArgument> nodeIdentifiers = new ArrayList<>();
670 nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
671 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
672 nodeIdentifiers.add(new AugmentationIdentifier(Set.of(APPLICATIONS_QNAME)));
673 nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
674 nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
675 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
676 applicationNameQname, "testApplication"));
678 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
680 NormalizedNode<?, ?> payload = initEmptyInputAction(APPLICATION_QNAME);
681 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
682 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
684 Node childAction = checkBasePartOfActionRequest(actionRequest);
686 Node childServer = childAction.getFirstChild();
687 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
689 Node childServerName = childServer.getFirstChild();
690 checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
692 Node childServerNameTest = childServerName.getFirstChild();
693 assertEquals(childServerNameTest.getNodeValue(), "testServer");
695 Node childApplications = childServer.getLastChild();
696 checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
698 Node childApplication = childApplications.getFirstChild();
699 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
701 Node childApplicationName = childApplication.getFirstChild();
702 checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
704 Node childApplicationNameTest = childApplicationName.getFirstChild();
705 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
707 Node childKillAction = childApplication.getLastChild();
708 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
709 checkNode(childKillAction, KILL_QNAME.getLocalName(), KILL_QNAME.getLocalName(),
710 KILL_QNAME.getNamespace().toString());
714 public void toActionRequestConflictingInListTest() {
715 QName barInputQname = QName.create(BAR_QNAME, "bar");
716 QName barIdQname = QName.create(BAR_QNAME, "bar-id");
719 List<PathArgument> nodeIdentifiers = new ArrayList<>();
720 nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
721 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
723 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
725 ImmutableLeafNodeBuilder<Byte> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
726 DataContainerChild<NodeIdentifier, Byte> build = immutableLeafNodeBuilder.withNodeIdentifier(
727 NodeIdentifier.create(barInputQname)).withValue(barInput).build();
728 NormalizedNode<?, ?> payload = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
729 QName.create(barInputQname, "input"))).withChild(build).build();
731 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
732 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
734 Node childAction = checkBasePartOfActionRequest(actionRequest);
736 Node childBar = childAction.getFirstChild();
737 checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
739 Node childBarId = childBar.getFirstChild();
740 checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
742 Node childTest = childBarId.getFirstChild();
743 assertEquals(childTest.getNodeValue(), "test");
745 Node action = childBar.getLastChild();
746 checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
747 XYZZY_QNAME.getNamespace().toString());
751 public void toActionRequestConflictingInContainerTest() {
752 QName fooInputQname = QName.create(FOO_QNAME, "foo");
754 List<PathArgument> nodeIdentifiers = new ArrayList<>();
755 nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
756 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
757 NormalizedNode<?, ?> payload = initInputAction(fooInputQname, "test");
759 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
760 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
762 Node childAction = checkBasePartOfActionRequest(actionRequest);
764 Node childBar = childAction.getFirstChild();
765 checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
767 Node action = childBar.getLastChild();
768 checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
769 XYZZY_QNAME.getNamespace().toString());
773 public void toActionRequestChoiceTest() {
774 List<PathArgument> nodeIdentifiers = new ArrayList<>();
775 nodeIdentifiers.add(NodeIdentifier.create(CONFLICT_CHOICE_QNAME));
776 nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
777 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
778 NormalizedNode<?, ?> payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
780 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
781 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
783 Node childAction = checkBasePartOfActionRequest(actionRequest);
785 Node childChoiceCont = childAction.getFirstChild();
786 checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
788 Node action = childChoiceCont.getLastChild();
789 checkNode(action, CHOICE_ACTION_QNAME.getLocalName(), CHOICE_ACTION_QNAME.getLocalName(),
790 CHOICE_ACTION_QNAME.getNamespace().toString());
794 public void toAugmentedActionRequestListInContainerTest() {
795 QName nameQname = QName.create(INTERFACE_QNAME, "name");
797 List<PathArgument> nodeIdentifiers = new ArrayList<>();
798 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
799 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
800 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
802 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
804 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
805 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
806 DISABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
808 Node childAction = checkBasePartOfActionRequest(actionRequest);
810 Node childDevice = childAction.getFirstChild();
811 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
813 Node childInterface = childDevice.getFirstChild();
814 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
816 Node childName = childInterface.getFirstChild();
817 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
819 Node childTest = childName.getFirstChild();
820 assertEquals(childTest.getNodeValue(), "test");
822 Node action = childInterface.getLastChild();
823 checkNode(action, DISABLE_QNAME.getLocalName(), DISABLE_QNAME.getLocalName(),
824 DISABLE_QNAME.getNamespace().toString());
827 @SuppressWarnings({ "rawtypes", "unchecked" })
829 public void toActionResultTest() throws Exception {
830 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
831 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
832 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
834 + "</reset-finished-at>"
836 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
837 assertNotNull(actionResult);
838 ContainerNode containerNode = actionResult.getOutput().get();
839 assertNotNull(containerNode);
840 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
841 assertEquals("now", leaf.getValue());
845 public void toActionEmptyBodyWithOutputDefinedResultTest() throws Exception {
846 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
847 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
850 DOMActionResult actionResult =
851 actionNetconfMessageTransformer.toActionResult(CHECK_WITH_OUTPUT_INTERFACE_PATH, message);
852 assertNotNull(actionResult);
853 assertTrue(actionResult.getOutput().isEmpty());
857 public void toActionEmptyBodyWithoutOutputDefinedResultTest() throws Exception {
858 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
859 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
862 DOMActionResult actionResult =
863 actionNetconfMessageTransformer.toActionResult(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH, message);
864 assertNotNull(actionResult);
865 assertTrue(actionResult.getOutput().isEmpty());
869 public void getTwoNonOverlappingFieldsTest() throws IOException, SAXException {
870 // preparation of the fields
871 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
872 final YangInstanceIdentifier netconfStartTimeField = YangInstanceIdentifier.create(toId(Statistics.QNAME),
873 toId(QName.create(Statistics.QNAME, "netconf-start-time")));
874 final YangInstanceIdentifier datastoresField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
876 // building filter structure and NETCONF message
877 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
878 List.of(FieldsFilter.of(parentYiid, List.of(netconfStartTimeField, datastoresField))), SCHEMA);
879 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
880 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
883 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
885 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
886 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
888 + "<netconf-start-time/>\n"
891 + "</netconf-state>\n"
898 public void getOverlappingFieldsTest() throws IOException, SAXException {
899 // preparation of the fields
900 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
901 final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
902 final YangInstanceIdentifier capabilityField = YangInstanceIdentifier.create(toId(Capabilities.QNAME),
903 toId(QName.create(Capabilities.QNAME, "capability").intern()));
904 final YangInstanceIdentifier datastoreField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
905 final YangInstanceIdentifier locksFields = YangInstanceIdentifier.create(toId(Datastores.QNAME),
906 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME));
908 // building filter structure and NETCONF message
909 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
910 List.of(FieldsFilter.of(parentYiid,
911 List.of(capabilitiesField, capabilityField, datastoreField, locksFields))),
913 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
914 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
917 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
919 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
920 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
921 + "<capabilities/>\n"
923 + "</netconf-state>\n"
930 public void getOverlappingFieldsWithEmptyFieldTest() throws IOException, SAXException {
931 // preparation of the fields
932 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
933 final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
935 // building filter structure and NETCONF message
936 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
937 List.of(FieldsFilter.of(parentYiid, List.of(capabilitiesField, YangInstanceIdentifier.empty()))),
939 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
940 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
943 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
945 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
946 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"/>\n"
953 public void getSpecificFieldsUnderListTest() throws IOException, SAXException {
954 // preparation of the fields
955 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
956 toId(Schemas.QNAME), toId(Schema.QNAME), NodeIdentifierWithPredicates.of(Schema.QNAME));
957 final YangInstanceIdentifier versionField = YangInstanceIdentifier.create(
958 toId(QName.create(Schema.QNAME, "version").intern()));
959 final YangInstanceIdentifier identifierField = YangInstanceIdentifier.create(
960 toId(QName.create(Schema.QNAME, "namespace").intern()));
962 // building filter structure and NETCONF message
963 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
964 List.of(FieldsFilter.of(parentYiid, List.of(versionField, identifierField))), SCHEMA);
965 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
966 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
969 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
971 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
972 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
977 // explicitly fetched list keys - identifier and format
982 + "</netconf-state>\n"
989 public void getSpecificFieldsUnderMultipleLists() throws IOException, SAXException {
990 // preparation of the fields
991 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(
992 toId(NetconfState.QNAME), toId(Datastores.QNAME));
993 final YangInstanceIdentifier partialLockYiid = YangInstanceIdentifier.create(toId(Datastore.QNAME),
994 NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME),
995 toId(QName.create(Locks.QNAME, "lock-type").intern()), toId(PartialLock.QNAME),
996 NodeIdentifierWithPredicates.of(PartialLock.QNAME));
997 final YangInstanceIdentifier lockedTimeField = partialLockYiid.node(
998 QName.create(Locks.QNAME, "locked-time").intern());
999 final YangInstanceIdentifier lockedBySessionField = partialLockYiid.node(
1000 QName.create(Locks.QNAME, "locked-by-session").intern());
1002 // building filter structure and NETCONF message
1003 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
1004 List.of(FieldsFilter.of(parentYiid, List.of(lockedTimeField, lockedBySessionField))),
1006 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1007 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1010 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1012 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1013 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1017 + "<partial-lock>\n"
1018 + "<locked-time/>\n"
1019 + "<locked-by-session/>\n"
1021 + "</partial-lock>\n"
1026 + "</netconf-state>\n"
1033 public void getWholeListsUsingFieldsTest() throws IOException, SAXException {
1034 // preparation of the fields
1035 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
1036 final YangInstanceIdentifier datastoreListField = YangInstanceIdentifier.create(toId(Datastores.QNAME),
1037 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME));
1038 final YangInstanceIdentifier sessionListField = YangInstanceIdentifier.create(toId(Sessions.QNAME),
1039 toId(Session.QNAME), NodeIdentifierWithPredicates.of(Session.QNAME));
1041 // building filter structure and NETCONF message
1042 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
1043 List.of(FieldsFilter.of(parentYiid, List.of(datastoreListField, sessionListField))), SCHEMA);
1044 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1045 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1048 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1050 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1051 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1058 + "</netconf-state>\n"
1065 public void getSpecificListEntriesWithSpecificFieldsTest() throws IOException, SAXException {
1066 // preparation of the fields
1067 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
1068 toId(Sessions.QNAME));
1069 final QName sessionId = QName.create(Session.QNAME, "session-id").intern();
1070 final YangInstanceIdentifier session1Field = YangInstanceIdentifier.create(toId(Session.QNAME),
1071 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 1));
1072 final YangInstanceIdentifier session2TransportField = YangInstanceIdentifier.create(toId(Session.QNAME),
1073 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 2),
1074 toId(QName.create(Session.QNAME, "transport").intern()));
1076 // building filter structure and NETCONF message
1077 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
1078 List.of(FieldsFilter.of(parentYiid, List.of(session1Field, session2TransportField))), SCHEMA);
1079 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1080 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1083 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1085 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1086 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1089 + "<session-id>1</session-id>\n"
1092 + "<session-id>2</session-id>\n"
1096 + "</netconf-state>\n"
1102 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
1103 final String inputNodeName, final String inputValue) {
1104 checkNode(action, actionQname.getLocalName(), actionQname.getLocalName(),
1105 actionQname.getNamespace().toString());
1107 Node childResetAt = action.getFirstChild();
1108 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
1110 Node firstChild = childResetAt.getFirstChild();
1111 assertEquals(firstChild.getNodeValue(), inputValue);
1114 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
1115 Node baseRpc = actionRequest.getDocument().getFirstChild();
1116 checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
1117 assertTrue(baseRpc.getLocalName().equals("rpc"));
1118 assertTrue(baseRpc.getNodeName().equals("rpc"));
1120 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
1121 assertNotNull(messageId);
1122 assertTrue(messageId.getNodeValue().contains("m-"));
1123 Node childAction = baseRpc.getFirstChild();
1125 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
1129 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
1130 YangInstanceIdentifier yangInstanceIdentifier =
1131 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
1132 DOMDataTreeIdentifier domDataTreeIdentifier =
1133 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
1134 yangInstanceIdentifier);
1135 return domDataTreeIdentifier;
1138 private static ContainerNode initInputAction(final QName qname, final String value) {
1139 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
1140 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
1141 NodeIdentifier.create(qname)).withValue(value).build();
1142 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
1143 QName.create(qname, "input"))).withChild(build).build();
1147 private static ContainerNode initEmptyInputAction(final QName qname) {
1148 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
1149 QName.create(qname, "input"))).build();
1152 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
1153 final String expectedNamespace) {
1154 assertNotNull(childServer);
1155 assertEquals(childServer.getLocalName(), expectedLocalName);
1156 assertEquals(childServer.getNodeName(), expectedNodeName);
1157 assertEquals(childServer.getNamespaceURI(), expectedNamespace);