2 * Copyright (c) 2013 Ericsson. 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;
11 import com.google.common.base.Objects;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Ordering;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.OrderComparator;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flowflag.FlowFlagReactor;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
19 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActions;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTable;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.Meter;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActions;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MatchTypeBase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
76 import java.math.BigInteger;
77 import java.util.ArrayList;
78 import java.util.List;
81 * Utility class for converting a MD-SAL Flow into the OF flow mod
83 public class FlowConvertor {
84 private static final Logger logger = LoggerFactory.getLogger(FlowConvertor.class);
86 // Default values for when things are null
87 private static final BigInteger DEFAULT_COOKIE = BigInteger.ZERO;
88 private static final BigInteger DEFAULT_COOKIE_MASK = BigInteger.ZERO;
89 private static final TableId DEFAULT_TABLE_ID = new TableId(0L);
90 private static final Integer DEFAULT_IDLE_TIMEOUT = 5 * 60;
91 private static final Integer DEFAULT_HARD_TIMEOUT = 10 * 60;
92 private static final Integer DEFAULT_PRIORITY = Integer.parseInt("8000", 16);
93 private static final Long DEFAULT_BUFFER_ID = Long.parseLong("ffffffff", 16);
94 private static final Long OFPP_ANY = Long.parseLong("ffffffff", 16);
95 private static final Long DEFAULT_OUT_PORT = OFPP_ANY;
96 private static final Long OFPG_ANY = Long.parseLong("ffffffff", 16);
97 private static final Long DEFAULT_OUT_GROUP = OFPG_ANY;
98 /** flow flag: remove */
99 public static final boolean DEFAULT_OFPFF_FLOW_REM = true;
100 /** flow flag: check overlap */
101 public static final boolean DEFAULT_OFPFF_CHECK_OVERLAP = false;
102 /** flow flag: reset counts */
103 public static final boolean DEFAULT_OFPFF_RESET_COUNTS = false;
104 /** flow flag: don't keep track of packet counts */
105 public static final boolean DEFAULT_OFPFF_NO_PKT_COUNTS = false;
106 /** flow flag: don't keep track of byte counts */
107 public static final boolean DEFAULT_OFPFF_NO_BYT_COUNTS = false;
108 /** flow flag: emergency [OFP-1.0] */
109 public static final boolean DEFAULT_OFPFF_EMERGENCY = false;
111 public static final Class<? extends MatchTypeBase> DEFAULT_MATCH_TYPE = OxmMatchType.class;
112 /** default match entries - empty */
113 public static final List<MatchEntries> DEFAULT_MATCH_ENTRIES = new ArrayList<MatchEntries>();
117 * This method converts the SAL Flow to OF Flow.
118 * It checks if there is a set-vlan-id (1.0) action made on OF1.3.
119 * If yes its handled separately
121 public static List<FlowModInputBuilder> toFlowModInputs(Flow srcFlow, short version, BigInteger datapathId) {
122 List<FlowModInputBuilder> list = new ArrayList<>();
124 if (version >= OFConstants.OFP_VERSION_1_3 && isSetVlanIdActionCasePresent(srcFlow)) {
125 list.addAll(handleSetVlanIdForOF13(srcFlow, version, datapathId));
127 list.add(toFlowModInput(srcFlow, version, datapathId));
132 public static FlowModInputBuilder toFlowModInput(Flow flow, short version, BigInteger datapathid) {
134 FlowModInputBuilder flowMod = new FlowModInputBuilder();
135 if (flow.getCookie() != null) {
136 flowMod.setCookie(flow.getCookie().getValue());
138 flowMod.setCookie(DEFAULT_COOKIE);
141 if (flow.getCookieMask() != null) {
142 flowMod.setCookieMask(flow.getCookieMask().getValue());
144 flowMod.setCookieMask(DEFAULT_COOKIE_MASK);
147 if (flow.getTableId() != null) {
148 flowMod.setTableId(new TableId(flow.getTableId().longValue()));
150 flowMod.setTableId(DEFAULT_TABLE_ID);
153 if (flow instanceof AddFlowInput) {
154 flowMod.setCommand(FlowModCommand.OFPFCADD);
155 } else if (flow instanceof RemoveFlowInput) {
156 if (Objects.firstNonNull(flow.isStrict(), Boolean.FALSE)) {
157 flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT);
159 flowMod.setCommand(FlowModCommand.OFPFCDELETE);
161 } else if (flow instanceof UpdatedFlow) {
162 if (Objects.firstNonNull(flow.isStrict(), Boolean.FALSE)) {
163 flowMod.setCommand(FlowModCommand.OFPFCMODIFYSTRICT);
165 flowMod.setCommand(FlowModCommand.OFPFCMODIFY);
169 if (flow.getIdleTimeout() != null) {
170 flowMod.setIdleTimeout(flow.getIdleTimeout());
172 flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
174 if (flow.getHardTimeout() != null) {
175 flowMod.setHardTimeout(flow.getHardTimeout());
177 flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
179 if (flow.getPriority() != null) {
180 flowMod.setPriority(flow.getPriority());
182 flowMod.setPriority(DEFAULT_PRIORITY);
184 if (flow.getBufferId() != null) {
185 flowMod.setBufferId(flow.getBufferId());
187 flowMod.setBufferId(DEFAULT_BUFFER_ID);
190 if (flow.getOutPort() != null) {
191 flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
193 flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
195 if (flow.getOutGroup() != null) {
196 flowMod.setOutGroup(flow.getOutGroup());
198 flowMod.setOutGroup(DEFAULT_OUT_GROUP);
202 // convert and inject flowFlags
203 FlowFlagReactor.getInstance().convert(flow.getFlags(), version, flowMod,datapathid);
205 // convert and inject match
206 MatchReactor.getInstance().convert(flow.getMatch(), version, flowMod,datapathid);
208 if (flow.getInstructions() != null) {
209 flowMod.setInstruction(toInstructions(flow.getInstructions(), version,datapathid));
210 flowMod.setAction(getActions(flow.getInstructions(), version,datapathid));
212 flowMod.setVersion(version);
217 private static List<Instruction> toInstructions(
218 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions,
219 short version,BigInteger datapathid) {
220 List<Instruction> instructionsList = new ArrayList<>();
222 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
224 InstructionBuilder instructionBuilder = new InstructionBuilder();
225 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
227 if (curInstruction instanceof GoToTableCase) {
228 GoToTableCase goToTablecase = (GoToTableCase) curInstruction;
229 GoToTable goToTable = goToTablecase.getGoToTable();
231 .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class);
232 TableIdInstructionBuilder tableBuilder = new TableIdInstructionBuilder();
233 tableBuilder.setTableId(goToTable.getTableId());
234 instructionBuilder.addAugmentation(TableIdInstruction.class, tableBuilder.build());
235 instructionsList.add(instructionBuilder.build());
238 else if (curInstruction instanceof WriteMetadataCase) {
239 WriteMetadataCase writeMetadatacase = (WriteMetadataCase) curInstruction;
240 WriteMetadata writeMetadata = writeMetadatacase.getWriteMetadata();
242 .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class);
243 MetadataInstructionBuilder metadataBuilder = new MetadataInstructionBuilder();
244 metadataBuilder.setMetadata(ByteUtil.convertBigIntegerToNBytes(writeMetadata.getMetadata(),
245 OFConstants.SIZE_OF_LONG_IN_BYTES));
247 .setMetadataMask(ByteUtil.convertBigIntegerToNBytes(writeMetadata.getMetadataMask(),
248 OFConstants.SIZE_OF_LONG_IN_BYTES));
249 instructionBuilder.addAugmentation(MetadataInstruction.class, metadataBuilder.build());
250 instructionsList.add(instructionBuilder.build());
253 else if (curInstruction instanceof WriteActionsCase) {
254 WriteActionsCase writeActionscase = (WriteActionsCase) curInstruction;
255 WriteActions writeActions = writeActionscase.getWriteActions();
257 .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class);
258 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
259 actionsInstructionBuilder.setAction(ActionConvertor.getActions(writeActions.getAction(),
260 version,datapathid));
261 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
262 instructionsList.add(instructionBuilder.build());
265 else if (curInstruction instanceof ApplyActionsCase) {
266 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
267 ApplyActions applyActions = applyActionscase.getApplyActions();
269 .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
270 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
271 actionsInstructionBuilder.setAction(ActionConvertor.getActions(applyActions.getAction(),
272 version,datapathid));
273 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
274 instructionsList.add(instructionBuilder.build());
277 else if (curInstruction instanceof ClearActionsCase) {
278 ClearActionsCase clearActionscase = (ClearActionsCase) curInstruction;
279 ClearActions clearActions = clearActionscase.getClearActions();
281 .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class);
282 instructionsList.add(instructionBuilder.build());
285 else if (curInstruction instanceof MeterCase) {
286 MeterCase metercase = (MeterCase) curInstruction;
287 Meter meter = metercase.getMeter();
289 .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class);
290 MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();
291 Long meterId = meter.getMeterId().getValue();
292 meterBuilder.setMeterId(meterId);
293 instructionBuilder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());
294 instructionsList.add(instructionBuilder.build());
297 return instructionsList;
300 private static List<Action> getActions(
301 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions,
302 short version,BigInteger datapathid) {
304 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> sortedInstructions =
305 Ordering.from(OrderComparator.<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>toInstance())
306 .sortedCopy(instructions.getInstruction());
308 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : sortedInstructions) {
309 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
312 if (curInstruction instanceof ApplyActionsCase) {
313 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
314 ApplyActions applyActions = applyActionscase.getApplyActions();
315 return ActionConvertor.getActions(applyActions.getAction(), version,datapathid);
321 // check if set vlanid action is present in the flow
322 private static boolean isSetVlanIdActionCasePresent(Flow flow) {
323 boolean isPresent = false;
324 // we are trying to find if there is a set-vlan-id action (OF1.0) action present in the flow.
325 // If yes,then we would need to two flows
326 if (flow.getInstructions() != null) {
327 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction :
328 flow.getInstructions().getInstruction()) {
329 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction =
330 instruction.getInstruction();
332 if (curInstruction instanceof ApplyActionsCase) {
333 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
334 ApplyActions applyActions = applyActionscase.getApplyActions();
335 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action action :
336 applyActions.getAction()) {
337 if (action.getAction() instanceof SetVlanIdActionCase) {
350 * A) If user provided flow's match includes vlan match  and action has set_vlan_field
351 * Install following rules
352 *Â Â 1) match on (OFPVID_PRESENT |value) without mask + action [set_field]
354 * B) if user provided flow's match doesn't include vlan match but action has set_vlan field
355 *Â Â 1) Match on (OFPVID_NONEÂ ) without mask + action [push vlan tag + set_field]
356 *Â Â 2) Match on (OFPVID_PRESENT) with mask (OFPVID_PRESENT ) + action [ set_field]
358 private static List<FlowModInputBuilder> handleSetVlanIdForOF13(Flow srcFlow, short version, BigInteger datapathId) {
359 List<FlowModInputBuilder> list = new ArrayList<>();
361 VlanMatch srcVlanMatch = srcFlow.getMatch().getVlanMatch();
362 boolean hasVlanMatch = (srcFlow.getMatch() != null && srcVlanMatch != null);
364 //create flow with setfield and match
365 // match on vlan tag or vlanid with no mask
366 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder(srcVlanMatch);
367 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
368 vlanIdBuilder.setVlanIdPresent(srcVlanMatch.getVlanId().isVlanIdPresent());
369 vlanIdBuilder.setVlanId(srcVlanMatch.getVlanId().getVlanId());
370 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
371 Match match = new MatchBuilder(srcFlow.getMatch()).setVlanMatch(vlanMatchBuilder.build()).build();
373 Optional<? extends Flow> optional = injectMatchToFlow(srcFlow, match);
374 if (optional.isPresent()) {
375 list.add(toFlowModInput(optional.get(), version, datapathId));
380 // match on no vlan tag with no mask
381 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
382 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
383 vlanIdBuilder.setVlanIdPresent(false);
384 vlanIdBuilder.setVlanId(new VlanId(0));
385 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
386 Match match1 = new MatchBuilder(srcFlow.getMatch()).setVlanMatch(vlanMatchBuilder.build()).build();
388 Optional<? extends Flow> optional1 = injectMatchAndAction(srcFlow, match1);
389 if (optional1.isPresent()) {
390 list.add(toFlowModInput(optional1.get(), version, datapathId));
394 // match on vlan tag with mask
395 VlanMatchBuilder vlanMatchBuilder2 = new VlanMatchBuilder();
396 VlanIdBuilder vlanIdBuilder2 = new VlanIdBuilder();
397 vlanIdBuilder2.setVlanIdPresent(true);
398 vlanIdBuilder2.setVlanId(new VlanId(0));
399 vlanMatchBuilder2.setVlanId(vlanIdBuilder2.build());
400 Match match2 = new MatchBuilder(srcFlow.getMatch()).setVlanMatch(vlanMatchBuilder2.build()).build();
401 Optional<? extends Flow> optional2 = injectMatchToFlow(srcFlow, match2);
402 if (optional2.isPresent()) {
403 list.add(toFlowModInput(optional2.get(), version, datapathId));
410 private static Optional<? extends Flow> injectMatchToFlow(Flow sourceFlow, Match match) {
411 if (sourceFlow instanceof AddFlowInput) {
412 return Optional.<AddFlowInput>of(new AddFlowInputBuilder(sourceFlow).setMatch(match).build());
413 } else if (sourceFlow instanceof RemoveFlowInput) {
414 return Optional.<RemoveFlowInput>of(new RemoveFlowInputBuilder(sourceFlow).setMatch(match).build());
415 } else if (sourceFlow instanceof UpdatedFlow) {
416 return Optional.<UpdatedFlow>of(new UpdatedFlowBuilder(sourceFlow).setMatch(match).build());
418 return Optional.<Flow>absent();
422 private static Optional<? extends Flow> injectMatchAndAction(Flow sourceFlow, Match match) {
424 Instructions instructions = (new InstructionsBuilder())
425 .setInstruction(injectPushActionToInstruction(sourceFlow))
428 if (sourceFlow instanceof AddFlowInput) {
429 return Optional.<AddFlowInput>of(new AddFlowInputBuilder(sourceFlow)
430 .setMatch(match).setInstructions(instructions).build());
431 } else if (sourceFlow instanceof RemoveFlowInput) {
432 return Optional.<RemoveFlowInput>of(new RemoveFlowInputBuilder(sourceFlow)
433 .setMatch(match).setInstructions(instructions).build());
434 } else if (sourceFlow instanceof UpdatedFlow) {
435 return Optional.<UpdatedFlow>of(new UpdatedFlowBuilder(sourceFlow)
436 .setMatch(match).setInstructions(instructions).build());
438 return Optional.<Flow>absent();
442 private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
443 injectPushActionToInstruction(final Flow sourceFlow) {
445 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> srcInstructionList =
446 sourceFlow.getInstructions().getInstruction();
448 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> targetInstructionList = new ArrayList<>(srcInstructionList.size());
449 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> targetActionList = new ArrayList<>();
451 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder instructionBuilder =
452 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder();
454 for (int i=0; i < srcInstructionList.size(); i++) {
455 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction srcInstruction =
456 srcInstructionList.get(i);
457 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curSrcInstruction =
458 srcInstruction.getInstruction();
460 if (curSrcInstruction instanceof ApplyActionsCase) {
461 ApplyActionsCase applyActionscase = (ApplyActionsCase) curSrcInstruction;
462 ApplyActions applyActions = applyActionscase.getApplyActions();
463 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> srcActionList = applyActions.getAction();
466 for (int j=0; j < srcActionList.size(); j++) {
467 // check if its a set-vlan-action. If yes, then add the injected-action
469 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action actionItem = srcActionList.get(j);
470 if(actionItem.getAction() instanceof SetVlanIdActionCase) {
471 SetVlanIdActionCase setVlanIdActionCase = (SetVlanIdActionCase) actionItem.getAction();
473 PushVlanActionCaseBuilder pushVlanActionCaseBuilder = new PushVlanActionCaseBuilder();
474 PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
476 pushVlanActionBuilder.setCfi(new VlanCfi(1))
477 .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
478 .setEthernetType(sourceFlow.getMatch().getEthernetMatch()
479 .getEthernetType().getType().getValue().intValue())
480 .setTag(sourceFlow.getMatch().getEthernetMatch()
481 .getEthernetType().getType().getValue().intValue());
482 pushVlanActionCaseBuilder.setPushVlanAction(pushVlanActionBuilder.build());
483 PushVlanActionCase injectedAction = pushVlanActionCaseBuilder.build();
485 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder actionBuilder = new ActionBuilder();
486 actionBuilder.setAction(injectedAction)
487 .setKey(actionItem.getKey())
488 .setOrder(actionItem.getOrder() + offset);
490 targetActionList.add(actionBuilder.build());
495 // we need to increment the order for all the actions added after injection
496 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder actionBuilder =
497 new org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder(actionItem);
498 actionBuilder.setOrder(actionItem.getOrder() + offset);
499 actionItem = actionBuilder.build();
502 targetActionList.add(actionItem);
505 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
506 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
507 applyActionsBuilder.setAction(targetActionList);
508 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
510 instructionBuilder.setInstruction(applyActionsCaseBuilder.build());
512 instructionBuilder.setInstruction(curSrcInstruction);
516 .setKey(srcInstruction.getKey())
517 .setOrder(srcInstruction.getOrder());
518 targetInstructionList.add(instructionBuilder.build());
522 return targetInstructionList;