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