62bbe22aa9b2153d0f92e59f806dda05198f23d9
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / translator / PacketReceivedTranslatorTest.java
1 package org.opendaylight.openflowplugin.impl.translator;
2
3 import com.google.common.collect.Lists;
4 import java.math.BigInteger;
5 import java.util.Arrays;
6 import java.util.List;
7 import org.junit.Assert;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
12 import org.mockito.Mockito;
13 import org.mockito.runners.MockitoJUnitRunner;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
20 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPort;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPortBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match;
44 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45
46 /**
47  * Created by tkubas on 4/1/15.
48  */
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class PacketReceivedTranslatorTest {
52
53     @Mock
54     ConnectionContext connectionContext;
55     @Mock
56     FeaturesReply featuresReply;
57     @Mock
58     GetFeaturesOutput getFeaturesOutput;
59     @Mock
60     DeviceState deviceState;
61     @Mock
62     DataBroker dataBroker;
63     @Mock
64     DeviceContext deviceContext;
65     @Mock
66     DeviceInfo deviceInfo;
67     @Mock
68     List<PhyPort> phyPorts;
69     @Mock
70     PhyPort phyPort;
71
72     static final Long PORT_NO = 5l;
73     static final Long PORT_NO_DS = 6l;
74     static final String DATA = "Test_Data";
75     static final Long PORT_NUM_VALUE = 11l;
76
77     public PacketReceivedTranslatorTest() {
78         OpenflowPortsUtil.init();
79     }
80
81     @Before
82     public void setUp() throws Exception {
83         final List<PhyPort> phyPorts = Arrays.asList(phyPort);
84
85         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
86         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
87         Mockito.when(featuresReply.getDatapathId()).thenReturn(BigInteger.TEN);
88         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
89         Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
90         Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
91         Mockito.when(getFeaturesOutput.getDatapathId()).thenReturn(BigInteger.TEN);
92         Mockito.when(getFeaturesOutput.getPhyPort()).thenReturn(phyPorts);
93         Mockito.when(phyPort.getPortNo()).thenReturn(PORT_NO_DS);
94     }
95
96     @Test
97     public void testTranslate() throws Exception {
98         final KeyedInstanceIdentifier<Node, NodeKey> nodePath = KeyedInstanceIdentifier
99                 .create(Nodes.class)
100                 .child(Node.class, new NodeKey(new NodeId("openflow:10")));
101         final PacketReceivedTranslator packetReceivedTranslator = new PacketReceivedTranslator();
102         final PacketInMessage packetInMessage = createPacketInMessage(DATA.getBytes(), PORT_NO);
103         Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
104
105         final PacketReceived packetReceived = packetReceivedTranslator.translate(packetInMessage, deviceInfo, null);
106
107         Assert.assertArrayEquals(packetInMessage.getData(), packetReceived.getPayload());
108         Assert.assertEquals("org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.SendToController",
109                 packetReceived.getPacketInReason().getName());
110         Assert.assertEquals("openflow:10:" + PORT_NO,
111                 packetReceived.getIngress().getValue().firstKeyOf(NodeConnector.class, NodeConnectorKey.class)
112                         .getId().getValue());
113         Assert.assertEquals(0L, packetReceived.getFlowCookie().getValue().longValue());
114         Assert.assertEquals(42L, packetReceived.getTableId().getValue().longValue());
115     }
116
117     private static PacketInMessage createPacketInMessage(final byte[] data,
118                                                          final long port) {
119         final PacketInReason reason = PacketInReason.OFPRACTION;
120
121         MatchEntryBuilder matchEntryBuilder = assembleMatchEntryBld(port);
122         MatchBuilder packetInMatchBld = new MatchBuilder()
123                 .setMatchEntry(Lists.newArrayList(matchEntryBuilder.build()));
124
125         return new PacketInMessageBuilder()
126                 .setVersion(OFConstants.OFP_VERSION_1_0)
127                 .setData(data).setReason(reason)
128                 .setMatch(packetInMatchBld.build())
129                 .setVersion(OFConstants.OFP_VERSION_1_3)
130                 .setCookie(BigInteger.ZERO)
131                 .setTableId(new TableId(42L))
132                 .build();
133
134     }
135
136     @Test
137     public void testGetPacketInMatch() throws Exception {
138         MatchEntryBuilder matchEntryBuilder = assembleMatchEntryBld(PORT_NUM_VALUE);
139         MatchBuilder packetInMatchBld = new MatchBuilder()
140                 .setMatchEntry(Lists.newArrayList(matchEntryBuilder.build()));
141         PacketInMessageBuilder inputBld = new PacketInMessageBuilder()
142                 .setMatch(packetInMatchBld.build())
143                 .setVersion(OFConstants.OFP_VERSION_1_3);
144         BigInteger dpid = BigInteger.TEN;
145
146         final Match packetInMatch = PacketReceivedTranslator.getPacketInMatch(inputBld.build(), dpid);
147
148         Assert.assertNotNull(packetInMatch.getInPort());
149         Assert.assertEquals("openflow:10:" + PORT_NUM_VALUE, packetInMatch.getInPort().getValue());
150     }
151
152     private static MatchEntryBuilder assembleMatchEntryBld(long portNumValue) {
153         MatchEntryBuilder matchEntryBuilder = prepareHeader(InPort.class, false);
154         InPortBuilder inPortBld = new InPortBuilder().setPortNumber(new PortNumber(portNumValue));
155         InPortCaseBuilder caseBuilder = new InPortCaseBuilder();
156         caseBuilder.setInPort(inPortBld.build());
157         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
158         return matchEntryBuilder;
159     }
160
161     private static MatchEntryBuilder prepareHeader(Class<? extends MatchField> oxmMatchField, boolean hasMask) {
162         MatchEntryBuilder builder = new MatchEntryBuilder();
163         builder.setOxmClass(OpenflowBasicClass.class);
164         builder.setOxmMatchField(oxmMatchField);
165         builder.setHasMask(hasMask);
166         return builder;
167     }
168 }