Merge "Bug 6198 - Use sal-netconf-connector to connet device costs too much time"
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / SchemalessNetconfDeviceRpcTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.verify;
13
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.net.InetSocketAddress;
17 import javax.xml.transform.dom.DOMSource;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.config.util.xml.XmlUtil;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
26 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
27 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
28 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36
37 public class SchemalessNetconfDeviceRpcTest {
38
39     @Mock
40     private RemoteDeviceCommunicator<NetconfMessage> listener;
41
42     private SchemalessNetconfDeviceRpc deviceRpc;
43
44     @Before
45     public void setUp() throws Exception {
46         MockitoAnnotations.initMocks(this);
47         RpcResult<NetconfMessage> msg = null;
48         ListenableFuture<RpcResult<NetconfMessage>> future = Futures.immediateFuture(msg);
49         doReturn(future).when(listener).sendRequest(any(), any());
50         final MessageCounter counter = new MessageCounter();
51         deviceRpc = new SchemalessNetconfDeviceRpc(
52                 new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830)), listener,
53                 new BaseRpcSchemalessTransformer(counter), new SchemalessMessageTransformer(counter));
54
55     }
56
57     @Test
58     public void testInvokeRpc() throws Exception {
59         final QName qName = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "get-config");
60         SchemaPath type = SchemaPath.create(true, qName);
61         DOMSource src = new DOMSource(XmlUtil.readXmlToDocument("<get-config xmlns=\"dd\">\n" +
62                 "    <source>\n" +
63                 "      <running/>\n" +
64                 "    </source>\n" +
65                 "    <filter type=\"subtree\">\n" +
66                 "      <mainroot xmlns=\"urn:dummy:mod-0\">\n" +
67                 "        <maincontent/>\n" +
68                 "<choiceList></choiceList>\n" +
69                 "      </mainroot>\n" +
70                 "    </filter>\n" +
71                 "  </get-config>"));
72         NormalizedNode<?, ?> input = Builders.anyXmlBuilder()
73                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(qName))
74                 .withValue(src)
75                 .build();
76
77         deviceRpc.invokeRpc(type, input);
78         ArgumentCaptor<NetconfMessage> msgCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
79         ArgumentCaptor<QName> qNameCaptor = ArgumentCaptor.forClass(QName.class);
80         verify(listener).sendRequest(msgCaptor.capture(), qNameCaptor.capture());
81         System.out.println(XmlUtil.toString(msgCaptor.getValue().getDocument()));
82     }
83 }