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