2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.sal.connect.netconf.schema.mapping;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
30 import static org.opendaylight.netconf.util.NetconfUtil.NETCONF_DATA_QNAME;
32 import com.google.common.collect.Iterables;
33 import com.google.common.collect.Lists;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.List;
41 import java.util.Optional;
43 import javax.xml.transform.dom.DOMSource;
44 import org.custommonkey.xmlunit.Diff;
45 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
46 import org.custommonkey.xmlunit.XMLUnit;
47 import org.hamcrest.CoreMatchers;
48 import org.junit.AfterClass;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.Ignore;
52 import org.junit.Test;
53 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
54 import org.opendaylight.mdsal.dom.api.DOMActionResult;
55 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
56 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
57 import org.opendaylight.netconf.api.NetconfMessage;
58 import org.opendaylight.netconf.api.xml.XmlUtil;
59 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
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.$YangModuleInfoImpl;
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.Schemas;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
68 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
69 import org.opendaylight.yangtools.yang.common.QName;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
76 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
79 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
82 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
83 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
84 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
86 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
87 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
88 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
89 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
90 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
91 import org.w3c.dom.Node;
92 import org.xml.sax.SAXException;
94 public class NetconfMessageTransformerTest {
96 private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
97 private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
99 private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
100 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
102 private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
104 private static final QName SERVER_QNAME =
105 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
106 private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
107 private static final SchemaPath RESET_SERVER_PATH = SchemaPath.create(true, SERVER_QNAME, RESET_QNAME);
108 private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
109 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
110 private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
111 private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
112 private static final SchemaPath KILL_SERVER_APP_PATH =
113 SchemaPath.create(true, SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
115 private static final QName DEVICE_QNAME =
116 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
117 private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
118 private static final SchemaPath START_DEVICE_PATH = SchemaPath.create(true, DEVICE_QNAME, START_QNAME);
119 private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
120 private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
121 private static final SchemaPath ENABLE_INTERFACE_PATH =
122 SchemaPath.create(true, DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
124 private static final QName BOX_OUT_QNAME =
125 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
126 private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
127 private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
128 private static final SchemaPath OPEN_BOXES_PATH =
129 SchemaPath.create(true, BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
131 private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
132 private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
133 private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
134 private static final SchemaPath XYZZY_FOO_PATH = SchemaPath.create(true, FOO_QNAME, XYZZY_QNAME);
135 private static final SchemaPath XYZZY_BAR_PATH = SchemaPath.create(true, BAR_QNAME, XYZZY_QNAME);
137 private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
138 private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
139 private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
140 private static final SchemaPath CHOICE_ACTION_PATH =
141 SchemaPath.create(true, CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
143 private static SchemaContext PARTIAL_SCHEMA;
144 private static SchemaContext SCHEMA;
145 private static SchemaContext ACTION_SCHEMA;
147 private NetconfMessageTransformer actionNetconfMessageTransformer;
148 private NetconfMessageTransformer netconfMessageTransformer;
151 public static void beforeClass() {
152 final ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
153 context.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
154 .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
155 PARTIAL_SCHEMA = context.tryToCreateSchemaContext().get();
157 context.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
158 SCHEMA = context.tryToCreateSchemaContext().get();
160 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
161 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
162 "/schemas/conflicting-actions.yang");
166 public static void afterClass() {
167 PARTIAL_SCHEMA = null;
169 ACTION_SCHEMA = null;
173 public void setUp() throws Exception {
174 XMLUnit.setIgnoreWhitespace(true);
175 XMLUnit.setIgnoreAttributeOrder(true);
176 XMLUnit.setIgnoreComments(true);
178 netconfMessageTransformer = getTransformer(SCHEMA);
179 actionNetconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(ACTION_SCHEMA),
184 public void testLockRequestBaseSchemaNotPresent() throws Exception {
185 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
186 final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
187 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
189 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
190 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
194 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
195 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(new EmptyMountPointContext(SCHEMA),
196 true, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS);
197 NetconfMessage netconfMessage = transformer.toRpcRequest(
198 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
199 CREATE_SUBSCRIPTION_RPC_CONTENT
201 String documentString = XmlUtil.toString(netconfMessage.getDocument());
202 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
203 assertThat(documentString, CoreMatchers.containsString("<rpc"));
207 public void tesLockSchemaRequest() throws Exception {
208 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
209 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
211 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
215 public void testDiscardChangesRequest() throws Exception {
216 final NetconfMessage netconfMessage =
217 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
218 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
219 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
220 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
224 public void testGetSchemaRequest() throws Exception {
225 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
226 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
227 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
228 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
229 + "<format>yang</format>\n"
230 + "<identifier>module</identifier>\n"
231 + "<version>2012-12-12</version>\n"
237 public void testGetSchemaResponse() throws Exception {
238 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
239 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
240 "<rpc-reply message-id=\"101\"\n"
241 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
243 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
244 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
245 + "Random YANG SCHEMA\n"
250 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
251 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
252 assertNotNull(compositeNodeRpcResult.getResult());
253 final DOMSource schemaContent =
254 ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
255 assertThat(schemaContent.getNode().getTextContent(),
256 CoreMatchers.containsString("Random YANG SCHEMA"));
260 public void testGetConfigResponse() throws Exception {
261 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
262 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
264 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
267 + "<identifier>module</identifier>\n"
268 + "<version>2012-12-12</version>\n"
269 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
272 + "</netconf-state>\n"
276 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
277 final DOMRpcResult compositeNodeRpcResult =
278 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
279 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
280 assertNotNull(compositeNodeRpcResult.getResult());
282 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
283 NetconfRemoteSchemaYangSourceProvider
284 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
286 final Map<QName, Object> keys = new HashMap<>();
287 for (final DataContainerChild<?, ?> value : values) {
288 keys.put(value.getNodeType(), value.getValue());
291 final NodeIdentifierWithPredicates identifierWithPredicates =
292 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
293 final MapEntryNode schemaNode =
294 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
296 final AnyXmlNode data = (AnyXmlNode) ((ContainerNode) compositeNodeRpcResult
297 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
299 NormalizedNodeResult nodeResult =
300 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
301 ContainerNode result = (ContainerNode) nodeResult.getResult();
302 final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
303 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
304 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
305 assertEquals(1, Iterables.size(schemaParent.getValue()));
307 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
311 public void testGetConfigLeafRequest() throws Exception {
312 final DataContainerChild<?, ?> filter = toFilterStructure(
313 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
314 NodeIdentifierWithPredicates.of(Schema.QNAME),
315 toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
317 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
319 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
320 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
322 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
323 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
324 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
325 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
331 + "</netconf-state>\n"
341 public void testGetConfigRequest() throws Exception {
342 final DataContainerChild<?, ?> filter = toFilterStructure(
343 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
345 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
347 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
348 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
350 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
351 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
352 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
353 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
365 public void testEditConfigRequest() throws Exception {
366 final List<DataContainerChild<?, ?>> values = Lists.newArrayList(
367 NetconfRemoteSchemaYangSourceProvider
368 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
370 final Map<QName, Object> keys = new HashMap<>();
371 for (final DataContainerChild<?, ?> value : values) {
372 keys.put(value.getNodeType(), value.getValue());
375 final NodeIdentifierWithPredicates identifierWithPredicates =
376 NodeIdentifierWithPredicates.of(Schema.QNAME, keys);
377 final MapEntryNode schemaNode =
378 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
380 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
381 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
382 .nodeWithKey(Schema.QNAME, keys).build();
383 final DataContainerChild<?, ?> editConfigStructure =
384 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
385 Optional.empty(), Optional.ofNullable(schemaNode));
387 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
389 final ContainerNode wrap =
390 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
391 final NetconfMessage netconfMessage =
392 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
394 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
400 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
403 + "<identifier>module</identifier>\n"
404 + "<version>2012-12-12</version>\n"
405 + "<format>yang</format>\n"
408 + "</netconf-state>\n"
414 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
415 throws SAXException, IOException {
416 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
417 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
418 assertTrue(diff.toString(), diff.similar());
422 public void testGetRequest() throws Exception {
424 final QName capability = QName.create(Capabilities.QNAME, "capability");
425 final DataContainerChild<?, ?> filter = toFilterStructure(
426 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
427 new NodeWithValue<>(capability, "a:b:c")), SCHEMA);
429 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
430 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
432 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
433 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
434 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
435 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
437 + "<capability>a:b:c</capability>\n"
438 + "</capabilities>\n"
445 private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
446 return new NetconfMessageTransformer(new EmptyMountPointContext(schema), true);
450 public void testCommitResponse() throws Exception {
451 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
452 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
454 final DOMRpcResult compositeNodeRpcResult =
455 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
456 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
457 assertNull(compositeNodeRpcResult.getResult());
461 public void getActionsTest() {
462 Set<SchemaPath> schemaPaths = new HashSet<>();
463 schemaPaths.add(RESET_SERVER_PATH);
464 schemaPaths.add(START_DEVICE_PATH);
465 schemaPaths.add(ENABLE_INTERFACE_PATH);
466 schemaPaths.add(OPEN_BOXES_PATH);
467 schemaPaths.add(KILL_SERVER_APP_PATH);
468 schemaPaths.add(XYZZY_FOO_PATH);
469 schemaPaths.add(XYZZY_BAR_PATH);
470 schemaPaths.add(CHOICE_ACTION_PATH);
472 List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
473 assertEquals(schemaPaths.size(), actions.size());
474 for (ActionDefinition actionDefinition : actions) {
475 SchemaPath path = actionDefinition.getPath();
476 assertTrue(schemaPaths.remove(path));
481 public void toActionRequestListTopLevelTest() {
482 QName nameQname = QName.create(SERVER_QNAME, "name");
483 List<PathArgument> nodeIdentifiers = new ArrayList<>();
484 nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
485 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, nameQname, "test"));
486 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
488 ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
490 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
491 RESET_SERVER_PATH, domDataTreeIdentifier, data);
493 Node childAction = checkBasePartOfActionRequest(actionRequest);
495 Node childServer = childAction.getFirstChild();
496 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
498 Node childName = childServer.getFirstChild();
499 checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
501 Node childTest = childName.getFirstChild();
502 assertEquals(childTest.getNodeValue(), "test");
504 checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
508 public void toActionRequestContainerTopLevelTest() {
509 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(DEVICE_QNAME));
510 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
512 NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
513 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
514 START_DEVICE_PATH, domDataTreeIdentifier, payload);
516 Node childAction = checkBasePartOfActionRequest(actionRequest);
518 Node childDevice = childAction.getFirstChild();
519 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
521 checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
525 public void toActionRequestContainerInContainerTest() {
526 List<PathArgument> nodeIdentifiers = new ArrayList<>();
527 nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
528 nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
530 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
532 NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
533 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
534 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
536 Node childAction = checkBasePartOfActionRequest(actionRequest);
538 Node childBoxOut = childAction.getFirstChild();
539 checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
541 Node childBoxIn = childBoxOut.getFirstChild();
542 checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
544 Node action = childBoxIn.getFirstChild();
545 checkNode(action, null, OPEN_QNAME.getLocalName(), null);
549 public void toActionRequestListInContainerTest() {
550 QName nameQname = QName.create(INTERFACE_QNAME, "name");
552 List<PathArgument> nodeIdentifiers = new ArrayList<>();
553 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
554 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
555 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(INTERFACE_QNAME, nameQname, "test"));
557 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
559 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
560 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
561 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
563 Node childAction = checkBasePartOfActionRequest(actionRequest);
565 Node childDevice = childAction.getFirstChild();
566 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
568 Node childInterface = childDevice.getFirstChild();
569 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
571 Node childName = childInterface.getFirstChild();
572 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
574 Node childTest = childName.getFirstChild();
575 assertEquals(childTest.getNodeValue(), "test");
577 Node action = childInterface.getLastChild();
578 checkNode(action, null, ENABLE_QNAME.getLocalName(), null);
582 public void toActionRequestListInContainerAugmentedIntoListTest() {
583 QName serverNameQname = QName.create(SERVER_QNAME, "name");
584 QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
586 List<PathArgument> nodeIdentifiers = new ArrayList<>();
587 nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
588 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(SERVER_QNAME, serverNameQname, "testServer"));
589 nodeIdentifiers.add(new AugmentationIdentifier(Collections.singleton(APPLICATIONS_QNAME)));
590 nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
591 nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
592 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(APPLICATION_QNAME,
593 applicationNameQname, "testApplication"));
595 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
597 NormalizedNode<?, ?> payload = initEmptyInputAction(APPLICATION_QNAME);
598 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
599 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
601 Node childAction = checkBasePartOfActionRequest(actionRequest);
603 Node childServer = childAction.getFirstChild();
604 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
606 Node childServerName = childServer.getFirstChild();
607 checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
609 Node childServerNameTest = childServerName.getFirstChild();
610 assertEquals(childServerNameTest.getNodeValue(), "testServer");
612 Node childApplications = childServer.getLastChild();
613 checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
615 Node childApplication = childApplications.getFirstChild();
616 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
618 Node childApplicationName = childApplication.getFirstChild();
619 checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
621 Node childApplicationNameTest = childApplicationName.getFirstChild();
622 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
624 Node childKillAction = childApplication.getLastChild();
625 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
626 checkNode(childKillAction, null, KILL_QNAME.getLocalName(), null);
630 public void toActionRequestConflictingInListTest() {
631 QName barInputQname = QName.create(BAR_QNAME, "bar");
632 QName barIdQname = QName.create(BAR_QNAME, "bar-id");
633 Byte barInput = new Byte("1");
635 List<PathArgument> nodeIdentifiers = new ArrayList<>();
636 nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
637 nodeIdentifiers.add(NodeIdentifierWithPredicates.of(BAR_QNAME, barIdQname, "test"));
639 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
641 ImmutableLeafNodeBuilder<Byte> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
642 DataContainerChild<NodeIdentifier, Byte> build = immutableLeafNodeBuilder.withNodeIdentifier(
643 NodeIdentifier.create(barInputQname)).withValue(barInput).build();
644 NormalizedNode<?, ?> payload = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
645 QName.create(barInputQname, "input"))).withChild(build).build();
647 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
648 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
650 Node childAction = checkBasePartOfActionRequest(actionRequest);
652 Node childBar = childAction.getFirstChild();
653 checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
655 Node childBarId = childBar.getFirstChild();
656 checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
658 Node childTest = childBarId.getFirstChild();
659 assertEquals(childTest.getNodeValue(), "test");
661 Node action = childBar.getLastChild();
662 checkNode(action, null, XYZZY_QNAME.getLocalName(), null);
666 public void toActionRequestConflictingInContainerTest() {
667 QName fooInputQname = QName.create(FOO_QNAME, "foo");
669 List<PathArgument> nodeIdentifiers = new ArrayList<>();
670 nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
671 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
672 NormalizedNode<?, ?> payload = initInputAction(fooInputQname, "test");
674 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
675 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
677 Node childAction = checkBasePartOfActionRequest(actionRequest);
679 Node childBar = childAction.getFirstChild();
680 checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
682 Node action = childBar.getLastChild();
683 checkNode(action, null, XYZZY_QNAME.getLocalName(), null);
688 public void toActionRequestChoiceTest() {
689 List<PathArgument> nodeIdentifiers = new ArrayList<>();
690 nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
691 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
692 NormalizedNode<?, ?> payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
694 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
695 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
697 Node childAction = checkBasePartOfActionRequest(actionRequest);
699 Node childChoiceCont = childAction.getFirstChild();
700 checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
702 Node action = childChoiceCont.getLastChild();
703 checkNode(action, null, CHOICE_ACTION_QNAME.getLocalName(), null);
706 @SuppressWarnings({ "rawtypes", "unchecked" })
708 public void toActionResultTest() throws Exception {
709 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
710 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
711 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
713 + "</reset-finished-at>"
715 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
716 assertNotNull(actionResult);
717 ContainerNode containerNode = actionResult.getOutput().get();
718 assertNotNull(containerNode);
719 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
720 assertEquals("now", leaf.getValue());
723 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
724 final String inputNodeName, final String inputValue) {
725 checkNode(action, null, actionQname.getLocalName(), null);
727 Node childResetAt = action.getFirstChild();
728 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
730 Node firstChild = childResetAt.getFirstChild();
731 assertEquals(firstChild.getNodeValue(), inputValue);
734 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
735 Node baseRpc = actionRequest.getDocument().getFirstChild();
736 checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
737 assertTrue(baseRpc.getLocalName().equals("rpc"));
738 assertTrue(baseRpc.getNodeName().equals("rpc"));
740 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
741 assertNotNull(messageId);
742 assertTrue(messageId.getNodeValue().contains("m-"));
744 Node childAction = baseRpc.getFirstChild();
745 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
749 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
750 YangInstanceIdentifier yangInstanceIdentifier =
751 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
752 DOMDataTreeIdentifier domDataTreeIdentifier =
753 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
754 yangInstanceIdentifier);
755 return domDataTreeIdentifier;
758 private static ContainerNode initInputAction(final QName qname, final String value) {
759 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
760 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
761 NodeIdentifier.create(qname)).withValue(value).build();
762 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
763 QName.create(qname, "input"))).withChild(build).build();
767 private static ContainerNode initEmptyInputAction(final QName qname) {
768 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
769 QName.create(qname, "input"))).build();
772 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
773 final String expectedNamespace) {
774 assertNotNull(childServer);
775 assertEquals(childServer.getLocalName(), expectedLocalName);
776 assertEquals(childServer.getNodeName(), expectedNodeName);
777 assertEquals(childServer.getNamespaceURI(), expectedNamespace);