5007457fa1e39b107aa25a730a0cd76ae1c06c51
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / NetconfToRpcRequestTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.client.mdsal;
9
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;
15
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.rfc8528.data.util.EmptyMountPointContext;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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;
33
34 public class NetconfToRpcRequestTest extends AbstractBaseSchemasTest {
35
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");
40
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");
46
47     static EffectiveModelContext cfgCtx;
48
49     private  NetconfMessageTransformer messageTransformer;
50
51     @BeforeClass
52     public static void setup() {
53         final Collection<? extends Module> notifModules = YangParserTestUtils.parseYangResource(
54             "/schemas/rpc-notification-subscription.yang").getModules();
55         assertTrue(!notifModules.isEmpty());
56
57         cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
58             "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
59     }
60
61     @Before
62     public void before() {
63         messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(cfgCtx), true,
64             BASE_SCHEMAS.getBaseSchema());
65     }
66
67     @Test
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"))
72                 .build();
73
74         final NetconfMessage message = messageTransformer.toRpcRequest(SUBSCRIBE_RPC_NAME, root);
75         assertNotNull(message);
76
77         final Document xmlDoc = message.getDocument();
78         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
79         assertEquals(rpcChild.getLocalName(), "rpc");
80
81         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
82         assertEquals(subscribeName.getLocalName(), "subscribe");
83
84         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
85         assertEquals(streamName.getLocalName(), "stream-name");
86
87     }
88
89     @Test
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\">"
94                         + "module schema"
95                         + "</data>\n"
96                         + "</rpc-reply>\n"
97         ));
98
99         // The edit config defined in yang has no output
100         assertThrows(IllegalArgumentException.class,
101             () -> messageTransformer.toRpcResult(RpcResultBuilder.success(response).build(), EDIT_CONFIG_QNAME));
102     }
103
104 }