Merge "Use yangtools ParserStreamUtils.findSchemaNodeByNameAndNamespace() method"
[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 com.google.common.collect.Lists;
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.config.util.xml.XmlUtil;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.opendaylight.netconf.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.test.util.YangParserTestUtils;
36 import org.w3c.dom.Document;
37
38 public class NetconfToRpcRequestTest {
39
40     private final static String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
41     private final static String REVISION = "2014-07-14";
42     private final static QName INPUT_QNAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "input");
43     private final static QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
44     private final static QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
45
46     private final static String CONFIG_TEST_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
47     private final static String CONFIG_TEST_REVISION = "2014-07-21";
48     private final static QName EDIT_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
49     private final static QName GET_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get");
50     private final static QName GET_CONFIG_QNAME = 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() throws Exception {
57         List<InputStream> modelsToParse = Collections
58             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
59         final Set<Module> notifModules = YangParserTestUtils.parseYangStreams(modelsToParse).getModules();
60         assertTrue(!notifModules.isEmpty());
61
62         modelsToParse = Lists.newArrayList(
63                 NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/config-test-rpc.yang"),
64                 NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
65         cfgCtx = YangParserTestUtils.parseYangStreams(modelsToParse);
66         messageTransformer = new NetconfMessageTransformer(cfgCtx, true);
67     }
68
69     private static LeafNode<Object> buildLeaf(final QName running, final Object value) {
70         return Builders.leafBuilder().withNodeIdentifier(toId(running)).withValue(value).build();
71     }
72
73     @Test
74     public void testUserDefinedRpcCall() throws Exception {
75         final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> rootBuilder = Builders.containerBuilder();
76         rootBuilder.withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME));
77
78         rootBuilder.withChild(buildLeaf(STREAM_NAME, "NETCONF"));
79         final ContainerNode root = rootBuilder.build();
80
81         final NetconfMessage message = messageTransformer.toRpcRequest(toPath(SUBSCRIBE_RPC_NAME), root);
82         assertNotNull(message);
83
84         final Document xmlDoc = message.getDocument();
85         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
86         assertEquals(rpcChild.getLocalName(), "rpc");
87
88         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
89         assertEquals(subscribeName.getLocalName(), "subscribe");
90
91         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
92         assertEquals(streamName.getLocalName(), "stream-name");
93
94     }
95
96     // The edit config defined in yang has no output
97     @Test(expected = IllegalArgumentException.class)
98     public void testRpcResponse() throws Exception {
99         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
100                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n" +
101                 "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">" +
102                 "module schema" +
103                 "</data>\n" +
104                 "</rpc-reply>\n"
105         ));
106
107         messageTransformer.toRpcResult(response, toPath(EDIT_CONFIG_QNAME));
108     }
109
110 }