2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.sal.connect.netconf.sal;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.ArgumentMatchers.isNull;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import java.net.InetSocketAddress;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Optional;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
38 import org.opendaylight.mdsal.dom.api.DOMRpcService;
39 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
40 import org.opendaylight.netconf.api.NetconfMessage;
41 import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
42 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
43 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.TxTestUtils;
44 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
45 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
46 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
49 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
50 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
55 @RunWith(MockitoJUnitRunner.StrictStubs.class)
56 public class NetconfDataTreeServiceImplTest extends AbstractTestModelTest {
58 private DOMRpcService rpcService;
59 private AbstractNetconfDataTreeService netconService;
60 private NetconfMessageTransformer netconfMessageTransformer;
61 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
65 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpcService)
66 .invokeRpc(any(), any());
67 netconService = getNetconService();
68 final EffectiveModelContext model = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class,
70 netconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(model), true,
71 BASE_SCHEMAS.getBaseSchema());
77 verify(rpcService).invokeRpc(eq(NETCONF_LOCK_QNAME), any(ContainerNode.class));
81 public void unlock() {
82 netconService.unlock();
83 verify(rpcService).invokeRpc(eq(NETCONF_UNLOCK_QNAME), any(ContainerNode.class));
87 public void discardChanges() {
88 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpcService)
89 .invokeRpc(any(QName.class), isNull());
90 netconService.discardChanges();
91 verify(rpcService).invokeRpc(eq(NETCONF_DISCARD_CHANGES_QNAME), isNull());
96 netconService.get(null);
97 verify(rpcService).invokeRpc(eq(NETCONF_GET_QNAME), any(ContainerNode.class));
101 public void getConfig() {
102 netconService.getConfig(null);
103 verify(rpcService).invokeRpc(eq(NETCONF_GET_CONFIG_QNAME), any(ContainerNode.class));
107 public void merge() {
108 netconService.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
110 verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
112 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
113 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
114 Assert.assertTrue(netconfMessage.toString().contains("operation=\"merge\""));
118 public void replace() {
119 netconService.replace(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
121 verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
123 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
124 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
125 Assert.assertTrue(netconfMessage.toString().contains("operation=\"replace\""));
129 public void create() {
130 netconService.create(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
132 verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
134 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
135 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
136 Assert.assertTrue(netconfMessage.toString().contains("operation=\"create\""));
140 public void delete() {
141 netconService.delete(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
142 verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
144 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
145 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
146 Assert.assertTrue(netconfMessage.toString().contains("operation=\"delete\""));
150 public void remove() {
151 netconService.remove(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
152 verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
154 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
155 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
156 Assert.assertTrue(netconfMessage.toString().contains("operation=\"remove\""));
160 public void commit() {
161 List<ListenableFuture<? extends DOMRpcResult>> resultsFutures = new ArrayList<>();
162 netconService.commit(resultsFutures);
163 verify(rpcService).invokeRpc(eq(NETCONF_COMMIT_QNAME), any(ContainerNode.class));
166 private AbstractNetconfDataTreeService getNetconService() {
167 NetconfSessionPreferences prefs = NetconfSessionPreferences.fromStrings(
168 Collections.singletonList(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString()));
169 final RemoteDeviceId id =
170 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
171 return AbstractNetconfDataTreeService.of(id, new EmptyMountPointContext(SCHEMA_CONTEXT), rpcService, prefs);