2 * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.netconf.topology.pipeline;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
16 import java.net.InetSocketAddress;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.netconf.api.NetconfTerminationReason;
24 import org.opendaylight.netconf.client.NetconfClientSession;
25 import org.opendaylight.netconf.client.NetconfClientSessionListener;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 public class ClusteredNetconfDeviceCommunicatorTest {
30 private static final RemoteDeviceId REMOTE_DEVICE_ID = new RemoteDeviceId("testing-device", new InetSocketAddress(9999));
33 private ClusteredNetconfDevice remoteDevice;
36 private EntityOwnershipService ownershipService;
39 private NetconfClientSession session;
42 private NetconfClientSessionListener listener1;
45 private NetconfClientSessionListener listener2;
48 private EntityOwnershipListenerRegistration ownershipListenerRegistration;
50 private ClusteredNetconfDeviceCommunicator communicator;
54 public void setUp() throws Exception {
55 MockitoAnnotations.initMocks(this);
57 doReturn(ownershipListenerRegistration).when(ownershipService).registerListener(
58 "netconf-node/" + REMOTE_DEVICE_ID.getName(), remoteDevice);
60 communicator = new ClusteredNetconfDeviceCommunicator(REMOTE_DEVICE_ID, remoteDevice, ownershipService, 10);
61 communicator.registerNetconfClientSessionListener(listener1);
62 communicator.registerNetconfClientSessionListener(listener2);
66 public void testOnSessionDown() {
67 communicator.onSessionUp(session);
69 Exception exception = mock(Exception.class);
70 communicator.onSessionDown(session, exception);
72 verify(ownershipListenerRegistration).close();
74 verify(listener1).onSessionDown(eq(session), eq(exception));
75 verify(listener2).onSessionDown(eq(session), eq(exception));
79 public void testOnSessionUp() {
80 communicator.onSessionUp(session);
82 verify(ownershipService).registerListener("netconf-node/" + REMOTE_DEVICE_ID.getName(), remoteDevice);
84 verify(listener1).onSessionUp(eq(session));
85 verify(listener2).onSessionUp(eq(session));
89 public void testOnSessionTerminated() {
90 communicator.onSessionUp(session);
92 NetconfTerminationReason reason = mock(NetconfTerminationReason.class);
93 communicator.onSessionTerminated(session, reason);
95 verify(ownershipListenerRegistration).close();
97 verify(listener1).onSessionTerminated(eq(session), eq(reason));
98 verify(listener2).onSessionTerminated(eq(session), eq(reason));