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