a2f89b82aafafc482d161397b3a28476f4998440
[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.messages.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.spi.node.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.w3c.dom.Document;
32
33 public class NetconfToRpcRequestTest extends AbstractBaseSchemasTest {
34
35     private static final String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
36     private static final String REVISION = "2014-07-14";
37     private static final QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
38     private static final QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
39
40     private static final String CONFIG_TEST_NAMESPACE =
41             "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
42     private static final String CONFIG_TEST_REVISION = "2014-07-21";
43     private static final QName EDIT_CONFIG_QNAME =
44             QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
45
46     static EffectiveModelContext cfgCtx;
47
48     private  NetconfMessageTransformer messageTransformer;
49
50     @BeforeClass
51     public static void setup() {
52         final Collection<? extends Module> notifModules = YangParserTestUtils.parseYangResource(
53             "/schemas/rpc-notification-subscription.yang").getModules();
54         assertTrue(!notifModules.isEmpty());
55
56         cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
57             "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
58     }
59
60     @Before
61     public void before() {
62         messageTransformer = new NetconfMessageTransformer(MountPointContext.of(cfgCtx), true,
63             BASE_SCHEMAS.baseSchema());
64     }
65
66     @Test
67     public void testUserDefinedRpcCall() throws Exception {
68         final ContainerNode root = ImmutableNodes.newContainerBuilder()
69                 .withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME))
70                 .withChild(ImmutableNodes.leafNode(STREAM_NAME, "NETCONF"))
71                 .build();
72
73         final NetconfMessage message = messageTransformer.toRpcRequest(SUBSCRIBE_RPC_NAME, root);
74         assertNotNull(message);
75
76         final Document xmlDoc = message.getDocument();
77         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
78         assertEquals(rpcChild.getLocalName(), "rpc");
79
80         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
81         assertEquals(subscribeName.getLocalName(), "subscribe");
82
83         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
84         assertEquals(streamName.getLocalName(), "stream-name");
85
86     }
87
88     @Test
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\">"
93                         + "module schema"
94                         + "</data>\n"
95                         + "</rpc-reply>\n"
96         ));
97
98         // The edit config defined in yang has no output
99         assertThrows(IllegalArgumentException.class,
100             () -> messageTransformer.toRpcResult(RpcResultBuilder.success(response).build(), EDIT_CONFIG_QNAME));
101     }
102
103 }