BUG-1422 Introduce Call-Home functionality for the NETCONF Topology.
[netconf.git] / netconf / callhome-provider / src / test / java / org / opendaylight / netconf / callhome / mount / CallHomeMountDispatcherTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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
9 package org.opendaylight.netconf.callhome.mount;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.mockito.Mockito.mock;
13
14 import io.netty.util.concurrent.EventExecutor;
15 import io.netty.util.concurrent.Future;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.net.UnknownHostException;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
22 import org.opendaylight.controller.config.threadpool.ThreadPool;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
26 import org.opendaylight.controller.sal.core.api.Broker;
27 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
28 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
29 import org.opendaylight.netconf.client.NetconfClientSession;
30 import org.opendaylight.netconf.client.NetconfClientSessionListener;
31 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
32 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
33 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
34 import org.opendaylight.netconf.topology.api.SchemaRepositoryProvider;
35 import org.opendaylight.protocol.framework.ReconnectStrategy;
36
37 public class CallHomeMountDispatcherTest {
38     private String topologyId;
39     private BindingAwareBroker mockBroker;
40     private EventExecutor mockExecutor;
41     private ScheduledThreadPool mockKeepAlive;
42     private ThreadPool mockProcessingExecutor;
43     private SchemaRepositoryProvider mockSchemaRepoProvider;
44     private Broker mockDomBroker;
45
46     private CallHomeMountDispatcher instance;
47     private DataBroker mockDataBroker;
48     private DOMMountPointService mockMount;
49
50     @Before
51     public void setup() {
52         topologyId = "";
53         mockBroker = mock(BindingAwareBroker.class);
54         mockExecutor = mock(EventExecutor.class);
55         mockKeepAlive = mock(ScheduledThreadPool.class);
56         mockProcessingExecutor = mock(ThreadPool.class);
57         mockSchemaRepoProvider = mock(SchemaRepositoryProvider.class);
58         mockDomBroker = mock(Broker.class);
59         mockDataBroker = mock(DataBroker.class);
60         mockMount = mock(DOMMountPointService.class);
61         instance = new CallHomeMountDispatcher(topologyId, mockBroker, mockExecutor, mockKeepAlive,
62                 mockProcessingExecutor, mockSchemaRepoProvider, mockDomBroker, mockDataBroker, mockMount);
63     }
64
65     NetconfClientConfiguration someConfiguration(InetSocketAddress address) {
66         // NetconfClientConfiguration has mostly final methods, making it un-mock-able
67
68         NetconfClientConfiguration.NetconfClientProtocol protocol =
69                 NetconfClientConfiguration.NetconfClientProtocol.SSH;
70         NetconfHelloMessageAdditionalHeader additionalHeader = mock(NetconfHelloMessageAdditionalHeader.class);
71         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
72         ReconnectStrategy reconnectStrategy = mock(ReconnectStrategy.class);
73         AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
74
75         return NetconfClientConfigurationBuilder.create().withProtocol(protocol).withAddress(address)
76                 .withConnectionTimeoutMillis(0).withAdditionalHeader(additionalHeader)
77                 .withSessionListener(sessionListener).withReconnectStrategy(reconnectStrategy)
78                 .withAuthHandler(authHandler).build();
79     }
80
81     @Test
82     public void canCreateASessionFromAConfiguration() {
83         // given
84         CallHomeMountSessionContext mockContext = mock(CallHomeMountSessionContext.class);
85         InetSocketAddress someAddress = InetSocketAddress.createUnresolved("1.2.3.4", 123);
86         // instance.contextByAddress.put(someAddress, mockContext);
87
88         NetconfClientConfiguration someCfg = someConfiguration(someAddress);
89         // when
90         instance.createClient(someCfg);
91         // then
92         // verify(mockContext, times(1)).activate(any(NetconfClientSessionListener.class));
93     }
94
95     @Test
96     public void noSessionIsCreatedWithoutAContextAvailableForAGivenAddress() {
97         // given
98         InetSocketAddress someAddress = InetSocketAddress.createUnresolved("1.2.3.4", 123);
99         NetconfClientConfiguration someCfg = someConfiguration(someAddress);
100         // when
101         Future<NetconfClientSession> future = instance.createClient(someCfg);
102         // then
103         assertFalse(future.isSuccess());
104     }
105
106     @Test
107     public void nodeIsInsertedIntoTopologyWhenSubsystemIsOpened() throws UnknownHostException {
108         // given
109         InetSocketAddress someAddress = new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 123);
110         CallHomeChannelActivator activator = mock(CallHomeChannelActivator.class);
111         // instance.topology = mock(CallHomeTopology.class);
112         // when
113         // instance.onNetconfSubsystemOpened(someAddress, activator);
114         // then
115         // verify(instance.topology, times(1)).connectNode(any(NodeId.class), any(Node.class));
116     }
117 }