Merge "OPNFLWPLUG-929 : Remove deprecated guava library"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / translator / PacketReceivedTranslatorTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.translator;
9
10 import com.google.common.collect.Lists;
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.openflowplugin.api.OFConstants;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPort;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPortBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class PacketReceivedTranslatorTest {
56
57     @Mock
58     ConnectionContext connectionContext;
59     @Mock
60     FeaturesReply featuresReply;
61     @Mock
62     GetFeaturesOutput getFeaturesOutput;
63     @Mock
64     DeviceState deviceState;
65     @Mock
66     DataBroker dataBroker;
67     @Mock
68     DeviceContext deviceContext;
69     @Mock
70     DeviceInfo deviceInfo;
71     @Mock
72     PhyPort phyPort;
73
74     ConvertorManager convertorManager;
75
76     static final Long PORT_NO = 5L;
77     static final Long PORT_NO_DS = 6L;
78     static final String DATA = "Test_Data";
79     static final Long PORT_NUM_VALUE = 11L;
80
81     @Before
82     public void setUp() throws Exception {
83         final List<PhyPort> phyPorts = Collections.singletonList(phyPort);
84         convertorManager = ConvertorManagerFactory.createDefaultManager();
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(convertorManager);
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 PacketReceivedTranslator packetReceivedTranslator = new PacketReceivedTranslator(convertorManager);
148         final Match packetInMatch = packetReceivedTranslator.getPacketInMatch(inputBld.build(), dpid);
149
150         Assert.assertNotNull(packetInMatch.getInPort());
151         Assert.assertEquals("openflow:10:" + PORT_NUM_VALUE, packetInMatch.getInPort().getValue());
152     }
153
154     private static MatchEntryBuilder assembleMatchEntryBld(long portNumValue) {
155         MatchEntryBuilder matchEntryBuilder = prepareHeader(InPort.class, false);
156         InPortBuilder inPortBld = new InPortBuilder().setPortNumber(new PortNumber(portNumValue));
157         InPortCaseBuilder caseBuilder = new InPortCaseBuilder();
158         caseBuilder.setInPort(inPortBld.build());
159         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
160         return matchEntryBuilder;
161     }
162
163     private static MatchEntryBuilder prepareHeader(Class<? extends MatchField> oxmMatchField, boolean hasMask) {
164         MatchEntryBuilder builder = new MatchEntryBuilder();
165         builder.setOxmClass(OpenflowBasicClass.class);
166         builder.setOxmMatchField(oxmMatchField);
167         builder.setHasMask(hasMask);
168         return builder;
169     }
170 }