2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
71 import org.opendaylight.yangtools.yang.binding.Augmentation;
72 import org.opendaylight.yangtools.yang.binding.DataContainer;
75 * Unit tests for flow conversion.
77 * @author michal.polkorab
79 public class FlowConvertorTest {
80 private ConvertorManager convertorManager;
84 convertorManager = ConvertorManagerFactory.createDefaultManager();
88 * Tests {@link FlowConvertor#convert(Flow, VersionDatapathIdConvertorData)} }.
92 RemoveFlowInputBuilder flowBuilder = new RemoveFlowInputBuilder();
93 flowBuilder.setBarrier(false);
94 flowBuilder.setCookie(new FlowCookie(new BigInteger("4")));
95 flowBuilder.setCookieMask(new FlowCookie(new BigInteger("5")));
96 flowBuilder.setTableId((short) 6);
97 flowBuilder.setStrict(true);
98 flowBuilder.setIdleTimeout(50);
99 flowBuilder.setHardTimeout(500);
100 flowBuilder.setPriority(40);
101 flowBuilder.setBufferId(18L);
102 flowBuilder.setOutPort(new BigInteger("65535"));
103 flowBuilder.setOutGroup(5000L);
104 flowBuilder.setFlags(null);
105 flowBuilder.setMatch(null);
106 RemoveFlowInput flow = flowBuilder.build();
108 VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(OFConstants.OFP_VERSION_1_3);
109 data.setDatapathId(new BigInteger("42"));
111 List<FlowModInputBuilder> flowMod = convert(flow, data);
113 Assert.assertEquals("Wrong version", 4, flowMod.get(0).getVersion().intValue());
114 Assert.assertEquals("Wrong cookie", 4, flowMod.get(0).getCookie().intValue());
115 Assert.assertEquals("Wrong cookie mask", 5, flowMod.get(0).getCookieMask().intValue());
116 Assert.assertEquals("Wrong table id", 6, flowMod.get(0).getTableId().getValue().intValue());
117 Assert.assertEquals("Wrong command", FlowModCommand.OFPFCDELETESTRICT, flowMod.get(0).getCommand());
118 Assert.assertEquals("Wrong idle timeout", 50, flowMod.get(0).getIdleTimeout().intValue());
119 Assert.assertEquals("Wrong hard timeout", 500, flowMod.get(0).getHardTimeout().intValue());
120 Assert.assertEquals("Wrong priority", 40, flowMod.get(0).getPriority().intValue());
121 Assert.assertEquals("Wrong buffer id", 18, flowMod.get(0).getBufferId().intValue());
122 Assert.assertEquals("Wrong out port", 65535, flowMod.get(0).getOutPort().getValue().intValue());
123 Assert.assertEquals("Wrong out group", 5000, flowMod.get(0).getOutGroup().intValue());
124 Assert.assertEquals("Wrong flags", new FlowModFlags(false, false, false, false, false),
125 flowMod.get(0).getFlags());
126 Assert.assertEquals("Wrong match",
127 "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType",
128 flowMod.get(0).getMatch().getType().getName());
129 Assert.assertEquals("Wrong match entries size", 0, flowMod.get(0).getMatch().getMatchEntry().size());
133 * Tests {@link FlowConvertor#convert(Flow, VersionDatapathIdConvertorData)} }.
136 public void testOnlyModifyStrictCommand() {
137 UpdatedFlowBuilder flowBuilder = new UpdatedFlowBuilder();
138 flowBuilder.setStrict(true);
139 UpdatedFlow flow = flowBuilder.build();
141 VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(OFConstants.OFP_VERSION_1_0);
142 data.setDatapathId(new BigInteger("42"));
144 List<FlowModInputBuilder> flowMod = convert(flow, data);
146 Assert.assertEquals("Wrong version", 1, flowMod.get(0).getVersion().intValue());
147 Assert.assertEquals("Wrong command", FlowModCommand.OFPFCADD, flowMod.get(0).getCommand());
151 * Tests {@link FlowConvertor#convert(Flow, VersionDatapathIdConvertorData)} }.
154 public void testInstructionsTranslation() {
155 InstructionBuilder instructionBuilder = new InstructionBuilder();
156 GoToTableCaseBuilder goToCaseBuilder = new GoToTableCaseBuilder();
157 GoToTableBuilder goToBuilder = new GoToTableBuilder();
158 goToBuilder.setTableId((short) 1);
159 goToCaseBuilder.setGoToTable(goToBuilder.build());
160 instructionBuilder.setInstruction(goToCaseBuilder.build());
161 instructionBuilder.setOrder(0);
163 List<Instruction> instructions = new ArrayList<>();
164 instructions.add(instructionBuilder.build());
165 instructionBuilder = new InstructionBuilder();
166 WriteMetadataCaseBuilder metaCaseBuilder = new WriteMetadataCaseBuilder();
167 WriteMetadataBuilder metaBuilder = new WriteMetadataBuilder();
168 metaBuilder.setMetadata(new BigInteger("2"));
169 metaBuilder.setMetadataMask(new BigInteger("3"));
170 metaCaseBuilder.setWriteMetadata(metaBuilder.build());
171 instructionBuilder.setInstruction(metaCaseBuilder.build());
172 instructionBuilder.setOrder(1);
173 instructions.add(instructionBuilder.build());
174 instructionBuilder = new InstructionBuilder();
175 WriteActionsCaseBuilder writeCaseBuilder = new WriteActionsCaseBuilder();
176 WriteActionsBuilder writeBuilder = new WriteActionsBuilder();
177 List<Action> actions = new ArrayList<>();
178 writeBuilder.setAction(actions);
179 writeCaseBuilder.setWriteActions(writeBuilder.build());
180 instructionBuilder.setInstruction(writeCaseBuilder.build());
181 instructionBuilder.setOrder(2);
182 instructions.add(instructionBuilder.build());
183 instructionBuilder = new InstructionBuilder();
184 ApplyActionsCaseBuilder applyCaseBuilder = new ApplyActionsCaseBuilder();
185 ApplyActionsBuilder applyBuilder = new ApplyActionsBuilder();
186 actions = new ArrayList<>();
187 applyBuilder.setAction(actions);
188 applyCaseBuilder.setApplyActions(applyBuilder.build());
189 instructionBuilder.setInstruction(applyCaseBuilder.build());
190 instructionBuilder.setOrder(3);
191 instructions.add(instructionBuilder.build());
192 instructionBuilder = new InstructionBuilder();
193 ClearActionsCaseBuilder clearCaseBuilder = new ClearActionsCaseBuilder();
194 ClearActionsBuilder clearBuilder = new ClearActionsBuilder();
195 actions = new ArrayList<>();
196 clearBuilder.setAction(actions);
197 clearCaseBuilder.setClearActions(clearBuilder.build());
198 instructionBuilder.setInstruction(clearCaseBuilder.build());
199 instructionBuilder.setOrder(4);
200 instructions.add(instructionBuilder.build());
201 instructionBuilder = new InstructionBuilder();
202 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
203 MeterBuilder meterBuilder = new MeterBuilder();
204 meterBuilder.setMeterId(new MeterId(5L));
205 meterCaseBuilder.setMeter(meterBuilder.build());
206 instructionBuilder.setInstruction(meterCaseBuilder.build());
207 instructionBuilder.setOrder(5);
208 instructions.add(instructionBuilder.build());
210 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
211 instructionsBuilder.setInstruction(instructions);
213 AddFlowInputBuilder flowBuilder = new AddFlowInputBuilder();
214 flowBuilder.setInstructions(instructionsBuilder.build());
215 AddFlowInput flow = flowBuilder.build();
217 VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(OFConstants.OFP_VERSION_1_0);
218 data.setDatapathId(new BigInteger("42"));
219 List<FlowModInputBuilder> flowMod = convert(flow, data);
221 Assert.assertEquals("Wrong version", 1, flowMod.get(0).getVersion().intValue());
222 Assert.assertEquals("Wrong command", FlowModCommand.OFPFCADD, flowMod.get(0).getCommand());
223 Assert.assertEquals("Wrong instructions size", 6, flowMod.get(0).getInstruction().size());
224 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions
225 .grouping.Instruction instruction = flowMod.get(0).getInstruction().get(0);
226 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"
227 + ".instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase",
228 instruction.getInstructionChoice().getImplementedInterface().getName());
229 GotoTableCase gotoTableCase = (GotoTableCase) instruction.getInstructionChoice();
230 Assert.assertEquals("Wrong table id", 1, gotoTableCase.getGotoTable().getTableId().intValue());
231 instruction = flowMod.get(0).getInstruction().get(1);
232 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"
233 + ".instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase",
234 instruction.getInstructionChoice().getImplementedInterface().getName());
235 WriteMetadataCase writeMetadataCase = (WriteMetadataCase) instruction.getInstructionChoice();
236 Assert.assertArrayEquals("Wrong metadata", new byte[]{0, 0, 0, 0, 0, 0, 0, 2},
237 writeMetadataCase.getWriteMetadata().getMetadata());
238 Assert.assertArrayEquals("Wrong metadata mask", new byte[]{0, 0, 0, 0, 0, 0, 0, 3},
239 writeMetadataCase.getWriteMetadata().getMetadataMask());
241 instruction = flowMod.get(0).getInstruction().get(2);
242 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"
243 + ".instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase",
244 instruction.getInstructionChoice().getImplementedInterface().getName());
245 WriteActionsCase writeActionsCase = (WriteActionsCase) instruction.getInstructionChoice();
246 Assert.assertEquals("Wrong actions size", 0, writeActionsCase.getWriteActions().getAction().size());
247 instruction = flowMod.get(0).getInstruction().get(3);
248 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"
249 + ".instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase",
250 instruction.getInstructionChoice().getImplementedInterface().getName());
251 ApplyActionsCase applyActionsCase = (ApplyActionsCase) instruction.getInstructionChoice();
252 Assert.assertEquals("Wrong actions size", 0, applyActionsCase.getApplyActions().getAction().size());
253 instruction = flowMod.get(0).getInstruction().get(4);
254 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"
255 + ".instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase",
256 instruction.getInstructionChoice().getImplementedInterface().getName());
257 instruction = flowMod.get(0).getInstruction().get(5);
258 Assert.assertEquals("Wrong type", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common"
259 + ".instruction.rev130731.instruction.grouping.instruction.choice.MeterCase",
260 instruction.getInstructionChoice().getImplementedInterface().getName());
261 MeterCase meterCase = (MeterCase) instruction.getInstructionChoice();
262 Assert.assertEquals("Wrong meter id", 5, meterCase.getMeter().getMeterId().intValue());
266 public void testCloneAndAugmentFlowWithSetVlanId() {
267 MockFlow mockFlow = new MockFlow();
268 Action action1 = createAction(
269 new SetVlanIdActionCaseBuilder().setSetVlanIdAction(
270 new SetVlanIdActionBuilder().setVlanId(new VlanId(10)).build())
274 mockFlow.setMatch(new MatchBuilder().setEthernetMatch(createEthernetMatch()).build());
275 mockFlow.setInstructions(toApplyInstruction(Collections.singletonList(action1)));
277 VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(OFConstants.OFP_VERSION_1_3);
278 data.setDatapathId(BigInteger.ONE);
280 List<FlowModInputBuilder> flowModInputBuilders = convert(mockFlow, data);
282 Assert.assertEquals(2, flowModInputBuilders.size());
286 private List<FlowModInputBuilder> convert(Flow flow, VersionDatapathIdConvertorData data) {
287 Optional<List<FlowModInputBuilder>> flowModOptional = convertorManager.convert(flow, data);
288 Assert.assertTrue("Flow convertor not found", flowModOptional.isPresent());
289 return flowModOptional.get();
292 private static Action createAction(
293 final org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action actionCase,
295 Action action = new ActionBuilder().setOrder(order).setAction(actionCase).build();
299 private static EthernetMatch createEthernetMatch() {
300 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
301 ethernetMatchBuilder.setEthernetType(new EthernetTypeBuilder().setType(new EtherType(33024L)).build());
302 return ethernetMatchBuilder.build();
305 private static Instructions toApplyInstruction(
306 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actions) {
307 return new InstructionsBuilder().setInstruction(Collections.singletonList(new InstructionBuilder().setOrder(0)
308 .setInstruction(new ApplyActionsCaseBuilder()
309 .setApplyActions(new ApplyActionsBuilder().setAction(actions).build()).build())
313 private static class MockFlow implements AddFlowInput {
314 private Instructions instructions;
317 public void setInstructions(final Instructions instructions) {
318 this.instructions = instructions;
321 public void setMatch(final Match match) {
327 public FlowRef getFlowRef() {
332 public <E extends Augmentation<AddFlowInput>> E getAugmentation(final Class<E> augmentationType) {
337 public FlowTableRef getFlowTable() {
342 public Match getMatch() {
347 public Instructions getInstructions() {
352 public String getContainerName() {
357 public FlowCookie getCookieMask() {
362 public Long getBufferId() {
367 public BigInteger getOutPort() {
372 public Long getOutGroup() {
377 public org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags getFlags() {
382 public String getFlowName() {
387 public Boolean isInstallHw() {
392 public Boolean isBarrier() {
397 public Boolean isStrict() {
402 public Integer getPriority() {
407 public Integer getIdleTimeout() {
412 public Integer getHardTimeout() {
417 public FlowCookie getCookie() {
422 public Short getTableId() {
427 public NodeRef getNode() {
432 public Uri getTransactionUri() {
437 public Class<? extends DataContainer> getImplementedInterface() {