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;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import java.net.InetSocketAddress;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
39 import org.opendaylight.mdsal.dom.api.DOMRpcService;
40 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
41 import org.opendaylight.netconf.api.NetconfMessage;
42 import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
43 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
44 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.TxTestUtils;
45 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
46 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
47 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
50 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
51 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
54 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class NetconfDataTreeServiceImplTest extends AbstractTestModelTest {
59 private DOMRpcService rpcService;
60 private NetconfDataTreeServiceImpl netconService;
61 private NetconfMessageTransformer netconfMessageTransformer;
62 ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
66 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpcService)
67 .invokeRpc(any(), any());
68 netconService = getNetconService();
69 final EffectiveModelContext model = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class,
71 netconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(model), true,
72 BASE_SCHEMAS.getBaseSchema());
78 verify(rpcService).invokeRpc(eq(toPath(NETCONF_LOCK_QNAME)), any(ContainerNode.class));
82 public void unlock() {
83 netconService.unlock();
84 verify(rpcService).invokeRpc(eq(toPath(NETCONF_UNLOCK_QNAME)), any(ContainerNode.class));
88 public void discardChanges() {
89 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpcService)
90 .invokeRpc(any(SchemaPath.class), isNull());
91 netconService.discardChanges();
92 verify(rpcService).invokeRpc(eq(toPath(NETCONF_DISCARD_CHANGES_QNAME)), isNull());
97 netconService.get(null);
98 verify(rpcService).invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any(ContainerNode.class));
102 public void getConfig() {
103 netconService.getConfig(null);
104 verify(rpcService).invokeRpc(eq(toPath(NETCONF_GET_CONFIG_QNAME)), any(ContainerNode.class));
108 public void merge() {
109 netconService.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
111 verify(rpcService).invokeRpc(eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)),
114 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
115 toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.getValue());
116 Assert.assertTrue(netconfMessage.toString().contains("operation=\"merge\""));
120 public void replace() {
121 netconService.replace(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
123 verify(rpcService).invokeRpc(
124 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), captor.capture());
126 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
127 toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.getValue());
128 Assert.assertTrue(netconfMessage.toString().contains("operation=\"replace\""));
132 public void create() {
133 netconService.create(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
135 verify(rpcService).invokeRpc(
136 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), captor.capture());
138 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
139 toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.getValue());
140 Assert.assertTrue(netconfMessage.toString().contains("operation=\"create\""));
144 public void delete() {
145 netconService.delete(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
146 verify(rpcService).invokeRpc(
147 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), captor.capture());
149 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
150 toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.getValue());
151 Assert.assertTrue(netconfMessage.toString().contains("operation=\"delete\""));
155 public void remove() {
156 netconService.remove(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
157 verify(rpcService).invokeRpc(
158 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), captor.capture());
160 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
161 toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.getValue());
162 Assert.assertTrue(netconfMessage.toString().contains("operation=\"remove\""));
166 public void commit() {
167 List<ListenableFuture<? extends DOMRpcResult>> resultsFutures = new ArrayList<>();
168 netconService.commit(resultsFutures);
169 verify(rpcService).invokeRpc(eq(toPath(NETCONF_COMMIT_QNAME)), any(ContainerNode.class));
172 private NetconfDataTreeServiceImpl getNetconService() {
173 NetconfSessionPreferences prefs = NetconfSessionPreferences.fromStrings(
174 Collections.singletonList(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString()));
175 final RemoteDeviceId id =
176 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
177 return new NetconfDataTreeServiceImpl(id, new EmptyMountPointContext(SCHEMA_CONTEXT), rpcService, prefs);