Fix "Bug 5589 - Deprecate PortNumberCache"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / NodeConnectorRefToPortTranslatorTest.java
1 package org.opendaylight.openflowplugin.impl.util;
2
3 import junit.framework.TestCase;
4 import org.junit.Before;
5 import org.junit.Rule;
6 import org.junit.Test;
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;
23
24 import static org.mockito.Mockito.*;
25
26 import java.math.BigInteger;
27 import java.util.Arrays;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /**
32  * Created by Tomas Slusny on 24.3.2016.
33  */
34 @RunWith(MockitoJUnitRunner.class)
35 public class NodeConnectorRefToPortTranslatorTest extends TestCase {
36
37     @Mock
38     DeviceState deviceState;
39     @Mock
40     KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
41     @Mock
42     GetFeaturesOutput getFeaturesOutput;
43     @Mock
44     List<PhyPort> phyPorts;
45     @Mock
46     Iterator<PhyPort> phyPortsIterator;
47     @Mock
48     PhyPort phyPort;
49     @Mock
50     PhyPort phyPort2;
51
52
53     @Mock
54     DeviceState secondDeviceState;
55     @Mock
56     GetFeaturesOutput secondGetFeaturesOutput;
57     @Mock
58     PhyPort secondPhyPort;
59
60     static final Long PORT_NO = 5l;
61     static final Long SECOND_PORT_NO = 6l;
62     static final String ID_VALUE = "openflow:10";
63
64     @Before
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
68                 .create(Nodes.class)
69                 .child(Node.class, new NodeKey(new NodeId(ID_VALUE)));
70
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);
80
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);
89
90
91         OpenflowPortsUtil.init();
92     }
93
94     @Test(expected = NullPointerException.class)
95     public void testForNotNullableDeviceStateInGetPortNo() throws Exception {
96         NodeConnectorRefToPortTranslator.getPortNoFromDeviceState(null);
97     }
98
99     @Test(expected = NullPointerException.class)
100     public void testForNotNullableDeviceStateInToNodeConnectorRef() throws Exception {
101         NodeConnectorRefToPortTranslator.toNodeConnectorRef(null);
102     }
103
104     @Test(expected = NullPointerException.class)
105     public void testForNotNullableDeviceStateInFromNodeConnectorRef() throws Exception {
106         NodeConnectorRefToPortTranslator.fromNodeConnectorRef(null, null);
107     }
108
109     @Test
110     public void testGetPortNoFromDeviceState() throws Exception {
111         Long portNo = NodeConnectorRefToPortTranslator.getPortNoFromDeviceState(deviceState);
112         assertEquals(portNo, PORT_NO);
113     }
114
115     @Test
116     public void testNodeConnectorConversion() throws Exception {
117         // Convert DeviceState to NodeConnectorRef
118         NodeConnectorRef ref = NodeConnectorRefToPortTranslator.toNodeConnectorRef(deviceState);
119
120         // Test getting port from NodeConnectorRef
121         Long refPort = NodeConnectorRefToPortTranslator.fromNodeConnectorRef(deviceState, ref);
122         assertEquals(PORT_NO, refPort);
123
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);
127
128         // Test fallback to device state if there is any problem with NodeConnectorRef
129         refPort = NodeConnectorRefToPortTranslator.fromNodeConnectorRef(deviceState, null);
130         assertEquals(PORT_NO, refPort);
131
132         // Check if 2 NodeConnectorRef created from same DeviceState have same value
133         assertEquals(ref, NodeConnectorRefToPortTranslator.toNodeConnectorRef(deviceState));
134     }
135 }