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