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.ArgumentMatchers.isNull;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
24 import com.google.common.util.concurrent.Futures;
25 import java.net.InetSocketAddress;
26 import java.util.List;
27 import java.util.Optional;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Captor;
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.spi.DefaultDOMRpcResult;
38 import org.opendaylight.netconf.api.NetconfMessage;
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.netconf.sal.connect.util.RemoteDeviceId;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
48 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 @RunWith(MockitoJUnitRunner.StrictStubs.class)
52 public class NetconfDataTreeServiceImplTest extends AbstractTestModelTest {
54 private Rpcs.Normalized rpcService;
56 private ArgumentCaptor<ContainerNode> captor;
58 private AbstractNetconfDataTreeService netconService;
59 private NetconfMessageTransformer netconfMessageTransformer;
63 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(rpcService).invokeNetconf(any(), any());
64 netconService = getNetconService();
65 final var model = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class, NetconfState.class);
66 netconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(model), true,
67 BASE_SCHEMAS.getBaseSchema());
73 verify(rpcService).invokeNetconf(eq(NETCONF_LOCK_QNAME), any());
77 public void unlock() {
79 netconService.unlock();
80 verify(rpcService).invokeNetconf(eq(NETCONF_LOCK_QNAME), any());
81 verify(rpcService).invokeNetconf(eq(NETCONF_UNLOCK_QNAME), any());
85 public void discardChanges() {
86 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(rpcService).invokeNetconf(any(), isNull());
87 netconService.discardChanges();
88 verify(rpcService).invokeNetconf(eq(NETCONF_DISCARD_CHANGES_QNAME), isNull());
93 netconService.get(null);
94 verify(rpcService).invokeNetconf(eq(NETCONF_GET_QNAME), any());
98 public void getConfig() {
99 netconService.getConfig(null);
100 verify(rpcService).invokeNetconf(eq(NETCONF_GET_CONFIG_QNAME), any());
104 public void merge() {
105 netconService.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
107 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
109 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
110 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
111 assertThat(netconfMessage.toString(), containsString("operation=\"merge\""));
115 public void replace() {
116 netconService.replace(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
118 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
120 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
121 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
122 assertThat(netconfMessage.toString(), containsString("operation=\"replace\""));
126 public void create() {
127 netconService.create(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
129 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
131 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
132 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
133 assertThat(netconfMessage.toString(), containsString("operation=\"create\""));
137 public void delete() {
138 netconService.delete(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
139 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
141 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
142 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
143 assertThat(netconfMessage.toString(), containsString("operation=\"delete\""));
147 public void remove() {
148 netconService.remove(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
149 verify(rpcService).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
151 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
152 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
153 assertThat(netconfMessage.toString(), containsString("operation=\"remove\""));
157 public void commit() {
158 netconService.commit();
159 verify(rpcService).invokeNetconf(eq(NETCONF_COMMIT_QNAME), any());
162 private AbstractNetconfDataTreeService getNetconService() {
163 NetconfSessionPreferences prefs = NetconfSessionPreferences.fromStrings(
164 List.of(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString()));
165 final RemoteDeviceId id =
166 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
167 return AbstractNetconfDataTreeService.of(id, new EmptyMountPointContext(SCHEMA_CONTEXT), rpcService, prefs);