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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
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.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
23 import com.google.common.util.concurrent.Futures;
24 import java.net.InetSocketAddress;
25 import java.util.List;
26 import java.util.Optional;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Captor;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
37 import org.opendaylight.netconf.api.NetconfMessage;
38 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
39 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
40 import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
41 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
42 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.TxTestUtils;
43 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
44 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
47 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 @RunWith(MockitoJUnitRunner.StrictStubs.class)
51 public class NetconfDataTreeServiceImplTest extends AbstractTestModelTest {
53 private Rpcs.Normalized rpcService;
55 private ArgumentCaptor<ContainerNode> captor;
57 private AbstractNetconfDataTreeService netconService;
58 private NetconfMessageTransformer netconfMessageTransformer;
62 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(rpcService).invokeNetconf(any(), any());
63 netconService = getNetconService();
64 final var model = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class);
65 netconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(model), true,
66 BASE_SCHEMAS.getBaseSchema());
72 verify(rpcService).invokeNetconf(eq(NETCONF_LOCK_QNAME), any());
76 public void unlock() {
78 netconService.unlock();
79 verify(rpcService).invokeNetconf(eq(NETCONF_LOCK_QNAME), any());
80 verify(rpcService).invokeNetconf(eq(NETCONF_UNLOCK_QNAME), any());
84 public void discardChanges() {
85 netconService.discardChanges();
86 verify(rpcService).invokeNetconf(eq(NETCONF_DISCARD_CHANGES_QNAME), any());
91 netconService.get(null);
92 verify(rpcService).invokeNetconf(eq(NETCONF_GET_QNAME), any());
96 public void getConfig() {
97 netconService.getConfig(null);
98 verify(rpcService).invokeNetconf(eq(NETCONF_GET_CONFIG_QNAME), any());
102 public void merge() {
103 netconService.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
105 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
107 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
108 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
109 assertThat(netconfMessage.toString(), containsString("operation=\"merge\""));
113 public void replace() {
114 netconService.replace(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
116 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
118 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
119 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
120 assertThat(netconfMessage.toString(), containsString("operation=\"replace\""));
124 public void create() {
125 netconService.create(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
127 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
129 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
130 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
131 assertThat(netconfMessage.toString(), containsString("operation=\"create\""));
135 public void delete() {
136 netconService.delete(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
137 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
139 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
140 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
141 assertThat(netconfMessage.toString(), containsString("operation=\"delete\""));
145 public void remove() {
146 netconService.remove(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
147 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
149 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
150 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
151 assertThat(netconfMessage.toString(), containsString("operation=\"remove\""));
155 public void commit() {
156 netconService.commit();
157 verify(rpcService).invokeNetconf(eq(NETCONF_COMMIT_QNAME), any());
160 private AbstractNetconfDataTreeService getNetconService() {
161 NetconfSessionPreferences prefs = NetconfSessionPreferences.fromStrings(
162 List.of(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString()));
163 final RemoteDeviceId id =
164 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
165 return AbstractNetconfDataTreeService.of(id, new EmptyMountPointContext(SCHEMA_CONTEXT), rpcService, prefs,