BUG-2314 Migrate netconf-connector to NormalizedNode
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / NetconfToRpcRequestTest.java
1 package org.opendaylight.controller.sal.connect.netconf;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
7 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
8
9 import com.google.common.collect.Sets;
10 import java.io.InputStream;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Set;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 import org.w3c.dom.Document;
30
31
32 /**
33  * Test case for reported bug 1355
34  *
35  * @author Lukas Sedlak
36  * @see <a
37  *      https://bugs.opendaylight.org/show_bug.cgi?id=1355</a>
38  */
39 public class NetconfToRpcRequestTest {
40
41     private final static String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
42     private final static String REVISION = "2014-07-14";
43     private final static QName INPUT_QNAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "input");
44     private final static QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
45     private final static QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
46
47     private final static String CONFIG_TEST_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
48     private final static String CONFIG_TEST_REVISION = "2014-07-21";
49     private final static QName EDIT_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
50     private final static QName GET_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get");
51     private final static QName GET_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get-config");
52
53     static SchemaContext cfgCtx;
54     static NetconfMessageTransformer messageTransformer;
55
56     @SuppressWarnings("deprecation")
57     @BeforeClass
58     public static void setup() throws Exception {
59         List<InputStream> modelsToParse = Collections
60             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
61         YangContextParser parser = new YangParserImpl();
62         final Set<Module> notifModules = parser.parseYangModelsFromStreams(modelsToParse);
63         assertTrue(!notifModules.isEmpty());
64
65         modelsToParse = Collections
66             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/config-test-rpc.yang"));
67         parser = new YangParserImpl();
68         final Set<Module> configModules = parser.parseYangModelsFromStreams(modelsToParse);
69         cfgCtx = parser.resolveSchemaContext(Sets.union(configModules, notifModules));
70         assertNotNull(cfgCtx);
71
72         messageTransformer = new NetconfMessageTransformer(cfgCtx);
73     }
74
75     private LeafNode<Object> buildLeaf(final QName running, final Object value) {
76         return Builders.leafBuilder().withNodeIdentifier(toId(running)).withValue(value).build();
77     }
78
79     @Test
80     public void testUserDefinedRpcCall() throws Exception {
81         final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> rootBuilder = Builders.containerBuilder();
82         rootBuilder.withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME));
83
84         rootBuilder.withChild(buildLeaf(STREAM_NAME, "NETCONF"));
85         final ContainerNode root = rootBuilder.build();
86
87         final NetconfMessage message = messageTransformer.toRpcRequest(toPath(SUBSCRIBE_RPC_NAME), root);
88         assertNotNull(message);
89
90         final Document xmlDoc = message.getDocument();
91         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
92         assertEquals(rpcChild.getLocalName(), "rpc");
93
94         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
95         assertEquals(subscribeName.getLocalName(), "subscribe");
96
97         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
98         assertEquals(streamName.getLocalName(), "stream-name");
99
100     }
101
102     // The edit config defined in yang has no output
103     @Test(expected = IllegalArgumentException.class)
104     public void testRpcResponse() throws Exception {
105         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
106                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n" +
107                 "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">" +
108                 "module schema" +
109                 "</data>\n" +
110                 "</rpc-reply>\n"
111         ));
112
113         messageTransformer.toRpcResult(response, toPath(EDIT_CONFIG_QNAME));
114     }
115
116 }