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