Fix bug 1941 - Deleting of flows very slow with large number of flows
[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 \r
15 import org.junit.Assert;\r
16 import org.junit.Test;\r
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;\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.FlowModInput;\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#toFlowModInput(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         FlowModInput flowMod = FlowConvertor\r
79                 .toFlowModInput(flow, EncodeConstants.OF13_VERSION_ID, new BigInteger("42")).build();\r
80 \r
81         Assert.assertEquals("Wrong version", 4, flowMod.getVersion().intValue());\r
82         Assert.assertEquals("Wrong cookie", 4, flowMod.getCookie().intValue());\r
83         Assert.assertEquals("Wrong cookie mask", 5, flowMod.getCookieMask().intValue());\r
84         Assert.assertEquals("Wrong table id", 6, flowMod.getTableId().getValue().intValue());\r
85         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCDELETESTRICT, flowMod.getCommand());\r
86         Assert.assertEquals("Wrong idle timeout", 50, flowMod.getIdleTimeout().intValue());\r
87         Assert.assertEquals("Wrong hard timeout", 500, flowMod.getHardTimeout().intValue());\r
88         Assert.assertEquals("Wrong priority", 40, flowMod.getPriority().intValue());\r
89         Assert.assertEquals("Wrong buffer id", 18, flowMod.getBufferId().intValue());\r
90         Assert.assertEquals("Wrong out port", 65535, flowMod.getOutPort().getValue().intValue());\r
91         Assert.assertEquals("Wrong out group", 5000, flowMod.getOutGroup().intValue());\r
92         Assert.assertEquals("Wrong flags", new FlowModFlags(false, false, false, false, false), flowMod.getFlags());\r
93         Assert.assertEquals("Wrong match", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType",\r
94                 flowMod.getMatch().getType().getName());\r
95         Assert.assertEquals("Wrong match entries size", 0, flowMod.getMatch().getMatchEntries().size());\r
96     }\r
97 \r
98     /**\r
99      * Tests {@link FlowConvertor#toFlowModInput(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         FlowModInput flowMod = FlowConvertor\r
108                 .toFlowModInput(flow, EncodeConstants.OF10_VERSION_ID, new BigInteger("42")).build();\r
109 \r
110         Assert.assertEquals("Wrong version", 1, flowMod.getVersion().intValue());\r
111         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCMODIFYSTRICT, flowMod.getCommand());\r
112     }\r
113 \r
114     /**\r
115      * Tests {@link FlowConvertor#toFlowModInput(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         FlowModInput flowMod = FlowConvertor\r
179                 .toFlowModInput(flow, EncodeConstants.OF10_VERSION_ID, new BigInteger("42")).build();\r
180 \r
181         Assert.assertEquals("Wrong version", 1, flowMod.getVersion().intValue());\r
182         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCADD, flowMod.getCommand());\r
183         Assert.assertEquals("Wrong instructions size", 6, flowMod.getInstruction().size());\r
184         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions\r
185         .grouping.Instruction instruction = flowMod.getInstruction().get(0);\r
186         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
187                 + ".instruction.rev130731.GotoTable", instruction.getType().getName());\r
188         Assert.assertEquals("Wrong table id", 1, instruction.getAugmentation(TableIdInstruction.class)\r
189                 .getTableId().intValue());\r
190         instruction = flowMod.getInstruction().get(1);\r
191         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
192                 + ".instruction.rev130731.WriteMetadata", instruction.getType().getName());\r
193         Assert.assertArrayEquals("Wrong metadata", new byte[]{0, 0, 0, 0, 0, 0, 0, 2},\r
194                 instruction.getAugmentation(MetadataInstruction.class).getMetadata());\r
195         Assert.assertArrayEquals("Wrong metadata mask", new byte[]{0, 0, 0, 0, 0, 0, 0, 3},\r
196                 instruction.getAugmentation(MetadataInstruction.class).getMetadataMask());\r
197         instruction = flowMod.getInstruction().get(2);\r
198         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
199                 + ".instruction.rev130731.WriteActions", instruction.getType().getName());\r
200         Assert.assertEquals("Wrong actions size", 0, instruction.getAugmentation(ActionsInstruction.class)\r
201                 .getAction().size());\r
202         instruction = flowMod.getInstruction().get(3);\r
203         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
204                 + ".instruction.rev130731.ApplyActions", instruction.getType().getName());\r
205         Assert.assertEquals("Wrong actions size", 0, instruction.getAugmentation(ActionsInstruction.class)\r
206                 .getAction().size());\r
207         instruction = flowMod.getInstruction().get(4);\r
208         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
209                 + ".instruction.rev130731.ClearActions", instruction.getType().getName());\r
210         instruction = flowMod.getInstruction().get(5);\r
211         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
212                 + ".instruction.rev130731.Meter", instruction.getType().getName());\r
213         Assert.assertEquals("Wrong meter id", 5, instruction.getAugmentation(MeterIdInstruction.class)\r
214                 .getMeterId().intValue());\r
215     }\r
216 }