2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.sal.connect.netconf;
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;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.config.util.xml.XmlUtil;
21 import org.opendaylight.netconf.api.NetconfMessage;
22 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.w3c.dom.Document;
34 public class NetconfToRpcRequestTest {
36 private static final String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
37 private static final String REVISION = "2014-07-14";
38 private static final QName INPUT_QNAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "input");
39 private static final QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
40 private static final QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
42 private static final String CONFIG_TEST_NAMESPACE =
43 "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
44 private static final String CONFIG_TEST_REVISION = "2014-07-21";
45 private static final QName EDIT_CONFIG_QNAME =
46 QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
47 private static final QName GET_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get");
48 private static final QName GET_CONFIG_QNAME =
49 QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get-config");
51 static SchemaContext cfgCtx;
52 static NetconfMessageTransformer messageTransformer;
55 public static void setup() {
56 final Set<Module> notifModules = YangParserTestUtils.parseYangResource(
57 "/schemas/rpc-notification-subscription.yang").getModules();
58 assertTrue(!notifModules.isEmpty());
60 cfgCtx = YangParserTestUtils.parseYangResources(NetconfToRpcRequestTest.class,
61 "/schemas/config-test-rpc.yang", "/schemas/rpc-notification-subscription.yang");
62 messageTransformer = new NetconfMessageTransformer(cfgCtx, true);
65 private static LeafNode<Object> buildLeaf(final QName running, final Object value) {
66 return Builders.leafBuilder().withNodeIdentifier(toId(running)).withValue(value).build();
70 public void testUserDefinedRpcCall() throws Exception {
71 final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> rootBuilder =
72 Builders.containerBuilder();
73 rootBuilder.withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME));
75 rootBuilder.withChild(buildLeaf(STREAM_NAME, "NETCONF"));
76 final ContainerNode root = rootBuilder.build();
78 final NetconfMessage message = messageTransformer.toRpcRequest(toPath(SUBSCRIBE_RPC_NAME), root);
79 assertNotNull(message);
81 final Document xmlDoc = message.getDocument();
82 final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
83 assertEquals(rpcChild.getLocalName(), "rpc");
85 final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
86 assertEquals(subscribeName.getLocalName(), "subscribe");
88 final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
89 assertEquals(streamName.getLocalName(), "stream-name");
93 // The edit config defined in yang has no output
94 @Test(expected = IllegalArgumentException.class)
95 public void testRpcResponse() throws Exception {
96 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
97 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-5\">\n"
98 + "<data xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">"
104 messageTransformer.toRpcResult(response, toPath(EDIT_CONFIG_QNAME));