383cd9c47b4f6355479023a7cbf94f4ec1fe1f18
[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.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;\r
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;\r
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;\r
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;\r
52 \r
53 /**\r
54  * @author michal.polkorab\r
55  *\r
56  */\r
57 public class FlowConvertorTest {\r
58 \r
59     /**\r
60      * Tests {@link FlowConvertor#toFlowModInputs(Flow, short, BigInteger)}\r
61      */\r
62     @Test\r
63     public void test() {\r
64         RemoveFlowInputBuilder flowBuilder = new RemoveFlowInputBuilder();\r
65         flowBuilder.setBarrier(false);\r
66         flowBuilder.setCookie(new FlowCookie(new BigInteger("4")));\r
67         flowBuilder.setCookieMask(new FlowCookie(new BigInteger("5")));\r
68         flowBuilder.setTableId((short) 6);\r
69         flowBuilder.setStrict(true);\r
70         flowBuilder.setIdleTimeout(50);\r
71         flowBuilder.setHardTimeout(500);\r
72         flowBuilder.setPriority(40);\r
73         flowBuilder.setBufferId(18L);\r
74         flowBuilder.setOutPort(new BigInteger("65535"));\r
75         flowBuilder.setOutGroup(5000L);\r
76         flowBuilder.setFlags(null);\r
77         flowBuilder.setMatch(null);\r
78         RemoveFlowInput flow = flowBuilder.build();\r
79 \r
80         List<FlowModInputBuilder> flowMod = FlowConvertor\r
81                 .toFlowModInputs(flow, EncodeConstants.OF13_VERSION_ID, new BigInteger("42"));\r
82 \r
83         Assert.assertEquals("Wrong version", 4, flowMod.get(0).getVersion().intValue());\r
84         Assert.assertEquals("Wrong cookie", 4, flowMod.get(0).getCookie().intValue());\r
85         Assert.assertEquals("Wrong cookie mask", 5, flowMod.get(0).getCookieMask().intValue());\r
86         Assert.assertEquals("Wrong table id", 6, flowMod.get(0).getTableId().getValue().intValue());\r
87         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCDELETESTRICT, flowMod.get(0).getCommand());\r
88         Assert.assertEquals("Wrong idle timeout", 50, flowMod.get(0).getIdleTimeout().intValue());\r
89         Assert.assertEquals("Wrong hard timeout", 500, flowMod.get(0).getHardTimeout().intValue());\r
90         Assert.assertEquals("Wrong priority", 40, flowMod.get(0).getPriority().intValue());\r
91         Assert.assertEquals("Wrong buffer id", 18, flowMod.get(0).getBufferId().intValue());\r
92         Assert.assertEquals("Wrong out port", 65535, flowMod.get(0).getOutPort().getValue().intValue());\r
93         Assert.assertEquals("Wrong out group", 5000, flowMod.get(0).getOutGroup().intValue());\r
94         Assert.assertEquals("Wrong flags", new FlowModFlags(false, false, false, false, false), flowMod.get(0).getFlags());\r
95         Assert.assertEquals("Wrong match", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType",\r
96                 flowMod.get(0).getMatch().getType().getName());\r
97         Assert.assertEquals("Wrong match entries size", 0, flowMod.get(0).getMatch().getMatchEntry().size());\r
98     }\r
99 \r
100     /**\r
101      * Tests {@link FlowConvertor#toFlowModInputs(Flow, short, BigInteger)}\r
102      */\r
103     @Test\r
104     public void testOnlyModifyStrictCommand() {\r
105         UpdatedFlowBuilder flowBuilder = new UpdatedFlowBuilder();\r
106         flowBuilder.setStrict(true);\r
107         UpdatedFlow flow = flowBuilder.build();\r
108 \r
109         List<FlowModInputBuilder> flowMod = FlowConvertor\r
110                 .toFlowModInputs(flow, EncodeConstants.OF10_VERSION_ID, new BigInteger("42"));\r
111 \r
112         Assert.assertEquals("Wrong version", 1, flowMod.get(0).getVersion().intValue());\r
113         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCMODIFYSTRICT, flowMod.get(0).getCommand());\r
114     }\r
115 \r
116     /**\r
117      * Tests {@link FlowConvertor#toFlowModInputs(Flow, short, BigInteger)}\r
118      */\r
119     @Test\r
120     public void testInstructionsTranslation() {\r
121         AddFlowInputBuilder flowBuilder = new AddFlowInputBuilder();\r
122         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();\r
123         List<Instruction> instructions = new ArrayList<>();\r
124         InstructionBuilder instructionBuilder = new InstructionBuilder();\r
125         GoToTableCaseBuilder goToCaseBuilder = new GoToTableCaseBuilder();\r
126         GoToTableBuilder goToBuilder = new GoToTableBuilder();\r
127         goToBuilder.setTableId((short) 1);\r
128         goToCaseBuilder.setGoToTable(goToBuilder.build());\r
129         instructionBuilder.setInstruction(goToCaseBuilder.build());\r
130         instructionBuilder.setOrder(0);\r
131         instructions.add(instructionBuilder.build());\r
132         instructionBuilder = new InstructionBuilder();\r
133         WriteMetadataCaseBuilder metaCaseBuilder = new WriteMetadataCaseBuilder();\r
134         WriteMetadataBuilder metaBuilder = new WriteMetadataBuilder();\r
135         metaBuilder.setMetadata(new BigInteger("2"));\r
136         metaBuilder.setMetadataMask(new BigInteger("3"));\r
137         metaCaseBuilder.setWriteMetadata(metaBuilder.build());\r
138         instructionBuilder.setInstruction(metaCaseBuilder.build());\r
139         instructionBuilder.setOrder(1);\r
140         instructions.add(instructionBuilder.build());\r
141         instructionBuilder = new InstructionBuilder();\r
142         WriteActionsCaseBuilder writeCaseBuilder = new WriteActionsCaseBuilder();\r
143         WriteActionsBuilder writeBuilder = new WriteActionsBuilder();\r
144         List<Action> actions = new ArrayList<>();\r
145         writeBuilder.setAction(actions);\r
146         writeCaseBuilder.setWriteActions(writeBuilder.build());\r
147         instructionBuilder.setInstruction(writeCaseBuilder.build());\r
148         instructionBuilder.setOrder(2);\r
149         instructions.add(instructionBuilder.build());\r
150         instructionBuilder = new InstructionBuilder();\r
151         ApplyActionsCaseBuilder applyCaseBuilder = new ApplyActionsCaseBuilder();\r
152         ApplyActionsBuilder applyBuilder = new ApplyActionsBuilder();\r
153         actions = new ArrayList<>();\r
154         applyBuilder.setAction(actions);\r
155         applyCaseBuilder.setApplyActions(applyBuilder.build());\r
156         instructionBuilder.setInstruction(applyCaseBuilder.build());\r
157         instructionBuilder.setOrder(3);\r
158         instructions.add(instructionBuilder.build());\r
159         instructionBuilder = new InstructionBuilder();\r
160         ClearActionsCaseBuilder clearCaseBuilder = new ClearActionsCaseBuilder();\r
161         ClearActionsBuilder clearBuilder = new ClearActionsBuilder();\r
162         actions = new ArrayList<>();\r
163         clearBuilder.setAction(actions);\r
164         clearCaseBuilder.setClearActions(clearBuilder.build());\r
165         instructionBuilder.setInstruction(clearCaseBuilder.build());\r
166         instructionBuilder.setOrder(4);\r
167         instructions.add(instructionBuilder.build());\r
168         instructionBuilder = new InstructionBuilder();\r
169         MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();\r
170         MeterBuilder meterBuilder = new MeterBuilder();\r
171         meterBuilder.setMeterId(new MeterId(5L));\r
172         meterCaseBuilder.setMeter(meterBuilder.build());\r
173         instructionBuilder.setInstruction(meterCaseBuilder.build());\r
174         instructionBuilder.setOrder(5);\r
175         instructions.add(instructionBuilder.build());\r
176         instructionsBuilder.setInstruction(instructions);\r
177         flowBuilder.setInstructions(instructionsBuilder.build());\r
178         AddFlowInput flow = flowBuilder.build();\r
179 \r
180         List<FlowModInputBuilder> flowMod = FlowConvertor\r
181                 .toFlowModInputs(flow, EncodeConstants.OF10_VERSION_ID, new BigInteger("42"));\r
182 \r
183         Assert.assertEquals("Wrong version", 1, flowMod.get(0).getVersion().intValue());\r
184         Assert.assertEquals("Wrong command", FlowModCommand.OFPFCADD, flowMod.get(0).getCommand());\r
185         Assert.assertEquals("Wrong instructions size", 6, flowMod.get(0).getInstruction().size());\r
186         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions\r
187         .grouping.Instruction instruction = flowMod.get(0).getInstruction().get(0);\r
188         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
189                 + ".instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
190         GotoTableCase gotoTableCase = (GotoTableCase) instruction.getInstructionChoice();\r
191         Assert.assertEquals("Wrong table id", 1, gotoTableCase.getGotoTable().getTableId().intValue());\r
192         instruction = flowMod.get(0).getInstruction().get(1);\r
193         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
194                 + ".instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
195         WriteMetadataCase writeMetadataCase = (WriteMetadataCase) instruction.getInstructionChoice();\r
196         Assert.assertArrayEquals("Wrong metadata", new byte[]{0, 0, 0, 0, 0, 0, 0, 2},\r
197                 writeMetadataCase.getWriteMetadata().getMetadata());\r
198         Assert.assertArrayEquals("Wrong metadata mask", new byte[]{0, 0, 0, 0, 0, 0, 0, 3},\r
199                 writeMetadataCase.getWriteMetadata().getMetadataMask());\r
200         \r
201         instruction = flowMod.get(0).getInstruction().get(2);\r
202         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
203                 + ".instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
204         WriteActionsCase writeActionsCase = (WriteActionsCase) instruction.getInstructionChoice();\r
205         Assert.assertEquals("Wrong actions size", 0, writeActionsCase.getWriteActions().getAction().size());\r
206         instruction = flowMod.get(0).getInstruction().get(3);\r
207         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
208                 + ".instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
209         ApplyActionsCase applyActionsCase =  (ApplyActionsCase) instruction.getInstructionChoice();\r
210         Assert.assertEquals("Wrong actions size", 0, applyActionsCase.getApplyActions().getAction().size());\r
211         instruction = flowMod.get(0).getInstruction().get(4);\r
212         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
213                 + ".instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
214         instruction = flowMod.get(0).getInstruction().get(5);\r
215         Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"\r
216                 + ".instruction.rev130731.instruction.grouping.instruction.choice.MeterCase", instruction.getInstructionChoice().getImplementedInterface().getName());\r
217         MeterCase meterCase = (MeterCase) instruction.getInstructionChoice();\r
218         Assert.assertEquals("Wrong meter id", 5, meterCase.getMeter().getMeterId().intValue());\r
219     }\r
220 }