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