2 * Copyright (c) 2016 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
8 package org.opendaylight.netconf.client.mdsal.spi;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
17 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
19 import com.google.common.util.concurrent.Futures;
20 import java.net.InetSocketAddress;
21 import java.util.List;
22 import org.junit.AfterClass;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
34 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
35 import org.opendaylight.netconf.api.CapabilityURN;
36 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
37 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
38 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
39 import org.opendaylight.netconf.dom.api.tx.NetconfDOMDataBrokerFieldsExtension;
40 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadTransaction;
41 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadWriteTransaction;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.monitoring.rev220718.NetconfTcp;
44 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
48 @RunWith(MockitoJUnitRunner.class)
49 public class NetconfDeviceDataBrokerTest {
50 private static EffectiveModelContext SCHEMA_CONTEXT;
53 private Rpcs.Normalized rpcService;
54 private NetconfDeviceDataBroker dataBroker;
57 public static void beforeClass() {
58 SCHEMA_CONTEXT = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfTcp.class);
62 public static void afterClass() {
63 SCHEMA_CONTEXT = null;
67 public void setUp() throws Exception {
68 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(rpcService).invokeNetconf(any(), any());
69 dataBroker = getDataBroker(CapabilityURN.CANDIDATE);
73 public void testNewReadOnlyTransaction() {
74 final DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction();
75 tx.read(LogicalDatastoreType.OPERATIONAL, null);
76 verify(rpcService).invokeNetconf(eq(NETCONF_GET_QNAME), any());
80 public void testNewReadWriteTransaction() {
81 final DOMDataTreeReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
82 tx.read(LogicalDatastoreType.OPERATIONAL, null);
83 verify(rpcService).invokeNetconf(eq(NETCONF_GET_QNAME), any());
87 public void testWritableRunningCandidateWriteTransaction() {
88 testWriteTransaction(WriteCandidateRunningTx.class, CapabilityURN.WRITABLE_RUNNING, CapabilityURN.CANDIDATE);
92 public void testCandidateWriteTransaction() {
93 testWriteTransaction(WriteCandidateTx.class, CapabilityURN.CANDIDATE);
97 public void testRunningWriteTransaction() {
98 testWriteTransaction(WriteRunningTx.class, CapabilityURN.WRITABLE_RUNNING);
102 public void testDOMFieldsExtensions() {
103 final NetconfDOMDataBrokerFieldsExtension fieldsExtension = dataBroker.getExtensions().getInstance(
104 NetconfDOMDataBrokerFieldsExtension.class);
105 assertNotNull(fieldsExtension);
107 // read-only transaction
108 final NetconfDOMFieldsReadTransaction roTx = fieldsExtension.newReadOnlyTransaction();
109 roTx.read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty(),
110 List.of(YangInstanceIdentifier.empty()));
111 verify(rpcService).invokeNetconf(Mockito.eq(NETCONF_GET_CONFIG_QNAME), any());
113 // read-write transaction
114 final NetconfDOMFieldsReadWriteTransaction rwTx = fieldsExtension.newReadWriteTransaction();
115 rwTx.read(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.empty(),
116 List.of(YangInstanceIdentifier.empty()));
117 verify(rpcService).invokeNetconf(Mockito.eq(NETCONF_GET_QNAME), any());
120 private void testWriteTransaction(final Class<? extends AbstractWriteTx> transaction,
121 final String... capabilities) {
122 NetconfDeviceDataBroker db = getDataBroker(capabilities);
123 assertEquals(transaction, db.newWriteOnlyTransaction().getClass());
126 private NetconfDeviceDataBroker getDataBroker(final String... caps) {
127 NetconfSessionPreferences prefs = NetconfSessionPreferences.fromStrings(List.of(caps));
128 final RemoteDeviceId id =
129 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
130 return new NetconfDeviceDataBroker(id, new EmptyMountPointContext(SCHEMA_CONTEXT), rpcService, prefs, true);