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;
17 import com.google.common.collect.Lists;
18 import java.io.InputStream;
19 import java.util.Collections;
20 import java.util.List;
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.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
38 import org.w3c.dom.Document;
40 public class NetconfToRpcRequestTest {
42 private final static String TEST_MODEL_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:rpc-test";
43 private final static String REVISION = "2014-07-14";
44 private final static QName INPUT_QNAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "input");
45 private final static QName STREAM_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "stream-name");
46 private final static QName SUBSCRIBE_RPC_NAME = QName.create(TEST_MODEL_NAMESPACE, REVISION, "subscribe");
48 private final static String CONFIG_TEST_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:rpc:config:defs";
49 private final static String CONFIG_TEST_REVISION = "2014-07-21";
50 private final static QName EDIT_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "edit-config");
51 private final static QName GET_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get");
52 private final static QName GET_CONFIG_QNAME = QName.create(CONFIG_TEST_NAMESPACE, CONFIG_TEST_REVISION, "get-config");
54 static SchemaContext cfgCtx;
55 static NetconfMessageTransformer messageTransformer;
58 public static void setup() throws Exception {
59 List<InputStream> modelsToParse = Collections
60 .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
61 final Set<Module> notifModules = parseYangStreams(modelsToParse).getModules();
62 assertTrue(!notifModules.isEmpty());
64 modelsToParse = Lists.newArrayList(
65 NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/config-test-rpc.yang"),
66 NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
67 cfgCtx = parseYangStreams(modelsToParse);
68 messageTransformer = new NetconfMessageTransformer(cfgCtx, true);
71 private static SchemaContext parseYangStreams(final List<InputStream> streams) {
72 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
74 final SchemaContext schemaContext;
76 schemaContext = reactor.buildEffective(streams);
77 } catch (ReactorException e) {
78 throw new RuntimeException("Unable to build schema context from " + streams, e);
83 private static LeafNode<Object> buildLeaf(final QName running, final Object value) {
84 return Builders.leafBuilder().withNodeIdentifier(toId(running)).withValue(value).build();
88 public void testUserDefinedRpcCall() throws Exception {
89 final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> rootBuilder = Builders.containerBuilder();
90 rootBuilder.withNodeIdentifier(toId(SUBSCRIBE_RPC_NAME));
92 rootBuilder.withChild(buildLeaf(STREAM_NAME, "NETCONF"));
93 final ContainerNode root = rootBuilder.build();
95 final NetconfMessage message = messageTransformer.toRpcRequest(toPath(SUBSCRIBE_RPC_NAME), root);
96 assertNotNull(message);
98 final Document xmlDoc = message.getDocument();
99 final org.w3c.dom.Node rpcChild = xmlDoc.getFirstChild();
100 assertEquals(rpcChild.getLocalName(), "rpc");
102 final org.w3c.dom.Node subscribeName = rpcChild.getFirstChild();
103 assertEquals(subscribeName.getLocalName(), "subscribe");
105 final org.w3c.dom.Node streamName = subscribeName.getFirstChild();
106 assertEquals(streamName.getLocalName(), "stream-name");
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\">" +
121 messageTransformer.toRpcResult(response, toPath(EDIT_CONFIG_QNAME));