1 package org.opendaylight.openflowplugin.impl.util;
3 import junit.framework.TestCase;
4 import org.junit.Before;
7 import org.junit.rules.ExpectedException;
8 import org.junit.runner.RunWith;
9 import org.mockito.Mock;
10 import org.mockito.runners.MockitoJUnitRunner;
11 import org.opendaylight.openflowplugin.api.OFConstants;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
13 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
22 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
24 import static org.mockito.Mockito.*;
26 import java.math.BigInteger;
27 import java.util.Arrays;
28 import java.util.Iterator;
29 import java.util.List;
32 * Created by Tomas Slusny on 24.3.2016.
34 @RunWith(MockitoJUnitRunner.class)
35 public class NodeConnectorRefToPortTranslatorTest extends TestCase {
38 DeviceState deviceState;
40 KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
42 GetFeaturesOutput getFeaturesOutput;
44 List<PhyPort> phyPorts;
46 Iterator<PhyPort> phyPortsIterator;
54 DeviceState secondDeviceState;
56 GetFeaturesOutput secondGetFeaturesOutput;
58 PhyPort secondPhyPort;
60 static final Long PORT_NO = 5l;
61 static final Long SECOND_PORT_NO = 6l;
62 static final String ID_VALUE = "openflow:10";
65 public void setUp() throws Exception {
66 // Create nodePath (we cannot mock it in regular way because KeyedInstanceIdentifier.getKey() is final)
67 final KeyedInstanceIdentifier<Node, NodeKey> nodePath = KeyedInstanceIdentifier
69 .child(Node.class, new NodeKey(new NodeId(ID_VALUE)));
71 // Mock first device state
72 final List<PhyPort> phyPorts = Arrays.asList(null, phyPort2, phyPort);
73 when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
74 when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
75 when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
76 when(getFeaturesOutput.getDatapathId()).thenReturn(BigInteger.TEN);
77 when(getFeaturesOutput.getPhyPort()).thenReturn(phyPorts);
78 when(phyPort.getPortNo()).thenReturn(PORT_NO);
79 when(phyPort2.getPortNo()).thenReturn(null);
81 // Mock second device state
82 final List<PhyPort> secondPhyPorts = Arrays.asList(phyPort);
83 when(secondDeviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
84 when(secondDeviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
85 when(secondDeviceState.getFeatures()).thenReturn(secondGetFeaturesOutput);
86 when(secondGetFeaturesOutput.getDatapathId()).thenReturn(BigInteger.TEN);
87 when(secondGetFeaturesOutput.getPhyPort()).thenReturn(secondPhyPorts);
88 when(secondPhyPort.getPortNo()).thenReturn(SECOND_PORT_NO);
91 OpenflowPortsUtil.init();
94 @Test(expected = NullPointerException.class)
95 public void testForNotNullableDeviceStateInGetPortNo() throws Exception {
96 NodeConnectorRefToPortTranslator.getPortNoFromDeviceState(null);
99 @Test(expected = NullPointerException.class)
100 public void testForNotNullableDeviceStateInToNodeConnectorRef() throws Exception {
101 NodeConnectorRefToPortTranslator.toNodeConnectorRef(null);
104 @Test(expected = NullPointerException.class)
105 public void testForNotNullableDeviceStateInFromNodeConnectorRef() throws Exception {
106 NodeConnectorRefToPortTranslator.fromNodeConnectorRef(null, null);
110 public void testGetPortNoFromDeviceState() throws Exception {
111 Long portNo = NodeConnectorRefToPortTranslator.getPortNoFromDeviceState(deviceState);
112 assertEquals(portNo, PORT_NO);
116 public void testNodeConnectorConversion() throws Exception {
117 // Convert DeviceState to NodeConnectorRef
118 NodeConnectorRef ref = NodeConnectorRefToPortTranslator.toNodeConnectorRef(deviceState);
120 // Test getting port from NodeConnectorRef
121 Long refPort = NodeConnectorRefToPortTranslator.fromNodeConnectorRef(deviceState, ref);
122 assertEquals(PORT_NO, refPort);
124 // Test for getting same port, even when we used different DeviceState as fallback
125 Long secondPort = NodeConnectorRefToPortTranslator.fromNodeConnectorRef(secondDeviceState, ref);
126 assertEquals(refPort, secondPort);
128 // Test fallback to device state if there is any problem with NodeConnectorRef
129 refPort = NodeConnectorRefToPortTranslator.fromNodeConnectorRef(deviceState, null);
130 assertEquals(PORT_NO, refPort);
132 // Check if 2 NodeConnectorRef created from same DeviceState have same value
133 assertEquals(ref, NodeConnectorRefToPortTranslator.toNodeConnectorRef(deviceState));