51f52cb4fc03409f3552ac95c5fa647d06124010
[netconf.git] / apps / callhome-provider / src / test / java / org / opendaylight / netconf / callhome / mount / CallHomeMountServiceTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.callhome.mount;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertNull;
13 import static org.junit.jupiter.api.Assertions.assertSame;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.eq;
16 import static org.mockito.Mockito.doAnswer;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21
22 import com.google.common.util.concurrent.ListenableFuture;
23 import io.netty.channel.Channel;
24 import java.net.InetSocketAddress;
25 import java.net.SocketAddress;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.opendaylight.netconf.callhome.server.CallHomeStatusRecorder;
32 import org.opendaylight.netconf.callhome.server.tls.CallHomeTlsAuthProvider;
33 import org.opendaylight.netconf.client.NetconfClientSession;
34 import org.opendaylight.netconf.client.NetconfClientSessionListener;
35 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
36 import org.opendaylight.netconf.transport.api.UnsupportedConfigurationException;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNode;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40
41 @ExtendWith(MockitoExtension.class)
42 public class CallHomeMountServiceTest {
43
44     private static final SocketAddress SOCKET_ADDRESS = new InetSocketAddress("127.0.0.1", 12345);
45     private static final String ID1 = "id1";
46     private static final NodeId NODE_ID1 = new NodeId(ID1);
47     private static final String ID2 = "id2";
48
49     @Mock
50     private CallHomeTopology topology;
51     @Mock
52     private NetconfClientSessionListener sessionListener;
53     @Mock
54     private ClientSession sshSession;
55     @Mock
56     private Channel nettyChannel;
57     @Mock
58     private CallHomeTlsAuthProvider tlsAuthProvider;
59     @Mock
60     private CallHomeStatusRecorder statusRecorder;
61
62     private CallHomeMountService service;
63     private ListenableFuture<NetconfClientSession> netconfSessionFuture;
64     private Node node1;
65
66     @BeforeEach
67     void beforeEach() {
68         service = new CallHomeMountService(topology);
69         /*
70          * Reproduce behavior of org.opendaylight.netconf.topology.spi.AbstractNetconfTopology#ensureNode(Node)
71          * for ID1 only.
72          */
73         doAnswer(invocation -> {
74             node1 = (Node) invocation.getArguments()[0];
75             if (ID1.equals(node1.requireNodeId().getValue())) {
76                 final var configBuilderFactory = CallHomeMountService.createClientConfigurationBuilderFactory();
77                 final var config = configBuilderFactory
78                     .createClientConfigurationBuilder(node1.requireNodeId(), node1.augmentation(NetconfNode.class))
79                     .withSessionListener(sessionListener).build();
80                 try {
81                     netconfSessionFuture = service.createClientFactory().createClient(config);
82                 } catch (UnsupportedConfigurationException e) {
83                     netconfSessionFuture = null;
84                 }
85             } else {
86                 netconfSessionFuture = null;
87             }
88             return null;
89         }).when(topology).enableNode(any(Node.class));
90         doNothing().when(topology).disableNode(any(NodeId.class));
91     }
92
93     @Test
94     void sshSessionContextManager() throws Exception {
95         doReturn(SOCKET_ADDRESS).when(sshSession).getRemoteAddress();
96         final var sshSessionContextManager = service.createSshSessionContextManager();
97
98         // id 1 -- netconf layer created
99         final var context = sshSessionContextManager.createContext(ID1, sshSession);
100         assertNotNull(context);
101         assertEquals(ID1, context.id());
102         assertEquals(SOCKET_ADDRESS, context.remoteAddress());
103         assertSame(sshSession, context.sshSession());
104         assertSame(sessionListener, context.netconfSessionListener());
105         assertNotNull(context.settableFuture());
106         assertSame(netconfSessionFuture, context.settableFuture());
107         // id 2 -- netconf layer omitted
108         assertNull(sshSessionContextManager.createContext(ID2, sshSession));
109
110         // verify that node is enabled with SSH
111         verify(topology, times(1)).enableNode(node1);
112
113         // remove context
114         sshSessionContextManager.remove(ID1);
115         verify(topology, times(1)).disableNode(eq(NODE_ID1));
116     }
117
118     @Test
119     void tlsSessionContextManager() {
120         doReturn(SOCKET_ADDRESS).when(nettyChannel).remoteAddress();
121         final var tlsSessionContextManager = service.createTlsSessionContextManager(tlsAuthProvider, statusRecorder);
122
123         // id 1 -- netconf layer created
124         final var context = tlsSessionContextManager.createContext(ID1, nettyChannel);
125         assertNotNull(context);
126         assertEquals(ID1, context.id());
127         assertSame(nettyChannel, context.nettyChannel());
128         assertSame(sessionListener, context.netconfSessionListener());
129         assertNotNull(context.settableFuture());
130         assertSame(netconfSessionFuture, context.settableFuture());
131         // id 2 -- netconf layer omitted
132         assertNull(tlsSessionContextManager.createContext(ID2, nettyChannel));
133
134         // verify that node is enabled with TLS
135         verify(topology, times(1)).enableNode(node1);
136
137         // remove context
138         tlsSessionContextManager.remove(ID1);
139         verify(topology, times(1)).disableNode(eq(NODE_ID1));
140     }
141
142 }