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.ImmutableMap;
33 import com.google.common.collect.Iterables;
34 import com.google.common.collect.Lists;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
42 import java.util.Optional;
44 import javax.xml.transform.dom.DOMSource;
45 import org.custommonkey.xmlunit.Diff;
46 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
47 import org.custommonkey.xmlunit.XMLUnit;
48 import org.hamcrest.CoreMatchers;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Ignore;
53 import org.junit.Test;
54 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
55 import org.opendaylight.mdsal.dom.api.DOMActionResult;
56 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
57 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
58 import org.opendaylight.netconf.api.NetconfMessage;
59 import org.opendaylight.netconf.api.xml.XmlUtil;
60 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
61 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
62 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
63 import org.opendaylight.netconf.util.NetconfUtil;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
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.schemas.Schema;
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.NodeIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
74 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
77 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
81 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
82 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
83 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
84 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
85 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
86 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
87 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
88 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
89 import org.w3c.dom.Node;
90 import org.xml.sax.SAXException;
92 public class NetconfMessageTransformerTest {
94 private static final String REVISION_EXAMPLE_SERVER_FARM = "2018-08-07";
95 private static final String URN_EXAMPLE_SERVER_FARM = "urn:example:server-farm";
97 private static final String REVISION_EXAMPLE_SERVER_FARM_2 = "2019-05-20";
98 private static final String URN_EXAMPLE_SERVER_FARM_2 = "urn:example:server-farm-2";
100 private static final String URN_EXAMPLE_CONFLICT = "urn:example:conflict";
102 private static final QName SERVER_QNAME =
103 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "server");
104 private static final QName RESET_QNAME = QName.create(SERVER_QNAME, "reset");
105 private static final SchemaPath RESET_SERVER_PATH = SchemaPath.create(true, SERVER_QNAME, RESET_QNAME);
106 private static final QName APPLICATIONS_QNAME = QName.create(URN_EXAMPLE_SERVER_FARM_2,
107 REVISION_EXAMPLE_SERVER_FARM_2, "applications");
108 private static final QName APPLICATION_QNAME = QName.create(APPLICATIONS_QNAME, "application");
109 private static final QName KILL_QNAME = QName.create(APPLICATION_QNAME, "kill");
110 private static final SchemaPath KILL_SERVER_APP_PATH =
111 SchemaPath.create(true, SERVER_QNAME, APPLICATIONS_QNAME, APPLICATION_QNAME, KILL_QNAME);
113 private static final QName DEVICE_QNAME =
114 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "device");
115 private static final QName START_QNAME = QName.create(DEVICE_QNAME, "start");
116 private static final SchemaPath START_DEVICE_PATH = SchemaPath.create(true, DEVICE_QNAME, START_QNAME);
117 private static final QName INTERFACE_QNAME = QName.create(DEVICE_QNAME, "interface");
118 private static final QName ENABLE_QNAME = QName.create(INTERFACE_QNAME, "enable");
119 private static final SchemaPath ENABLE_INTERFACE_PATH =
120 SchemaPath.create(true, DEVICE_QNAME, INTERFACE_QNAME, ENABLE_QNAME);
122 private static final QName BOX_OUT_QNAME =
123 QName.create(URN_EXAMPLE_SERVER_FARM, REVISION_EXAMPLE_SERVER_FARM, "box-out");
124 private static final QName BOX_IN_QNAME = QName.create(BOX_OUT_QNAME, "box-in");
125 private static final QName OPEN_QNAME = QName.create(BOX_IN_QNAME, "open");
126 private static final SchemaPath OPEN_BOXES_PATH =
127 SchemaPath.create(true, BOX_OUT_QNAME, BOX_IN_QNAME, OPEN_QNAME);
129 private static final QName FOO_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "foo");
130 private static final QName BAR_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "bar");
131 private static final QName XYZZY_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "xyzzy");
132 private static final SchemaPath XYZZY_FOO_PATH = SchemaPath.create(true, FOO_QNAME, XYZZY_QNAME);
133 private static final SchemaPath XYZZY_BAR_PATH = SchemaPath.create(true, BAR_QNAME, XYZZY_QNAME);
135 private static final QName CONFLICT_CHOICE_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "conflict-choice");
136 private static final QName CHOICE_CONT_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-cont");
137 private static final QName CHOICE_ACTION_QNAME = QName.create(URN_EXAMPLE_CONFLICT, "choice-action");
138 private static final SchemaPath CHOICE_ACTION_PATH =
139 SchemaPath.create(true, CONFLICT_CHOICE_QNAME, CHOICE_CONT_QNAME, CHOICE_CONT_QNAME, CHOICE_ACTION_QNAME);
141 private static SchemaContext PARTIAL_SCHEMA;
142 private static SchemaContext SCHEMA;
143 private static SchemaContext ACTION_SCHEMA;
145 private NetconfMessageTransformer actionNetconfMessageTransformer;
146 private NetconfMessageTransformer netconfMessageTransformer;
149 public static void beforeClass() {
150 final ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
151 context.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
152 .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
153 PARTIAL_SCHEMA = context.tryToCreateSchemaContext().get();
155 context.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
156 SCHEMA = context.tryToCreateSchemaContext().get();
158 ACTION_SCHEMA = YangParserTestUtils.parseYangResources(NetconfMessageTransformerTest.class,
159 "/schemas/example-server-farm.yang","/schemas/example-server-farm-2.yang",
160 "/schemas/conflicting-actions.yang");
164 public static void afterClass() {
165 PARTIAL_SCHEMA = null;
167 ACTION_SCHEMA = null;
171 public void setUp() throws Exception {
172 XMLUnit.setIgnoreWhitespace(true);
173 XMLUnit.setIgnoreAttributeOrder(true);
174 XMLUnit.setIgnoreComments(true);
176 netconfMessageTransformer = getTransformer(SCHEMA);
177 actionNetconfMessageTransformer = new NetconfMessageTransformer(ACTION_SCHEMA, true);
181 public void testLockRequestBaseSchemaNotPresent() throws Exception {
182 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
183 final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
184 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
186 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
187 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
191 public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
192 final NetconfMessageTransformer transformer = new NetconfMessageTransformer(SCHEMA, true,
193 BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS);
194 NetconfMessage netconfMessage = transformer.toRpcRequest(
195 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
196 CREATE_SUBSCRIPTION_RPC_CONTENT
198 String documentString = XmlUtil.toString(netconfMessage.getDocument());
199 assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
200 assertThat(documentString, CoreMatchers.containsString("<rpc"));
204 public void tesLockSchemaRequest() throws Exception {
205 final NetconfMessageTransformer transformer = getTransformer(PARTIAL_SCHEMA);
206 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
208 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
212 public void testDiscardChangesRequest() throws Exception {
213 final NetconfMessage netconfMessage =
214 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
215 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
216 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
217 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
221 public void testGetSchemaRequest() throws Exception {
222 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
223 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
224 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
225 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
226 + "<format>yang</format>\n"
227 + "<identifier>module</identifier>\n"
228 + "<version>2012-12-12</version>\n"
234 public void testGetSchemaResponse() throws Exception {
235 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
236 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
237 "<rpc-reply message-id=\"101\"\n"
238 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
240 + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
241 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
242 + "Random YANG SCHEMA\n"
247 final DOMRpcResult compositeNodeRpcResult = transformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
248 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
249 assertNotNull(compositeNodeRpcResult.getResult());
250 final DOMSource schemaContent =
251 ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
252 assertThat(schemaContent.getNode().getTextContent(),
253 CoreMatchers.containsString("Random YANG SCHEMA"));
257 public void testGetConfigResponse() throws Exception {
258 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
259 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
261 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
264 + "<identifier>module</identifier>\n"
265 + "<version>2012-12-12</version>\n"
266 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
269 + "</netconf-state>\n"
273 final NetconfMessageTransformer transformer = getTransformer(SCHEMA);
274 final DOMRpcResult compositeNodeRpcResult =
275 transformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
276 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
277 assertNotNull(compositeNodeRpcResult.getResult());
279 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
280 NetconfRemoteSchemaYangSourceProvider
281 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
283 final Map<QName, Object> keys = new HashMap<>();
284 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
285 keys.put(value.getNodeType(), value.getValue());
288 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
289 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
290 final MapEntryNode schemaNode =
291 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
293 final AnyXmlNode data = (AnyXmlNode) ((ContainerNode) compositeNodeRpcResult
294 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
296 NormalizedNodeResult nodeResult =
297 NetconfUtil.transformDOMSourceToNormalizedNode(SCHEMA, data.getValue());
298 ContainerNode result = (ContainerNode) nodeResult.getResult();
299 final ContainerNode state = (ContainerNode) result.getChild(toId(NetconfState.QNAME)).get();
300 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
301 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
302 assertEquals(1, Iterables.size(schemaParent.getValue()));
304 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
308 public void testGetConfigLeafRequest() throws Exception {
309 final DataContainerChild<?, ?> filter = toFilterStructure(
310 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME), toId(Schema.QNAME),
311 new NodeIdentifierWithPredicates(Schema.QNAME, ImmutableMap.of()),
312 toId(QName.create(Schemas.QNAME, "version"))), SCHEMA);
314 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
316 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
317 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
319 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
320 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
321 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
322 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
328 + "</netconf-state>\n"
338 public void testGetConfigRequest() throws Exception {
339 final DataContainerChild<?, ?> filter = toFilterStructure(
340 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), SCHEMA);
342 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
344 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
345 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
347 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
348 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
349 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
350 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
362 public void testEditConfigRequest() throws Exception {
363 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
364 NetconfRemoteSchemaYangSourceProvider
365 .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
367 final Map<QName, Object> keys = new HashMap<>();
368 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
369 keys.put(value.getNodeType(), value.getValue());
372 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
373 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
374 final MapEntryNode schemaNode =
375 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
377 final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
378 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
379 .nodeWithKey(Schema.QNAME, keys).build();
380 final DataContainerChild<?, ?> editConfigStructure =
381 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
382 Optional.empty(), Optional.ofNullable(schemaNode));
384 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
386 final ContainerNode wrap =
387 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
388 final NetconfMessage netconfMessage =
389 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
391 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
397 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
400 + "<identifier>module</identifier>\n"
401 + "<version>2012-12-12</version>\n"
402 + "<format>yang</format>\n"
405 + "</netconf-state>\n"
411 private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
412 throws SAXException, IOException {
413 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
414 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
415 assertTrue(diff.toString(), diff.similar());
419 public void testGetRequest() throws Exception {
421 final QName capability = QName.create(Capabilities.QNAME, "capability");
422 final DataContainerChild<?, ?> filter = toFilterStructure(
423 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
424 new YangInstanceIdentifier.NodeWithValue<>(capability, "a:b:c")), SCHEMA);
426 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
427 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
429 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
430 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
431 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
432 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
434 + "<capability>a:b:c</capability>\n"
435 + "</capabilities>\n"
442 private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
443 return new NetconfMessageTransformer(schema, true);
447 public void testCommitResponse() throws Exception {
448 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
449 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
451 final DOMRpcResult compositeNodeRpcResult =
452 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
453 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
454 assertNull(compositeNodeRpcResult.getResult());
458 public void getActionsTest() {
459 Set<SchemaPath> schemaPaths = new HashSet<>();
460 schemaPaths.add(RESET_SERVER_PATH);
461 schemaPaths.add(START_DEVICE_PATH);
462 schemaPaths.add(ENABLE_INTERFACE_PATH);
463 schemaPaths.add(OPEN_BOXES_PATH);
464 schemaPaths.add(KILL_SERVER_APP_PATH);
465 schemaPaths.add(XYZZY_FOO_PATH);
466 schemaPaths.add(XYZZY_BAR_PATH);
467 schemaPaths.add(CHOICE_ACTION_PATH);
469 List<ActionDefinition> actions = NetconfMessageTransformer.getActions(ACTION_SCHEMA);
470 assertEquals(schemaPaths.size(), actions.size());
471 for (ActionDefinition actionDefinition : actions) {
472 SchemaPath path = actionDefinition.getPath();
473 assertTrue(schemaPaths.remove(path));
478 public void toActionRequestListTopLevelTest() {
479 QName nameQname = QName.create(SERVER_QNAME, "name");
480 List<PathArgument> nodeIdentifiers = new ArrayList<>();
481 nodeIdentifiers.add(new NodeIdentifier(SERVER_QNAME));
482 nodeIdentifiers.add(new NodeIdentifierWithPredicates(SERVER_QNAME, nameQname, "test"));
483 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
485 ContainerNode data = initInputAction(QName.create(SERVER_QNAME, "reset-at"), "now");
487 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
488 RESET_SERVER_PATH, domDataTreeIdentifier, data);
490 Node childAction = checkBasePartOfActionRequest(actionRequest);
492 Node childServer = childAction.getFirstChild();
493 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
495 Node childName = childServer.getFirstChild();
496 checkNode(childName, "name", "name", URN_EXAMPLE_SERVER_FARM);
498 Node childTest = childName.getFirstChild();
499 assertEquals(childTest.getNodeValue(), "test");
501 checkAction(RESET_QNAME, childName.getNextSibling(), "reset-at", "reset-at", "now");
505 public void toActionRequestContainerTopLevelTest() {
506 List<PathArgument> nodeIdentifiers = Collections.singletonList(NodeIdentifier.create(DEVICE_QNAME));
507 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
509 NormalizedNode<?, ?> payload = initInputAction(QName.create(DEVICE_QNAME, "start-at"), "now");
510 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
511 START_DEVICE_PATH, domDataTreeIdentifier, payload);
513 Node childAction = checkBasePartOfActionRequest(actionRequest);
515 Node childDevice = childAction.getFirstChild();
516 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
518 checkAction(START_QNAME, childDevice.getFirstChild(), "start-at", "start-at", "now");
522 public void toActionRequestContainerInContainerTest() {
523 List<PathArgument> nodeIdentifiers = new ArrayList<>();
524 nodeIdentifiers.add(NodeIdentifier.create(BOX_OUT_QNAME));
525 nodeIdentifiers.add(NodeIdentifier.create(BOX_IN_QNAME));
527 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
529 NormalizedNode<?, ?> payload = initInputAction(QName.create(BOX_OUT_QNAME, "start-at"), "now");
530 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
531 OPEN_BOXES_PATH, domDataTreeIdentifier, payload);
533 Node childAction = checkBasePartOfActionRequest(actionRequest);
535 Node childBoxOut = childAction.getFirstChild();
536 checkNode(childBoxOut, "box-out", "box-out", URN_EXAMPLE_SERVER_FARM);
538 Node childBoxIn = childBoxOut.getFirstChild();
539 checkNode(childBoxIn, "box-in", "box-in", URN_EXAMPLE_SERVER_FARM);
541 Node action = childBoxIn.getFirstChild();
542 checkNode(action, null, OPEN_QNAME.getLocalName(), null);
546 public void toActionRequestListInContainerTest() {
547 QName nameQname = QName.create(INTERFACE_QNAME, "name");
549 List<PathArgument> nodeIdentifiers = new ArrayList<>();
550 nodeIdentifiers.add(NodeIdentifier.create(DEVICE_QNAME));
551 nodeIdentifiers.add(NodeIdentifier.create(INTERFACE_QNAME));
552 nodeIdentifiers.add(new NodeIdentifierWithPredicates(INTERFACE_QNAME, nameQname, "test"));
554 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
556 NormalizedNode<?, ?> payload = initEmptyInputAction(INTERFACE_QNAME);
557 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
558 ENABLE_INTERFACE_PATH, domDataTreeIdentifier, payload);
560 Node childAction = checkBasePartOfActionRequest(actionRequest);
562 Node childDevice = childAction.getFirstChild();
563 checkNode(childDevice, "device", "device", URN_EXAMPLE_SERVER_FARM);
565 Node childInterface = childDevice.getFirstChild();
566 checkNode(childInterface, "interface", "interface", URN_EXAMPLE_SERVER_FARM);
568 Node childName = childInterface.getFirstChild();
569 checkNode(childName, "name", "name", nameQname.getNamespace().toString());
571 Node childTest = childName.getFirstChild();
572 assertEquals(childTest.getNodeValue(), "test");
574 Node action = childInterface.getLastChild();
575 checkNode(action, null, ENABLE_QNAME.getLocalName(), null);
579 public void toActionRequestListInContainerAugmentedIntoListTest() {
580 QName serverNameQname = QName.create(SERVER_QNAME, "name");
581 QName applicationNameQname = QName.create(APPLICATION_QNAME, "name");
583 List<PathArgument> nodeIdentifiers = new ArrayList<>();
584 nodeIdentifiers.add(NodeIdentifier.create(SERVER_QNAME));
585 nodeIdentifiers.add(new NodeIdentifierWithPredicates(SERVER_QNAME, serverNameQname, "testServer"));
586 nodeIdentifiers.add(new YangInstanceIdentifier
587 .AugmentationIdentifier(Collections.singleton(APPLICATIONS_QNAME)));
588 nodeIdentifiers.add(NodeIdentifier.create(APPLICATIONS_QNAME));
589 nodeIdentifiers.add(NodeIdentifier.create(APPLICATION_QNAME));
590 nodeIdentifiers.add(new NodeIdentifierWithPredicates(APPLICATION_QNAME,
591 applicationNameQname, "testApplication"));
593 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
595 NormalizedNode<?, ?> payload = initEmptyInputAction(APPLICATION_QNAME);
596 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
597 KILL_SERVER_APP_PATH, domDataTreeIdentifier, payload);
599 Node childAction = checkBasePartOfActionRequest(actionRequest);
601 Node childServer = childAction.getFirstChild();
602 checkNode(childServer, "server", "server", URN_EXAMPLE_SERVER_FARM);
604 Node childServerName = childServer.getFirstChild();
605 checkNode(childServerName, "name", "name", URN_EXAMPLE_SERVER_FARM);
607 Node childServerNameTest = childServerName.getFirstChild();
608 assertEquals(childServerNameTest.getNodeValue(), "testServer");
610 Node childApplications = childServer.getLastChild();
611 checkNode(childApplications, "applications", "applications", URN_EXAMPLE_SERVER_FARM_2);
613 Node childApplication = childApplications.getFirstChild();
614 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
616 Node childApplicationName = childApplication.getFirstChild();
617 checkNode(childApplicationName, "name", "name", URN_EXAMPLE_SERVER_FARM_2);
619 Node childApplicationNameTest = childApplicationName.getFirstChild();
620 assertEquals(childApplicationNameTest.getNodeValue(), "testApplication");
622 Node childKillAction = childApplication.getLastChild();
623 checkNode(childApplication, "application", "application", URN_EXAMPLE_SERVER_FARM_2);
624 checkNode(childKillAction, null, KILL_QNAME.getLocalName(), null);
628 public void toActionRequestConflictingInListTest() {
629 QName barInputQname = QName.create(BAR_QNAME, "bar");
630 QName barIdQname = QName.create(BAR_QNAME, "bar-id");
631 Byte barInput = new Byte("1");
633 List<PathArgument> nodeIdentifiers = new ArrayList<>();
634 nodeIdentifiers.add(NodeIdentifier.create(BAR_QNAME));
635 nodeIdentifiers.add(new NodeIdentifierWithPredicates(BAR_QNAME, barIdQname, "test"));
637 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
639 ImmutableLeafNodeBuilder<Byte> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
640 DataContainerChild<NodeIdentifier, Byte> build = immutableLeafNodeBuilder.withNodeIdentifier(
641 NodeIdentifier.create(barInputQname)).withValue(barInput).build();
642 NormalizedNode<?, ?> payload = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
643 QName.create(barInputQname, "input"))).withChild(build).build();
645 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
646 XYZZY_BAR_PATH, domDataTreeIdentifier, payload);
648 Node childAction = checkBasePartOfActionRequest(actionRequest);
650 Node childBar = childAction.getFirstChild();
651 checkNode(childBar, "bar", "bar", URN_EXAMPLE_CONFLICT);
653 Node childBarId = childBar.getFirstChild();
654 checkNode(childBarId, "bar-id", "bar-id", URN_EXAMPLE_CONFLICT);
656 Node childTest = childBarId.getFirstChild();
657 assertEquals(childTest.getNodeValue(), "test");
659 Node action = childBar.getLastChild();
660 checkNode(action, null, XYZZY_QNAME.getLocalName(), null);
664 public void toActionRequestConflictingInContainerTest() {
665 QName fooInputQname = QName.create(FOO_QNAME, "foo");
667 List<PathArgument> nodeIdentifiers = new ArrayList<>();
668 nodeIdentifiers.add(NodeIdentifier.create(FOO_QNAME));
669 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
670 NormalizedNode<?, ?> payload = initInputAction(fooInputQname, "test");
672 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
673 XYZZY_FOO_PATH, domDataTreeIdentifier, payload);
675 Node childAction = checkBasePartOfActionRequest(actionRequest);
677 Node childBar = childAction.getFirstChild();
678 checkNode(childBar, "foo", "foo", URN_EXAMPLE_CONFLICT);
680 Node action = childBar.getLastChild();
681 checkNode(action, null, XYZZY_QNAME.getLocalName(), null);
686 public void toActionRequestChoiceTest() {
687 List<PathArgument> nodeIdentifiers = new ArrayList<>();
688 nodeIdentifiers.add(NodeIdentifier.create(CHOICE_CONT_QNAME));
689 DOMDataTreeIdentifier domDataTreeIdentifier = prepareDataTreeId(nodeIdentifiers);
690 NormalizedNode<?, ?> payload = initEmptyInputAction(CHOICE_ACTION_QNAME);
692 NetconfMessage actionRequest = actionNetconfMessageTransformer.toActionRequest(
693 CHOICE_ACTION_PATH, domDataTreeIdentifier, payload);
695 Node childAction = checkBasePartOfActionRequest(actionRequest);
697 Node childChoiceCont = childAction.getFirstChild();
698 checkNode(childChoiceCont, "choice-cont", "choice-cont", URN_EXAMPLE_CONFLICT);
700 Node action = childChoiceCont.getLastChild();
701 checkNode(action, null, CHOICE_ACTION_QNAME.getLocalName(), null);
704 @SuppressWarnings({ "rawtypes", "unchecked" })
706 public void toActionResultTest() throws Exception {
707 NetconfMessage message = new NetconfMessage(XmlUtil.readXmlToDocument(
708 "<rpc-reply message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
709 + "<reset-finished-at xmlns=\"urn:example:server-farm\">"
711 + "</reset-finished-at>"
713 DOMActionResult actionResult = actionNetconfMessageTransformer.toActionResult(RESET_SERVER_PATH, message);
714 assertNotNull(actionResult);
715 ContainerNode containerNode = actionResult.getOutput().get();
716 assertNotNull(containerNode);
717 LeafNode<String> leaf = (LeafNode) containerNode.getValue().iterator().next();
718 assertEquals("now", leaf.getValue());
721 private static void checkAction(final QName actionQname, final Node action , final String inputLocalName,
722 final String inputNodeName, final String inputValue) {
723 checkNode(action, null, actionQname.getLocalName(), null);
725 Node childResetAt = action.getFirstChild();
726 checkNode(childResetAt, inputLocalName, inputNodeName, actionQname.getNamespace().toString());
728 Node firstChild = childResetAt.getFirstChild();
729 assertEquals(firstChild.getNodeValue(), inputValue);
732 private static Node checkBasePartOfActionRequest(final NetconfMessage actionRequest) {
733 Node baseRpc = actionRequest.getDocument().getFirstChild();
734 checkNode(baseRpc, "rpc", "rpc", NetconfUtil.NETCONF_QNAME.getNamespace().toString());
735 assertTrue(baseRpc.getLocalName().equals("rpc"));
736 assertTrue(baseRpc.getNodeName().equals("rpc"));
738 Node messageId = baseRpc.getAttributes().getNamedItem("message-id");
739 assertNotNull(messageId);
740 assertTrue(messageId.getNodeValue().contains("m-"));
742 Node childAction = baseRpc.getFirstChild();
743 checkNode(childAction, "action", "action", NetconfMessageTransformUtil.NETCONF_ACTION_NAMESPACE.toString());
747 private static DOMDataTreeIdentifier prepareDataTreeId(final List<PathArgument> nodeIdentifiers) {
748 YangInstanceIdentifier yangInstanceIdentifier =
749 YangInstanceIdentifier.builder().append(nodeIdentifiers).build();
750 DOMDataTreeIdentifier domDataTreeIdentifier =
751 new DOMDataTreeIdentifier(org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION,
752 yangInstanceIdentifier);
753 return domDataTreeIdentifier;
756 private static ContainerNode initInputAction(final QName qname, final String value) {
757 ImmutableLeafNodeBuilder<String> immutableLeafNodeBuilder = new ImmutableLeafNodeBuilder<>();
758 DataContainerChild<NodeIdentifier, String> build = immutableLeafNodeBuilder.withNodeIdentifier(
759 NodeIdentifier.create(qname)).withValue(value).build();
760 ContainerNode data = ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
761 QName.create(qname, "input"))).withChild(build).build();
765 private static ContainerNode initEmptyInputAction(final QName qname) {
766 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(
767 QName.create(qname, "input"))).build();
770 private static void checkNode(final Node childServer, final String expectedLocalName, final String expectedNodeName,
771 final String expectedNamespace) {
772 assertNotNull(childServer);
773 assertEquals(childServer.getLocalName(), expectedLocalName);
774 assertEquals(childServer.getNodeName(), expectedNodeName);
775 assertEquals(childServer.getNamespaceURI(), expectedNamespace);