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