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