f6358fad05462f83e37d6ae8eaf8eebcd4c374cf
[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
8 import org.junit.Assert;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.mockito.Mock;
13 import org.mockito.Mockito;
14 import org.mockito.runners.MockitoJUnitRunner;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
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     List<PhyPort> phyPorts;
67     @Mock
68     PhyPort phyPort;
69
70     static final Long PORT_NO = 5l;
71     static final Long PORT_NO_DS = 6l;
72     static final String DATA = "Test_Data";
73     static final Long PORT_NUM_VALUE = 11l;
74
75     public PacketReceivedTranslatorTest() {
76         OpenflowPortsUtil.init();
77     }
78
79     @Before
80     public void setUp() throws Exception {
81         final List<PhyPort> phyPorts = Arrays.asList(phyPort);
82
83         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
84         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
85         Mockito.when(featuresReply.getDatapathId()).thenReturn(BigInteger.TEN);
86         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
87         Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
88         Mockito.when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
89         Mockito.when(getFeaturesOutput.getDatapathId()).thenReturn(BigInteger.TEN);
90         Mockito.when(getFeaturesOutput.getPhyPort()).thenReturn(phyPorts);
91         Mockito.when(phyPort.getPortNo()).thenReturn(PORT_NO_DS);
92     }
93
94     @Test
95     public void testTranslate() throws Exception {
96         final KeyedInstanceIdentifier<Node, NodeKey> nodePath = KeyedInstanceIdentifier
97                 .create(Nodes.class)
98                 .child(Node.class, new NodeKey(new NodeId("openflow:10")));
99         final PacketReceivedTranslator packetReceivedTranslator = new PacketReceivedTranslator();
100         final PacketInMessage packetInMessage = createPacketInMessage(DATA.getBytes(), PORT_NO);
101         Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
102
103         final PacketReceived packetReceived = packetReceivedTranslator.translate(packetInMessage, deviceState, null);
104
105         Assert.assertArrayEquals(packetInMessage.getData(), packetReceived.getPayload());
106         Assert.assertEquals("org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.SendToController",
107                 packetReceived.getPacketInReason().getName());
108         Assert.assertEquals("openflow:10:" + PORT_NO,
109                 packetReceived.getIngress().getValue().firstKeyOf(NodeConnector.class, NodeConnectorKey.class)
110                         .getId().getValue());
111         Assert.assertEquals(0L, packetReceived.getFlowCookie().getValue().longValue());
112         Assert.assertEquals(42L, packetReceived.getTableId().getValue().longValue());
113     }
114
115     private static PacketInMessage createPacketInMessage(final byte[] data,
116                                                          final long port) {
117         final PacketInReason reason = PacketInReason.OFPRACTION;
118
119         MatchEntryBuilder matchEntryBuilder = assembleMatchEntryBld(port);
120         MatchBuilder packetInMatchBld = new MatchBuilder()
121                 .setMatchEntry(Lists.newArrayList(matchEntryBuilder.build()));
122
123         return new PacketInMessageBuilder()
124                 .setVersion(OFConstants.OFP_VERSION_1_0)
125                 .setData(data).setReason(reason)
126                 .setMatch(packetInMatchBld.build())
127                 .setVersion(OFConstants.OFP_VERSION_1_3)
128                 .setCookie(BigInteger.ZERO)
129                 .setTableId(new TableId(42L))
130                 .build();
131
132     }
133
134     @Test
135     public void testGetPacketInMatch() throws Exception {
136         MatchEntryBuilder matchEntryBuilder = assembleMatchEntryBld(PORT_NUM_VALUE);
137         MatchBuilder packetInMatchBld = new MatchBuilder()
138                 .setMatchEntry(Lists.newArrayList(matchEntryBuilder.build()));
139         PacketInMessageBuilder inputBld = new PacketInMessageBuilder()
140                 .setMatch(packetInMatchBld.build())
141                 .setVersion(OFConstants.OFP_VERSION_1_3);
142         BigInteger dpid = BigInteger.TEN;
143
144         final Match packetInMatch = PacketReceivedTranslator.getPacketInMatch(inputBld.build(), dpid);
145
146         Assert.assertNotNull(packetInMatch.getInPort());
147         Assert.assertEquals("openflow:10:" + PORT_NUM_VALUE, packetInMatch.getInPort().getValue());
148     }
149
150     private static MatchEntryBuilder assembleMatchEntryBld(long portNumValue) {
151         MatchEntryBuilder matchEntryBuilder = prepareHeader(InPort.class, false);
152         InPortBuilder inPortBld = new InPortBuilder().setPortNumber(new PortNumber(portNumValue));
153         InPortCaseBuilder caseBuilder = new InPortCaseBuilder();
154         caseBuilder.setInPort(inPortBld.build());
155         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
156         return matchEntryBuilder;
157     }
158
159     private static MatchEntryBuilder prepareHeader(Class<? extends MatchField> oxmMatchField, boolean hasMask) {
160         MatchEntryBuilder builder = new MatchEntryBuilder();
161         builder.setOxmClass(OpenflowBasicClass.class);
162         builder.setOxmMatchField(oxmMatchField);
163         builder.setHasMask(hasMask);
164         return builder;
165     }
166 }