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