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.client.mdsal;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.toId;
16 import java.util.Collection;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.netconf.api.NetconfMessage;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformer;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.w3c.dom.Document;
34 public class NetconfToRpcRequestTest extends AbstractBaseSchemasTest {
36 private static final String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
37 private static final String REVISION = "2014-07-14";
38 private static final QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
39 private static final QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
41 private static final String CONFIG_TEST_NAMESPACE =
42 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
43 private static final String CONFIG_TEST_REVISION = "2014-07-21";
44 private static final QName EDIT_CONFIG_QNAME =
45 QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
47 static EffectiveModelContext cfgCtx;
49 private NetconfMessageTransformer messageTransformer;
52 public static void setup() {
53 final Collection<? extends Module> notifModules = YangParserTestUtils.parseYangResource(
54 "/schemas/rpc-notification-subscription.yang").getModules();
55 assertTrue(!notifModules.isEmpty());
57 cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
58 "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
62 public void before() {
63 messageTransformer = new NetconfMessageTransformer(MountPointContext.of(cfgCtx), true,
64 BASE_SCHEMAS.getBaseSchema());
68 public void testUserDefinedRpcCall() throws Exception {
69 final ContainerNode root = Builders.containerBuilder()
70 .withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME))
71 .withChild(ImmutableNodes.leafNode(STREAM_NAME, "NETCONF"))
74 final NetconfMessage message = messageTransformer.toRpcRequest(SUBSCRIBE_RPC_NAME, root);
75 assertNotNull(message);
77 final Document xmlDoc = message.getDocument();
78 final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
79 assertEquals(rpcChild.getLocalName(), "rpc");
81 final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
82 assertEquals(subscribeName.getLocalName(), "subscribe");
84 final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
85 assertEquals(streamName.getLocalName(), "stream-name");
90 public void testRpcResponse() throws Exception {
91 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
92 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n"
93 + "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">"
99 // The edit config defined in yang has no output
100 assertThrows(IllegalArgumentException.class,
101 () -> messageTransformer.toRpcResult(RpcResultBuilder.success(response).build(), EDIT_CONFIG_QNAME));