Bug 5540 - PacketOutConvertor
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowConvertorTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;\r
10 \r
11 import java.math.BigInteger;\r
12 import java.util.ArrayList;\r
13 import java.util.List;\r
14 import org.junit.Assert;\r
15 import org.junit.Test;\r
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;\r
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;\r
50 \r
51 /**\r
52  * @author michal.polkorab\r
53  *\r
54  */\r
55 public class FlowConvertorTest {\r
56 \r
57     /**\r
58      * Tests {@link FlowConvertor#toFlowModInputs(Flow, short, BigInteger)}\r
59      */\r
60     @Test\r
61     public void test() {\r
62         RemoveFlowInputBuilder flowBuilder = new RemoveFlowInputBuilder();\r
63         flowBuilder.setBarrier(false);\r
64         flowBuilder.setCookie(new FlowCookie(new BigInteger("4")));\r
65         flowBuilder.setCookieMask(new FlowCookie(new BigInteger("5")));\r
66         flowBuilder.setTableId((short) 6);\r
67         flowBuilder.setStrict(true);\r
68         flowBuilder.setIdleTimeout(50);\r
69         flowBuilder.setHardTimeout(500);\r
70         flowBuilder.setPriority(40);\r
71         flowBuilder.setBufferId(18L);\r
72         flowBuilder.setOutPort(new BigInteger("65535"));\r
73         flowBuilder.setOutGroup(5000L);\r
74         flowBuilder.setFlags(null);\r
75         flowBuilder.setMatch(null);\r
76         RemoveFlowInput flow = flowBuilder.build();\r
77 \r
78         List<FlowModInputBuilder> flowMod = FlowConvertor\r
79                 .toFlowModInputs(flow, EncodeConstants.OF13_VERSION_ID, new BigInteger("42"));\r
80 \r
81         Assert.assertEquals("Wrong version", 4, flowMod.get(0).getVersion().intValue());\r
82         Assert.assertEquals("Wrong cookie", 4, flowMod.get(0).getCookie().intValue());\r
83         Assert.assertEquals("Wrong cookie mask", 5, flowMod.get(0).getCookieMask().intValue());\r
84         Assert.assertEquals("Wrong table id", 6, flowMod.get(0).getTableId().getValue().intValue());\r
85         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCDELETESTRICT, flowMod.get(0).getCommand());\r
86         Assert.assertEquals("Wrong idle timeout", 50, flowMod.get(0).getIdleTimeout().intValue());\r
87         Assert.assertEquals("Wrong hard timeout", 500, flowMod.get(0).getHardTimeout().intValue());\r
88         Assert.assertEquals("Wrong priority", 40, flowMod.get(0).getPriority().intValue());\r
89         Assert.assertEquals("Wrong buffer id", 18, flowMod.get(0).getBufferId().intValue());\r
90         Assert.assertEquals("Wrong out port", 65535, flowMod.get(0).getOutPort().getValue().intValue());\r
91         Assert.assertEquals("Wrong out group", 5000, flowMod.get(0).getOutGroup().intValue());\r
92         Assert.assertEquals("Wrong flags", new FlowModFlags(false, false, false, false, false), flowMod.get(0).getFlags());\r
93         Assert.assertEquals("Wrong match", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType",\r
94                 flowMod.get(0).getMatch().getType().getName());\r
95         Assert.assertEquals("Wrong match entries size", 0, flowMod.get(0).getMatch().getMatchEntry().size());\r
96     }\r
97 \r
98     /**\r
99      * Tests {@link FlowConvertor#toFlowModInputs(Flow, short, BigInteger)}\r
100      */\r
101     @Test\r
102     public void testOnlyModifyStrictCommand() {\r
103         UpdatedFlowBuilder flowBuilder = new UpdatedFlowBuilder();\r
104         flowBuilder.setStrict(true);\r
105         UpdatedFlow flow = flowBuilder.build();\r
106 \r
107         List<FlowModInputBuilder> flowMod = FlowConvertor\r
108                 .toFlowModInputs(flow, EncodeConstants.OF10_VERSION_ID, new BigInteger("42"));\r
109 \r
110         Assert.assertEquals("Wrong version", 1, flowMod.get(0).getVersion().intValue());\r
111         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCADD, flowMod.get(0).getCommand());\r
112     }\r
113 \r
114     /**\r
115      * Tests {@link FlowConvertor#toFlowModInputs(Flow, short, BigInteger)}\r
116      */\r
117     @Test\r
118     public void testInstructionsTranslation() {\r
119         AddFlowInputBuilder flowBuilder = new AddFlowInputBuilder();\r
120         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();\r
121         List<Instruction> instructions = new ArrayList<>();\r
122         InstructionBuilder instructionBuilder = new InstructionBuilder();\r
123         GoToTableCaseBuilder goToCaseBuilder = new GoToTableCaseBuilder();\r
124         GoToTableBuilder goToBuilder = new GoToTableBuilder();\r
125         goToBuilder.setTableId((short) 1);\r
126         goToCaseBuilder.setGoToTable(goToBuilder.build());\r
127         instructionBuilder.setInstruction(goToCaseBuilder.build());\r
128         instructionBuilder.setOrder(0);\r
129         instructions.add(instructionBuilder.build());\r
130         instructionBuilder = new InstructionBuilder();\r
131         WriteMetadataCaseBuilder metaCaseBuilder = new WriteMetadataCaseBuilder();\r
132         WriteMetadataBuilder metaBuilder = new WriteMetadataBuilder();\r
133         metaBuilder.setMetadata(new BigInteger("2"));\r
134         metaBuilder.setMetadataMask(new BigInteger("3"));\r
135         metaCaseBuilder.setWriteMetadata(metaBuilder.build());\r
136         instructionBuilder.setInstruction(metaCaseBuilder.build());\r
137         instructionBuilder.setOrder(1);\r
138         instructions.add(instructionBuilder.build());\r
139         instructionBuilder = new InstructionBuilder();\r
140         WriteActionsCaseBuilder writeCaseBuilder = new WriteActionsCaseBuilder();\r
141         WriteActionsBuilder writeBuilder = new WriteActionsBuilder();\r
142         List<Action> actions = new ArrayList<>();\r
143         writeBuilder.setAction(actions);\r
144         writeCaseBuilder.setWriteActions(writeBuilder.build());\r
145         instructionBuilder.setInstruction(writeCaseBuilder.build());\r
146         instructionBuilder.setOrder(2);\r
147         instructions.add(instructionBuilder.build());\r
148         instructionBuilder = new InstructionBuilder();\r
149         ApplyActionsCaseBuilder applyCaseBuilder = new ApplyActionsCaseBuilder();\r
150         ApplyActionsBuilder applyBuilder = new ApplyActionsBuilder();\r
151         actions = new ArrayList<>();\r
152         applyBuilder.setAction(actions);\r
153         applyCaseBuilder.setApplyActions(applyBuilder.build());\r
154         instructionBuilder.setInstruction(applyCaseBuilder.build());\r
155         instructionBuilder.setOrder(3);\r
156         instructions.add(instructionBuilder.build());\r
157         instructionBuilder = new InstructionBuilder();\r
158         ClearActionsCaseBuilder clearCaseBuilder = new ClearActionsCaseBuilder();\r
159         ClearActionsBuilder clearBuilder = new ClearActionsBuilder();\r
160         actions = new ArrayList<>();\r
161         clearBuilder.setAction(actions);\r
162         clearCaseBuilder.setClearActions(clearBuilder.build());\r
163         instructionBuilder.setInstruction(clearCaseBuilder.build());\r
164         instructionBuilder.setOrder(4);\r
165         instructions.add(instructionBuilder.build());\r
166         instructionBuilder = new InstructionBuilder();\r
167         MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();\r
168         MeterBuilder meterBuilder = new MeterBuilder();\r
169         meterBuilder.setMeterId(new MeterId(5L));\r
170         meterCaseBuilder.setMeter(meterBuilder.build());\r
171         instructionBuilder.setInstruction(meterCaseBuilder.build());\r
172         instructionBuilder.setOrder(5);\r
173         instructions.add(instructionBuilder.build());\r
174         instructionsBuilder.setInstruction(instructions);\r
175         flowBuilder.setInstructions(instructionsBuilder.build());\r
176         AddFlowInput flow = flowBuilder.build();\r
177 \r
178         List<FlowModInputBuilder> flowMod = FlowConvertor\r
179                 .toFlowModInputs(flow, EncodeConstants.OF10_VERSION_ID, new BigInteger("42"));\r
180 \r
181         Assert.assertEquals("Wrong version", 1, flowMod.get(0).getVersion().intValue());\r
182         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCADD, flowMod.get(0).getCommand());\r
183         Assert.assertEquals("Wrong instructions size", 6, flowMod.get(0).getInstruction().size());\r
184         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions\r
185         .grouping.Instruction instruction = flowMod.get(0).getInstruction().get(0);\r
186         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
187                 + ".instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
188         GotoTableCase gotoTableCase = (GotoTableCase) instruction.getInstructionChoice();\r
189         Assert.assertEquals("Wrong table id", 1, gotoTableCase.getGotoTable().getTableId().intValue());\r
190         instruction = flowMod.get(0).getInstruction().get(1);\r
191         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
192                 + ".instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
193         WriteMetadataCase writeMetadataCase = (WriteMetadataCase) instruction.getInstructionChoice();\r
194         Assert.assertArrayEquals("Wrong metadata", new byte[]{0, 0, 0, 0, 0, 0, 0, 2},\r
195                 writeMetadataCase.getWriteMetadata().getMetadata());\r
196         Assert.assertArrayEquals("Wrong metadata mask", new byte[]{0, 0, 0, 0, 0, 0, 0, 3},\r
197                 writeMetadataCase.getWriteMetadata().getMetadataMask());\r
198         \r
199         instruction = flowMod.get(0).getInstruction().get(2);\r
200         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
201                 + ".instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
202         WriteActionsCase writeActionsCase = (WriteActionsCase) instruction.getInstructionChoice();\r
203         Assert.assertEquals("Wrong actions size", 0, writeActionsCase.getWriteActions().getAction().size());\r
204         instruction = flowMod.get(0).getInstruction().get(3);\r
205         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
206                 + ".instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
207         ApplyActionsCase applyActionsCase =  (ApplyActionsCase) instruction.getInstructionChoice();\r
208         Assert.assertEquals("Wrong actions size", 0, applyActionsCase.getApplyActions().getAction().size());\r
209         instruction = flowMod.get(0).getInstruction().get(4);\r
210         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
211                 + ".instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
212         instruction = flowMod.get(0).getInstruction().get(5);\r
213         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
214                 + ".instruction.rev130731.instruction.grouping.instruction.choice.MeterCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
215         MeterCase meterCase = (MeterCase) instruction.getInstructionChoice();\r
216         Assert.assertEquals("Wrong meter id", 5, meterCase.getMeter().getMeterId().intValue());\r
217     }\r
218 }