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;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
15 import java.util.Collection;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.netconf.api.NetconfMessage;
20 import org.opendaylight.netconf.api.xml.XmlUtil;
21 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
22 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 import org.w3c.dom.Document;
32 public class NetconfToRpcRequestTest extends AbstractBaseSchemasTest {
34 private static final String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
35 private static final String REVISION = "2014-07-14";
36 private static final QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
37 private static final QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
39 private static final String CONFIG_TEST_NAMESPACE =
40 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
41 private static final String CONFIG_TEST_REVISION = "2014-07-21";
42 private static final QName EDIT_CONFIG_QNAME =
43 QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
45 static EffectiveModelContext cfgCtx;
47 private NetconfMessageTransformer messageTransformer;
50 public static void setup() {
51 final Collection<? extends Module> notifModules = YangParserTestUtils.parseYangResource(
52 "/schemas/rpc-notification-subscription.yang").getModules();
53 assertTrue(!notifModules.isEmpty());
55 cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
56 "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
60 public void before() {
61 messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(cfgCtx), true,
62 BASE_SCHEMAS.getBaseSchema());
66 public void testUserDefinedRpcCall() throws Exception {
67 final ContainerNode root = Builders.containerBuilder()
68 .withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME))
69 .withChild(ImmutableNodes.leafNode(STREAM_NAME, "NETCONF"))
72 final NetconfMessage message = messageTransformer.toRpcRequest(SUBSCRIBE_RPC_NAME, root);
73 assertNotNull(message);
75 final Document xmlDoc = message.getDocument();
76 final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
77 assertEquals(rpcChild.getLocalName(), "rpc");
79 final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
80 assertEquals(subscribeName.getLocalName(), "subscribe");
82 final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
83 assertEquals(streamName.getLocalName(), "stream-name");
87 // The edit config defined in yang has no output
88 @Test(expected = IllegalArgumentException.class)
89 public void testRpcResponse() throws Exception {
90 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
91 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n"
92 + "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">"
98 messageTransformer.toRpcResult(response, EDIT_CONFIG_QNAME);