Propagate MountPointContext to NetconfMessageTransformer
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / 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
9 package org.opendaylight.netconf.sal.connect.netconf;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
16
17 import java.util.Set;
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.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
23 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 import org.w3c.dom.Document;
34
35 public class NetconfToRpcRequestTest {
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 INPUT_QNAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "input");
40     private static final QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
41     private static final QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
42
43     private static final String CONFIG_TEST_NAMESPACE =
44             "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
45     private static final String CONFIG_TEST_REVISION = "2014-07-21";
46     private static final QName EDIT_CONFIG_QNAME =
47             QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
48     private static final QName GET_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get");
49     private static final QName GET_CONFIG_QNAME =
50             QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get-config");
51
52     static SchemaContext cfgCtx;
53     static NetconfMessageTransformer messageTransformer;
54
55     @BeforeClass
56     public static void setup() {
57         final Set<Module> notifModules = YangParserTestUtils.parseYangResource(
58             "/schemas/rpc-notification-subscription.yang").getModules();
59         assertTrue(!notifModules.isEmpty());
60
61         cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
62             "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
63         messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(cfgCtx), true);
64     }
65
66     private static LeafNode<Object> buildLeaf(final QName running, final Object value) {
67         return Builders.leafBuilder().withNodeIdentifier(toId(running)).withValue(value).build();
68     }
69
70     @Test
71     public void testUserDefinedRpcCall() throws Exception {
72         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> rootBuilder =
73                 Builders.containerBuilder();
74         rootBuilder.withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME));
75
76         rootBuilder.withChild(buildLeaf(STREAM_NAME, "NETCONF"));
77         final ContainerNode root = rootBuilder.build();
78
79         final NetconfMessage message = messageTransformer.toRpcRequest(toPath(SUBSCRIBE_RPC_NAME), root);
80         assertNotNull(message);
81
82         final Document xmlDoc = message.getDocument();
83         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
84         assertEquals(rpcChild.getLocalName(), "rpc");
85
86         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
87         assertEquals(subscribeName.getLocalName(), "subscribe");
88
89         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
90         assertEquals(streamName.getLocalName(), "stream-name");
91
92     }
93
94     // The edit config defined in yang has no output
95     @Test(expected = IllegalArgumentException.class)
96     public void testRpcResponse() throws Exception {
97         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
98                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n"
99                         + "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">"
100                         + "module schema"
101                         + "</data>\n"
102                         + "</rpc-reply>\n"
103         ));
104
105         messageTransformer.toRpcResult(response, toPath(EDIT_CONFIG_QNAME));
106     }
107
108 }