2 * Copyright (c) 2015 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.openflowplugin.impl.connection.listener;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import java.net.InetSocketAddress;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Captor;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.connection.DeviceConnectionStatusProvider;
30 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
32 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint64;
39 import org.opendaylight.yangtools.yang.common.Uint8;
42 * Test for {@link HandshakeListenerImpl}.
44 @RunWith(MockitoJUnitRunner.class)
45 public class HandshakeListenerImplTest {
48 private DeviceConnectedHandler deviceConnectedHandler;
50 private GetFeaturesOutput features;
52 private ConnectionAdapter connectionAdapter;
54 private HandshakeContext handshakeContext;
56 private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
58 private ArgumentCaptor<NodeId> nodeIdCaptor;
60 private ConnectionContext connectionContextSpy;
61 private HandshakeListenerImpl handshakeListener;
65 when(connectionAdapter.barrier(any()))
66 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
67 connectionContextSpy = spy(new ConnectionContextImpl(connectionAdapter, deviceConnectionStatusProvider));
68 when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
69 when(features.getDatapathId()).thenReturn(Uint64.TEN);
70 when(features.getVersion()).thenReturn(Uint8.ONE);
71 handshakeListener = new HandshakeListenerImpl(connectionContextSpy, deviceConnectedHandler);
72 handshakeListener.setHandshakeContext(handshakeContext);
76 public void tearDown() {
77 verify(handshakeContext).close();
81 public void testOnHandshakeSuccessfull() {
82 handshakeListener.onHandshakeSuccessful(features, OFConstants.OFP_VERSION_1_3);
83 verify(connectionContextSpy).changeStateToWorking();
84 verify(connectionContextSpy).setFeatures(any(FeaturesReply.class));
85 verify(connectionContextSpy).setNodeId(nodeIdCaptor.capture());
86 verify(connectionContextSpy).handshakeSuccessful();
87 verify(deviceConnectedHandler).deviceConnected(connectionContextSpy);
88 verify(handshakeContext).close();
90 assertEquals("openflow:10", nodeIdCaptor.getValue().getValue());
94 public void testOnHandshakeFailure1() {
95 connectionContextSpy.setNodeId(new NodeId("ut-device:10"));
96 handshakeListener.onHandshakeFailure();
97 verify(handshakeContext).close();
98 verify(connectionContextSpy).closeConnection(false);
102 public void testOnHandshakeFailure2() {
103 when(connectionAdapter.getRemoteAddress())
104 .thenReturn(InetSocketAddress.createUnresolved("ut-ofp.example.org", 4242));
105 connectionContextSpy.setNodeId(new NodeId("openflow:1"));
106 handshakeListener.onHandshakeFailure();
107 verify(handshakeContext).close();
108 verify(connectionContextSpy).closeConnection(false);