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.Collections;
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.sal.connect.netconf.AbstractBaseSchemasTest;
58 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
59 import org.opendaylight.netconf.sal.connect.netconf.util.FieldsFilter;
60 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
61 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
62 import org.opendaylight.netconf.util.NetconfUtil;
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.yangtools.rcf8528.data.util.EmptyMountPointContext;
76 import org.opendaylight.yangtools.yang.common.QName;
77 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
78 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
79 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
80 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
81 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
83 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
84 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
85 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
86 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
89 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
90 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
91 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
92 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
93 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
94 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
95 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
96 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
97 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
98 import org.w3c.dom.Node;
99 import org.xml.sax.SAXException;
101 public class NetconfMessageTransformerTest extends AbstractBaseSchemasTest {
103 private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
104 private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
106 private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
107 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
109 private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
111 private static final String URN_EXAMPLE_AUGMENTED_ACTION = "urn:example:augmented-action";
113 private static final String URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS = "urn:example:rpcs-actions-outputs";
115 private static final QName SERVER_QNAME =
116 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
117 private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
118 private static final Absolute RESET_SERVER_PATH = Absolute.of(SERVER_QNAME, RESET_QNAME);
119 private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
120 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
121 private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
122 private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
123 private static final Absolute KILL_SERVER_APP_PATH =
124 Absolute.of(SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
126 private static final QName DEVICE_QNAME =
127 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
128 private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
129 private static final Absolute START_DEVICE_PATH = Absolute.of(DEVICE_QNAME, START_QNAME);
130 private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
131 private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
132 private static final Absolute ENABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
134 private static final QName DISABLE_QNAME = QName.create(URN_EXAMPLE_AUGMENTED_ACTION, "disable");
135 private static final Absolute DISABLE_INTERFACE_PATH = Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, DISABLE_QNAME);
137 private static final QName CHECK_WITH_OUTPUT_QNAME =
138 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-with-output");
139 private static final Absolute CHECK_WITH_OUTPUT_INTERFACE_PATH =
140 Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITH_OUTPUT_QNAME);
141 private static final QName CHECK_WITHOUT_OUTPUT_QNAME =
142 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "check-without-output");
143 private static final Absolute CHECK_WITHOUT_OUTPUT_INTERFACE_PATH =
144 Absolute.of(DEVICE_QNAME, INTERFACE_QNAME, CHECK_WITHOUT_OUTPUT_QNAME);
145 private static final QName RPC_WITH_OUTPUT_QNAME =
146 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-with-output");
147 private static final QName RPC_WITHOUT_OUTPUT_QNAME =
148 QName.create(URN_EXAMPLE_RPCS_ACTIONS_OUTPUTS, "rpc-without-output");
150 private static final QName BOX_OUT_QNAME =
151 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
152 private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
153 private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
154 private static final Absolute OPEN_BOXES_PATH = Absolute.of(BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
156 private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
157 private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
158 private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
159 private static final Absolute XYZZY_FOO_PATH = Absolute.of(FOO_QNAME, XYZZY_QNAME);
160 private static final Absolute XYZZY_BAR_PATH = Absolute.of(BAR_QNAME, XYZZY_QNAME);
162 private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
163 private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
164 private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
165 private static final Absolute CHOICE_ACTION_PATH =
166 Absolute.of(CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
168 private static EffectiveModelContext PARTIAL_SCHEMA;
169 private static EffectiveModelContext SCHEMA;
170 private static EffectiveModelContext ACTION_SCHEMA;
172 private NetconfMessageTransformer actionNetconfMessageTransformer;
173 private NetconfMessageTransformer netconfMessageTransformer;
176 public static void beforeClass() {
177 PARTIAL_SCHEMA = BindingRuntimeHelpers.createEffectiveModel(NetconfState.class);
178 SCHEMA = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class);
179 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
180 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
181 "/schemas/conflicting-actions.yang", "/schemas/augmented-action.yang",
182 "/schemas/rpcs-actions-outputs.yang");
186 public static void afterClass() {
187 PARTIAL_SCHEMA = null;
189 ACTION_SCHEMA = null;
193 public void setUp() throws Exception {
194 XMLUnit.setIgnoreWhitespace(true);
195 XMLUnit.setIgnoreAttributeOrder(true);
196 XMLUnit.setIgnoreComments(true);
198 netconfMessageTransformer = getTransformer(SCHEMA);
199 actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
200 true, BASE_SCHEMAS.getBaseSchema());
204 public void testLockRequestBaseSchemaNotPresent() throws Exception {
205 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
206 final NetconfMessage netconfMessage = transformer.toRpcRequest(NETCONF_LOCK_QNAME,
207 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
209 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
210 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
214 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
215 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
216 true, BASE_SCHEMAS.getBaseSchemaWithNotifications());
217 NetconfMessage netconfMessage = transformer.toRpcRequest(CREATE_SUBSCRIPTION_RPC_QNAME,
218 CREATE_SUBSCRIPTION_RPC_CONTENT);
219 String documentString = XmlUtil.toString(netconfMessage.getDocument());
220 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
221 assertThat(documentString, CoreMatchers.containsString("<rpc"));
225 public void tesLockSchemaRequest() throws Exception {
226 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
227 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
229 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), NETCONF_LOCK_QNAME);
233 public void testRpcEmptyBodyWithOutputDefinedSchemaResult() throws Exception {
234 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
236 DOMRpcResult domRpcResult = actionNetconfMessageTransformer
237 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITH_OUTPUT_QNAME);
238 assertNotNull(domRpcResult);
242 public void testRpcEmptyBodyWithoutOutputDefinedSchemaResult() throws Exception {
243 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
245 DOMRpcResult domRpcResult = actionNetconfMessageTransformer
246 .toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), RPC_WITHOUT_OUTPUT_QNAME);
247 assertNotNull(domRpcResult);
251 public void testDiscardChangesRequest() throws Exception {
252 final NetconfMessage netconfMessage =
253 netconfMessageTransformer.toRpcRequest(NETCONF_DISCARD_CHANGES_QNAME, null);
254 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
255 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
256 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
260 public void testGetSchemaRequest() throws Exception {
261 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(GET_SCHEMA_QNAME,
262 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
263 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
264 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
265 + "<format>yang</format>\n"
266 + "<identifier>module</identifier>\n"
267 + "<version>2012-12-12</version>\n"
273 public void testGetSchemaResponse() throws Exception {
274 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
275 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
276 "<rpc-reply message-id=\"101\"\n"
277 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
279 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
280 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
281 + "Random YANG SCHEMA\n"
286 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, GET_SCHEMA_QNAME);
287 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
288 assertNotNull(compositeNodeRpcResult.getResult());
289 final DOMSource schemaContent = ((DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
290 .getValue().iterator().next()).getValue();
291 assertThat(schemaContent.getNode().getTextContent(),
292 CoreMatchers.containsString("Random YANG SCHEMA"));
296 public void testGetConfigResponse() throws Exception {
297 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
298 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
300 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
303 + "<identifier>module</identifier>\n"
304 + "<version>2012-12-12</version>\n"
305 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
308 + "</netconf-state>\n"
312 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
313 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, NETCONF_GET_CONFIG_QNAME);
314 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
315 assertNotNull(compositeNodeRpcResult.getResult());
317 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
318 NetconfRemoteSchemaYangSourceProvider
319 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
321 final Map<QName, Object> keys = new HashMap<>();
322 for (final DataContainerChild<?, ?> value : values) {
323 keys.put(value.getNodeType(), value.getValue());
326 final NodeIdentifierWithPredicates identifierWithPredicates =
327 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
328 final MapEntryNode schemaNode =
329 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
331 final DOMSourceAnyxmlNode data = (DOMSourceAnyxmlNode) ((ContainerNode) compositeNodeRpcResult.getResult())
332 .getChild(toId(NETCONF_DATA_QNAME)).get();
334 NormalizedNodeResult nodeResult =
335 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
336 ContainerNode result = (ContainerNode) nodeResult.getResult();
337 final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
338 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
339 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
340 assertEquals(1, Iterables.size(schemaParent.getValue()));
342 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
346 public void testGetConfigLeafRequest() throws Exception {
347 final DataContainerChild<?, ?> filter = toFilterStructure(
348 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
349 NodeIdentifierWithPredicates.of(Schema.QNAME),
350 toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
352 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
354 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
355 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
357 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
358 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
359 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
360 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
366 + "</netconf-state>\n"
376 public void testGetConfigRequest() throws Exception {
377 final DataContainerChild<?, ?> filter = toFilterStructure(
378 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
380 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
382 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_CONFIG_QNAME,
383 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
385 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
386 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
387 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
388 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
400 public void testEditConfigRequest() throws Exception {
401 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
402 NetconfRemoteSchemaYangSourceProvider
403 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
405 final Map<QName, Object> keys = new HashMap<>();
406 for (final DataContainerChild<?, ?> value : values) {
407 keys.put(value.getNodeType(), value.getValue());
410 final NodeIdentifierWithPredicates identifierWithPredicates =
411 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
412 final MapEntryNode schemaNode =
413 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
415 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
416 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
417 .nodeWithKey(Schema.QNAME, keys).build();
418 final DataContainerChild<?, ?> editConfigStructure =
419 createEditConfigStructure(BASE_SCHEMAS.getBaseSchemaWithNotifications().getEffectiveModelContext(), id,
420 Optional.empty(), Optional.ofNullable(schemaNode));
422 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
424 final ContainerNode wrap =
425 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
426 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_EDIT_CONFIG_QNAME, wrap);
428 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
434 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
437 + "<identifier>module</identifier>\n"
438 + "<version>2012-12-12</version>\n"
439 + "<format>yang</format>\n"
442 + "</netconf-state>\n"
448 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
449 throws SAXException, IOException {
450 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
451 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
452 assertTrue(diff.toString(), diff.similar());
456 public void testGetRequest() throws Exception {
458 final QName capability = QName.create(Capabilities.QNAME, "capability");
459 final DataContainerChild<?, ?> filter = toFilterStructure(
460 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
461 new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
463 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
464 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
466 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
467 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
468 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
469 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
471 + "<capability>a:b:c</capability>\n"
472 + "</capabilities>\n"
479 private static NetconfMessageTransformer getTransformer(final EffectiveModelContext schema) {
480 return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true, BASE_SCHEMAS.getBaseSchema());
484 public void testCommitResponse() throws Exception {
485 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
486 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
488 final DOMRpcResult compositeNodeRpcResult =
489 netconfMessageTransformer.toRpcResult(response, NETCONF_COMMIT_QNAME);
490 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
491 assertNull(compositeNodeRpcResult.getResult());
495 public void getActionsTest() {
496 Set<Absolute> schemaPaths = new HashSet<>();
497 schemaPaths.add(RESET_SERVER_PATH);
498 schemaPaths.add(START_DEVICE_PATH);
499 schemaPaths.add(ENABLE_INTERFACE_PATH);
500 schemaPaths.add(OPEN_BOXES_PATH);
501 schemaPaths.add(KILL_SERVER_APP_PATH);
502 schemaPaths.add(XYZZY_FOO_PATH);
503 schemaPaths.add(XYZZY_BAR_PATH);
504 schemaPaths.add(CHOICE_ACTION_PATH);
505 schemaPaths.add(DISABLE_INTERFACE_PATH);
506 schemaPaths.add(CHECK_WITH_OUTPUT_INTERFACE_PATH);
507 schemaPaths.add(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH);
509 List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
510 assertEquals(schemaPaths.size(), actions.size());
511 for (ActionDefinition actionDefinition : actions) {
512 Absolute path = actionDefinition.getPath().asAbsolute();
513 assertTrue(schemaPaths.remove(path));
518 public void toActionRequestListTopLevelTest() {
519 QName nameQname = QName.create(SERVER_QNAME, "name");
520 List<PathArgument> nodeIdentifiers = new ArrayList<>();
521 nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
522 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
523 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
525 ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
527 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
528 RESET_SERVER_PATH, domDataTreeIdentifier, data);
530 Node childAction = checkBasePartOfActionRequest(actionRequest);
532 Node childServer = childAction.getFirstChild();
533 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
535 Node childName = childServer.getFirstChild();
536 checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
538 Node childTest = childName.getFirstChild();
539 assertEquals(childTest.getNodeValue(), "test");
541 checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
545 public void toActionRequestContainerTopLevelTest() {
546 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(DEVICE_QNAME));
547 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
549 NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
550 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
551 START_DEVICE_PATH, domDataTreeIdentifier, payload);
553 Node childAction = checkBasePartOfActionRequest(actionRequest);
555 Node childDevice = childAction.getFirstChild();
556 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
558 checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
562 public void toActionRequestContainerInContainerTest() {
563 List<PathArgument> nodeIdentifiers = new ArrayList<>();
564 nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
565 nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
567 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
569 NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
570 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
571 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
573 Node childAction = checkBasePartOfActionRequest(actionRequest);
575 Node childBoxOut = childAction.getFirstChild();
576 checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
578 Node childBoxIn = childBoxOut.getFirstChild();
579 checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
581 Node action = childBoxIn.getFirstChild();
582 checkNode(action, OPEN_QNAME.getLocalName(), OPEN_QNAME.getLocalName(), OPEN_QNAME.getNamespace().toString());
586 public void toActionRequestListInContainerTest() {
587 QName nameQname = QName.create(INTERFACE_QNAME, "name");
589 List<PathArgument> nodeIdentifiers = new ArrayList<>();
590 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
591 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
592 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
594 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
596 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
597 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
598 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
600 Node childAction = checkBasePartOfActionRequest(actionRequest);
602 Node childDevice = childAction.getFirstChild();
603 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
605 Node childInterface = childDevice.getFirstChild();
606 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
608 Node childName = childInterface.getFirstChild();
609 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
611 Node childTest = childName.getFirstChild();
612 assertEquals(childTest.getNodeValue(), "test");
614 Node action = childInterface.getLastChild();
615 checkNode(action, ENABLE_QNAME.getLocalName(), ENABLE_QNAME.getLocalName(),
616 ENABLE_QNAME.getNamespace().toString());
620 public void toActionRequestListInContainerAugmentedIntoListTest() {
621 QName serverNameQname = QName.create(SERVER_QNAME, "name");
622 QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
624 List<PathArgument> nodeIdentifiers = new ArrayList<>();
625 nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
626 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
627 nodeIdentifiers.add(new AugmentationIdentifier(Collections.singleton(APPLICATIONS_QNAME)));
628 nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
629 nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
630 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
631 applicationNameQname, "testApplication"));
633 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
635 NormalizedNode<?, ?> payload = initEmptyInputAction(APPLICATION_QNAME);
636 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
637 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
639 Node childAction = checkBasePartOfActionRequest(actionRequest);
641 Node childServer = childAction.getFirstChild();
642 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
644 Node childServerName = childServer.getFirstChild();
645 checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
647 Node childServerNameTest = childServerName.getFirstChild();
648 assertEquals(childServerNameTest.getNodeValue(), "testServer");
650 Node childApplications = childServer.getLastChild();
651 checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
653 Node childApplication = childApplications.getFirstChild();
654 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
656 Node childApplicationName = childApplication.getFirstChild();
657 checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
659 Node childApplicationNameTest = childApplicationName.getFirstChild();
660 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
662 Node childKillAction = childApplication.getLastChild();
663 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
664 checkNode(childKillAction, KILL_QNAME.getLocalName(), KILL_QNAME.getLocalName(),
665 KILL_QNAME.getNamespace().toString());
669 public void toActionRequestConflictingInListTest() {
670 QName barInputQname = QName.create(BAR_QNAME, "bar");
671 QName barIdQname = QName.create(BAR_QNAME, "bar-id");
674 List<PathArgument> nodeIdentifiers = new ArrayList<>();
675 nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
676 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
678 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
680 ImmutableLeafNodeBuilder<Byte> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
681 DataContainerChild<NodeIdentifier, Byte> build = immutableLeafNodeBuilder.withNodeIdentifier(
682 NodeIdentifier.create(barInputQname)).withValue(barInput).build();
683 NormalizedNode<?, ?> payload = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
684 QName.create(barInputQname, "input"))).withChild(build).build();
686 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
687 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
689 Node childAction = checkBasePartOfActionRequest(actionRequest);
691 Node childBar = childAction.getFirstChild();
692 checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
694 Node childBarId = childBar.getFirstChild();
695 checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
697 Node childTest = childBarId.getFirstChild();
698 assertEquals(childTest.getNodeValue(), "test");
700 Node action = childBar.getLastChild();
701 checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
702 XYZZY_QNAME.getNamespace().toString());
706 public void toActionRequestConflictingInContainerTest() {
707 QName fooInputQname = QName.create(FOO_QNAME, "foo");
709 List<PathArgument> nodeIdentifiers = new ArrayList<>();
710 nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
711 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
712 NormalizedNode<?, ?> payload = initInputAction(fooInputQname, "test");
714 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
715 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
717 Node childAction = checkBasePartOfActionRequest(actionRequest);
719 Node childBar = childAction.getFirstChild();
720 checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
722 Node action = childBar.getLastChild();
723 checkNode(action, XYZZY_QNAME.getLocalName(), XYZZY_QNAME.getLocalName(),
724 XYZZY_QNAME.getNamespace().toString());
728 public void toActionRequestChoiceTest() {
729 List<PathArgument> nodeIdentifiers = new ArrayList<>();
730 nodeIdentifiers.add(NodeIdentifier.create(CONFLICT_CHOICE_QNAME));
731 nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
732 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
733 NormalizedNode<?, ?> payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
735 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
736 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
738 Node childAction = checkBasePartOfActionRequest(actionRequest);
740 Node childChoiceCont = childAction.getFirstChild();
741 checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
743 Node action = childChoiceCont.getLastChild();
744 checkNode(action, CHOICE_ACTION_QNAME.getLocalName(), CHOICE_ACTION_QNAME.getLocalName(),
745 CHOICE_ACTION_QNAME.getNamespace().toString());
749 public void toAugmentedActionRequestListInContainerTest() {
750 QName nameQname = QName.create(INTERFACE_QNAME, "name");
752 List<PathArgument> nodeIdentifiers = new ArrayList<>();
753 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
754 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
755 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
757 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
759 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
760 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
761 DISABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
763 Node childAction = checkBasePartOfActionRequest(actionRequest);
765 Node childDevice = childAction.getFirstChild();
766 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
768 Node childInterface = childDevice.getFirstChild();
769 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
771 Node childName = childInterface.getFirstChild();
772 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
774 Node childTest = childName.getFirstChild();
775 assertEquals(childTest.getNodeValue(), "test");
777 Node action = childInterface.getLastChild();
778 checkNode(action, DISABLE_QNAME.getLocalName(), DISABLE_QNAME.getLocalName(),
779 DISABLE_QNAME.getNamespace().toString());
782 @SuppressWarnings({ "rawtypes", "unchecked" })
784 public void toActionResultTest() throws Exception {
785 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
786 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
787 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
789 + "</reset-finished-at>"
791 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
792 assertNotNull(actionResult);
793 ContainerNode containerNode = actionResult.getOutput().get();
794 assertNotNull(containerNode);
795 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
796 assertEquals("now", leaf.getValue());
800 public void toActionEmptyBodyWithOutputDefinedResultTest() throws Exception {
801 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
802 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
805 DOMActionResult actionResult =
806 actionNetconfMessageTransformer.toActionResult(CHECK_WITH_OUTPUT_INTERFACE_PATH, message);
807 assertNotNull(actionResult);
808 assertTrue(actionResult.getOutput().isEmpty());
812 public void toActionEmptyBodyWithoutOutputDefinedResultTest() throws Exception {
813 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
814 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
817 DOMActionResult actionResult =
818 actionNetconfMessageTransformer.toActionResult(CHECK_WITHOUT_OUTPUT_INTERFACE_PATH, message);
819 assertNotNull(actionResult);
820 assertTrue(actionResult.getOutput().isEmpty());
824 public void getTwoNonOverlappingFieldsTest() throws IOException, SAXException {
825 // preparation of the fields
826 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
827 final YangInstanceIdentifier netconfStartTimeField = YangInstanceIdentifier.create(toId(Statistics.QNAME),
828 toId(QName.create(Statistics.QNAME, "netconf-start-time")));
829 final YangInstanceIdentifier datastoresField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
831 // building filter structure and NETCONF message
832 final DataContainerChild<?, ?> filterStructure = toFilterStructure(Collections.singletonList(FieldsFilter.of(
833 parentYiid, List.of(netconfStartTimeField, datastoresField))), SCHEMA);
834 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
835 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
838 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
840 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
841 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
843 + "<netconf-start-time/>\n"
846 + "</netconf-state>\n"
853 public void getOverlappingFieldsTest() throws IOException, SAXException {
854 // preparation of the fields
855 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
856 final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
857 final YangInstanceIdentifier capabilityField = YangInstanceIdentifier.create(toId(Capabilities.QNAME),
858 toId(QName.create(Capabilities.QNAME, "capability").intern()));
859 final YangInstanceIdentifier datastoreField = YangInstanceIdentifier.create(toId(Datastores.QNAME));
860 final YangInstanceIdentifier locksFields = YangInstanceIdentifier.create(toId(Datastores.QNAME),
861 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME));
863 // building filter structure and NETCONF message
864 final DataContainerChild<?, ?> filterStructure = toFilterStructure(Collections.singletonList(FieldsFilter.of(
865 parentYiid, List.of(capabilitiesField, capabilityField, datastoreField, locksFields))), SCHEMA);
866 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
867 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
870 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
872 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
873 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
874 + "<capabilities/>\n"
876 + "</netconf-state>\n"
883 public void getOverlappingFieldsWithEmptyFieldTest() throws IOException, SAXException {
884 // preparation of the fields
885 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
886 final YangInstanceIdentifier capabilitiesField = YangInstanceIdentifier.create(toId(Capabilities.QNAME));
888 // building filter structure and NETCONF message
889 final DataContainerChild<?, ?> filterStructure = toFilterStructure(Collections.singletonList(FieldsFilter.of(
890 parentYiid, List.of(capabilitiesField, YangInstanceIdentifier.empty()))), SCHEMA);
891 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
892 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
895 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
897 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
898 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"/>\n"
905 public void getSpecificFieldsUnderListTest() throws IOException, SAXException {
906 // preparation of the fields
907 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
908 toId(Schemas.QNAME), toId(Schema.QNAME), NodeIdentifierWithPredicates.of(Schema.QNAME));
909 final YangInstanceIdentifier versionField = YangInstanceIdentifier.create(
910 toId(QName.create(Schema.QNAME, "version").intern()));
911 final YangInstanceIdentifier identifierField = YangInstanceIdentifier.create(
912 toId(QName.create(Schema.QNAME, "namespace").intern()));
914 // building filter structure and NETCONF message
915 final DataContainerChild<?, ?> filterStructure = toFilterStructure(Collections.singletonList(FieldsFilter.of(
916 parentYiid, List.of(versionField, identifierField))), SCHEMA);
917 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
918 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
921 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
923 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
924 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
929 // explicitly fetched list keys - identifier and format
934 + "</netconf-state>\n"
941 public void getSpecificFieldsUnderMultipleLists() throws IOException, SAXException {
942 // preparation of the fields
943 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(
944 toId(NetconfState.QNAME), toId(Datastores.QNAME));
945 final YangInstanceIdentifier partialLockYiid = YangInstanceIdentifier.create(toId(Datastore.QNAME),
946 NodeIdentifierWithPredicates.of(Datastore.QNAME), toId(Locks.QNAME),
947 toId(QName.create(Locks.QNAME, "lock-type").intern()), toId(PartialLock.QNAME),
948 NodeIdentifierWithPredicates.of(PartialLock.QNAME));
949 final YangInstanceIdentifier lockedTimeField = partialLockYiid.node(
950 QName.create(Locks.QNAME, "locked-time").intern());
951 final YangInstanceIdentifier lockedBySessionField = partialLockYiid.node(
952 QName.create(Locks.QNAME, "locked-by-session").intern());
954 // building filter structure and NETCONF message
955 final DataContainerChild<?, ?> filterStructure = toFilterStructure(
956 List.of(FieldsFilter.of(parentYiid, List.of(lockedTimeField, lockedBySessionField))),
958 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
959 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
962 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
964 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
965 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
971 + "<locked-by-session/>\n"
973 + "</partial-lock>\n"
978 + "</netconf-state>\n"
985 public void getWholeListsUsingFieldsTest() throws IOException, SAXException {
986 // preparation of the fields
987 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME));
988 final YangInstanceIdentifier datastoreListField = YangInstanceIdentifier.create(toId(Datastores.QNAME),
989 toId(Datastore.QNAME), NodeIdentifierWithPredicates.of(Datastore.QNAME));
990 final YangInstanceIdentifier sessionListField = YangInstanceIdentifier.create(toId(Sessions.QNAME),
991 toId(Session.QNAME), NodeIdentifierWithPredicates.of(Session.QNAME));
993 // building filter structure and NETCONF message
994 final DataContainerChild<?, ?> filterStructure = toFilterStructure(Collections.singletonList(FieldsFilter.of(
995 parentYiid, List.of(datastoreListField, sessionListField))), SCHEMA);
996 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
997 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1000 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1002 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1003 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1010 + "</netconf-state>\n"
1017 public void getSpecificListEntriesWithSpecificFieldsTest() throws IOException, SAXException {
1018 // preparation of the fields
1019 final YangInstanceIdentifier parentYiid = YangInstanceIdentifier.create(toId(NetconfState.QNAME),
1020 toId(Sessions.QNAME));
1021 final QName sessionId = QName.create(Session.QNAME, "session-id").intern();
1022 final YangInstanceIdentifier session1Field = YangInstanceIdentifier.create(toId(Session.QNAME),
1023 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 1));
1024 final YangInstanceIdentifier session2TransportField = YangInstanceIdentifier.create(toId(Session.QNAME),
1025 NodeIdentifierWithPredicates.of(Session.QNAME, sessionId, 2),
1026 toId(QName.create(Session.QNAME, "transport").intern()));
1028 // building filter structure and NETCONF message
1029 final DataContainerChild<?, ?> filterStructure = toFilterStructure(Collections.singletonList(FieldsFilter.of(
1030 parentYiid, List.of(session1Field, session2TransportField))), SCHEMA);
1031 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(NETCONF_GET_QNAME,
1032 NetconfMessageTransformUtil.wrap(toId(NETCONF_GET_QNAME), filterStructure));
1035 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
1037 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
1038 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
1041 + "<session-id>1</session-id>\n"
1044 + "<session-id>2</session-id>\n"
1048 + "</netconf-state>\n"
1054 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
1055 final String inputNodeName, final String inputValue) {
1056 checkNode(action, actionQname.getLocalName(), actionQname.getLocalName(),
1057 actionQname.getNamespace().toString());
1059 Node childResetAt = action.getFirstChild();
1060 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
1062 Node firstChild = childResetAt.getFirstChild();
1063 assertEquals(firstChild.getNodeValue(), inputValue);
1066 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
1067 Node baseRpc = actionRequest.getDocument().getFirstChild();
1068 checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
1069 assertTrue(baseRpc.getLocalName().equals("rpc"));
1070 assertTrue(baseRpc.getNodeName().equals("rpc"));
1072 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
1073 assertNotNull(messageId);
1074 assertTrue(messageId.getNodeValue().contains("m-"));
1075 Node childAction = baseRpc.getFirstChild();
1077 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
1081 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
1082 YangInstanceIdentifier yangInstanceIdentifier =
1083 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
1084 DOMDataTreeIdentifier domDataTreeIdentifier =
1085 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
1086 yangInstanceIdentifier);
1087 return domDataTreeIdentifier;
1090 private static ContainerNode initInputAction(final QName qname, final String value) {
1091 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
1092 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
1093 NodeIdentifier.create(qname)).withValue(value).build();
1094 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
1095 QName.create(qname, "input"))).withChild(build).build();
1099 private static ContainerNode initEmptyInputAction(final QName qname) {
1100 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
1101 QName.create(qname, "input"))).build();
1104 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
1105 final String expectedNamespace) {
1106 assertNotNull(childServer);
1107 assertEquals(childServer.getLocalName(), expectedLocalName);
1108 assertEquals(childServer.getNodeName(), expectedNodeName);
1109 assertEquals(childServer.getNamespaceURI(), expectedNamespace);