Fix netconf-connector-config groupId
[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 import com.google.common.collect.Sets;
17 import java.io.InputStream;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.util.xml.XmlUtil;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
35 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
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     @SuppressWarnings("deprecation")
56     @BeforeClass
57     public static void setup() throws Exception {
58         List<InputStream> modelsToParse = Collections
59             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
60         YangContextParser parser = new YangParserImpl();
61         final Set<Module> notifModules = parser.parseYangModelsFromStreams(modelsToParse);
62         assertTrue(!notifModules.isEmpty());
63
64         modelsToParse = Collections
65             .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/config-test-rpc.yang"));
66         parser = new YangParserImpl();
67         final Set<Module> configModules = parser.parseYangModelsFromStreams(modelsToParse);
68         cfgCtx = parser.resolveSchemaContext(Sets.union(configModules, notifModules));
69         assertNotNull(cfgCtx);
70
71         messageTransformer = new NetconfMessageTransformer(cfgCtx, true);
72     }
73
74     private static LeafNode<Object> buildLeaf(final QName running, final Object value) {
75         return Builders.leafBuilder().withNodeIdentifier(toId(running)).withValue(value).build();
76     }
77
78     @Test
79     public void testUserDefinedRpcCall() throws Exception {
80         final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> rootBuilder = Builders.containerBuilder();
81         rootBuilder.withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME));
82
83         rootBuilder.withChild(buildLeaf(STREAM_NAME, "NETCONF"));
84         final ContainerNode root = rootBuilder.build();
85
86         final NetconfMessage message = messageTransformer.toRpcRequest(toPath(SUBSCRIBE_RPC_NAME), root);
87         assertNotNull(message);
88
89         final Document xmlDoc = message.getDocument();
90         final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
91         assertEquals(rpcChild.getLocalName(), "rpc");
92
93         final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
94         assertEquals(subscribeName.getLocalName(), "subscribe");
95
96         final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
97         assertEquals(streamName.getLocalName(), "stream-name");
98
99     }
100
101     // The edit config defined in yang has no output
102     @Test(expected = IllegalArgumentException.class)
103     public void testRpcResponse() throws Exception {
104         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
105                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n" +
106                 "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">" +
107                 "module schema" +
108                 "</data>\n" +
109                 "</rpc-reply>\n"
110         ));
111
112         messageTransformer.toRpcResult(response, toPath(EDIT_CONFIG_QNAME));
113     }
114
115 }