Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / flow / FlowConvertor.java
1 /*
2  * Copyright (c) 2013, 2015 Ericsson. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Ordering;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Optional;
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.action.data.ActionConvertorData;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.OrderComparator;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.ApplyActionsCase;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.ClearActionsCase;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.GoToTableCase;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.MeterCase;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.WriteActionsCase;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.WriteMetadataCase;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.flowflag.FlowFlagReactor;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MatchTypeBase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
67
68 /**
69  * Converts the SAL Flow to OF Flow. It checks if there is a set-vlan-id (1.0) action made on OF1.3.
70  * If yes its handled separately.
71  *
72  * Example usage:
73  * <pre>
74  * {@code
75  * VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(version);
76  * data.setDatapathId(datapathId);
77  * Optional<List<FlowModInputBuilder>> ofFlow = ConvertorManager.getInstance().convert(salFlow, data);
78  * }
79  * </pre>
80  */
81 public class FlowConvertor implements ParametrizedConvertor<Flow, List<FlowModInputBuilder>, VersionDatapathIdConvertorData> {
82     /**
83      * Default idle timeout
84      */
85     public static final Integer DEFAULT_IDLE_TIMEOUT = 0;
86     /**
87      * Default hard timeout
88      */
89     public static final Integer DEFAULT_HARD_TIMEOUT = 0;
90     /**
91      * Default priority
92      */
93     public static final Integer DEFAULT_PRIORITY = Integer.parseInt("8000", 16);
94     /**
95      * flow flag: remove
96      */
97     public static final boolean DEFAULT_OFPFF_FLOW_REM = false;
98     /**
99      * flow flag: check overlap
100      */
101     public static final boolean DEFAULT_OFPFF_CHECK_OVERLAP = false;
102     /**
103      * flow flag: reset counts
104      */
105     public static final boolean DEFAULT_OFPFF_RESET_COUNTS = false;
106     /**
107      * flow flag: don't keep track of packet counts
108      */
109     public static final boolean DEFAULT_OFPFF_NO_PKT_COUNTS = false;
110     /**
111      * flow flag: don't keep track of byte counts
112      */
113     public static final boolean DEFAULT_OFPFF_NO_BYT_COUNTS = false;
114     /**
115      * flow flag: emergency [OFP-1.0]
116      */
117     public static final boolean DEFAULT_OFPFF_EMERGENCY = false;
118     /**
119      * OxmMatch type
120      */
121     public static final Class<? extends MatchTypeBase> DEFAULT_MATCH_TYPE = OxmMatchType.class;
122     /**
123      * default match entries - empty
124      */
125     public static final List<MatchEntry> DEFAULT_MATCH_ENTRIES = new ArrayList<>();
126
127     // Default values for when things are null
128     private static final TableId DEFAULT_TABLE_ID = new TableId(0L);
129     private static final Long DEFAULT_BUFFER_ID = OFConstants.OFP_NO_BUFFER;
130     private static final Long OFPP_ANY = Long.parseLong("ffffffff", 16);
131     private static final Long DEFAULT_OUT_PORT = OFPP_ANY;
132     private static final Long OFPG_ANY = Long.parseLong("ffffffff", 16);
133     private static final Long DEFAULT_OUT_GROUP = OFPG_ANY;
134     private static final Integer PUSH_VLAN = 0x8100;
135     private static final Ordering<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> INSTRUCTION_ORDERING = Ordering.from(OrderComparator.build());
136     private static final VlanMatch VLAN_MATCH_FALSE;
137     private static final VlanMatch VLAN_MATCH_TRUE;
138     private static final ConvertorProcessor<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction, Instruction, ActionConvertorData> PROCESSOR = new ConvertorProcessor<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction, Instruction, ActionConvertorData>()
139             .addCase(new ApplyActionsCase())
140             .addCase(new ClearActionsCase())
141             .addCase(new GoToTableCase())
142             .addCase(new MeterCase())
143             .addCase(new WriteActionsCase())
144             .addCase(new WriteMetadataCase());
145
146     static {
147         final VlanId zeroVlan = new VlanId(0);
148         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
149         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
150         vlanIdBuilder.setVlanIdPresent(false);
151         vlanIdBuilder.setVlanId(zeroVlan);
152         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
153
154         VLAN_MATCH_FALSE = vlanMatchBuilder.build();
155
156         VlanMatchBuilder vlanMatchBuilder2 = new VlanMatchBuilder();
157         VlanIdBuilder vlanIdBuilder2 = new VlanIdBuilder();
158         vlanIdBuilder2.setVlanIdPresent(true);
159         vlanIdBuilder2.setVlanId(zeroVlan);
160         vlanMatchBuilder2.setVlanId(vlanIdBuilder2.build());
161
162         VLAN_MATCH_TRUE = vlanMatchBuilder2.build();
163     }
164
165     private static FlowModInputBuilder toFlowModInput(Flow flow, short version, BigInteger datapathid) {
166
167         FlowModInputBuilder flowMod = new FlowModInputBuilder();
168         salToOFFlowCookie(flow, flowMod);
169         salToOFFlowCookieMask(flow, flowMod);
170         salToOFFlowTableId(flow, flowMod);
171         salToOFFlowCommand(flow, flowMod);
172         salToOFFlowIdleTimeout(flow, flowMod);
173         salToOFFlowHardTimeout(flow, flowMod);
174         salToOFFlowPriority(flow, flowMod);
175         salToOFFlowBufferId(flow, flowMod);
176         salToOFFlowOutPort(flow, flowMod);
177         salToOFFlowOutGroup(flow, flowMod);
178
179         // convert and inject flowFlags
180         FlowFlagReactor.getInstance().convert(flow.getFlags(), version, flowMod);
181
182         // convert and inject match
183         MatchReactor.getInstance().convert(flow.getMatch(), version, flowMod);
184
185         if (flow.getInstructions() != null) {
186             flowMod.setInstruction(toInstructions(flow, version, datapathid));
187             flowMod.setAction(getActions(version, datapathid, flow));
188         }
189
190         flowMod.setVersion(version);
191
192         return flowMod;
193     }
194
195     private static void salToOFFlowOutGroup(Flow flow, FlowModInputBuilder flowMod) {
196         if (flow.getOutGroup() != null) {
197             flowMod.setOutGroup(flow.getOutGroup());
198         } else {
199             flowMod.setOutGroup(DEFAULT_OUT_GROUP);
200         }
201     }
202
203     private static void salToOFFlowOutPort(Flow flow, FlowModInputBuilder flowMod) {
204         if (flow.getOutPort() != null) {
205             flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
206         } else {
207             flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
208         }
209     }
210
211     private static void salToOFFlowBufferId(Flow flow, FlowModInputBuilder flowMod) {
212         if (flow.getBufferId() != null) {
213             flowMod.setBufferId(flow.getBufferId());
214         } else {
215             flowMod.setBufferId(DEFAULT_BUFFER_ID);
216         }
217     }
218
219     private static void salToOFFlowPriority(Flow flow, FlowModInputBuilder flowMod) {
220         if (flow.getPriority() != null) {
221             flowMod.setPriority(flow.getPriority());
222         } else {
223             flowMod.setPriority(DEFAULT_PRIORITY);
224         }
225     }
226
227     private static void salToOFFlowHardTimeout(Flow flow, FlowModInputBuilder flowMod) {
228         if (flow.getHardTimeout() != null) {
229             flowMod.setHardTimeout(flow.getHardTimeout());
230         } else {
231             flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
232         }
233     }
234
235     private static void salToOFFlowIdleTimeout(Flow flow, FlowModInputBuilder flowMod) {
236         if (flow.getIdleTimeout() != null) {
237             flowMod.setIdleTimeout(flow.getIdleTimeout());
238         } else {
239             flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
240         }
241     }
242
243     private static void salToOFFlowCommand(Flow flow, FlowModInputBuilder flowMod) {
244         if (flow instanceof AddFlowInput || flow instanceof UpdatedFlow) {
245             flowMod.setCommand(FlowModCommand.OFPFCADD);
246         } else if (flow instanceof RemoveFlowInput) {
247             if (MoreObjects.firstNonNull(flow.isStrict(), Boolean.FALSE)) {
248                 flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT);
249             } else {
250                 flowMod.setCommand(FlowModCommand.OFPFCDELETE);
251             }
252         }
253     }
254
255     private static void salToOFFlowTableId(Flow flow, FlowModInputBuilder flowMod) {
256         if (flow.getTableId() != null) {
257             flowMod.setTableId(new TableId(flow.getTableId().longValue()));
258         } else {
259             flowMod.setTableId(DEFAULT_TABLE_ID);
260         }
261     }
262
263     private static void salToOFFlowCookieMask(Flow flow, FlowModInputBuilder flowMod) {
264         if (flow.getCookieMask() != null) {
265             flowMod.setCookieMask(flow.getCookieMask().getValue());
266         } else {
267             flowMod.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
268         }
269     }
270
271     private static void salToOFFlowCookie(Flow flow, FlowModInputBuilder flowMod) {
272         if (flow.getCookie() != null) {
273             flowMod.setCookie(flow.getCookie().getValue());
274         } else {
275             flowMod.setCookie(OFConstants.DEFAULT_COOKIE);
276         }
277     }
278
279     private static List<Instruction> toInstructions(Flow flow, short version, BigInteger datapathid) {
280         final List<Instruction> instructionsList = new ArrayList<>();
281         final ActionConvertorData data = new ActionConvertorData(version);
282         data.setDatapathId(datapathid);
283         data.setIpProtocol(FlowConvertorUtil.getIpProtocolFromFlow(flow));
284
285         Instructions instructions = flow.getInstructions();
286
287         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
288                 .getInstruction()) {
289             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
290                     .getInstruction();
291
292             Optional<Instruction> result = PROCESSOR.process(curInstruction, data);
293
294             if (result.isPresent()) {
295                 instructionsList.add(result.get());
296             }
297         }
298
299         return instructionsList;
300     }
301
302     private static List<Action> getActions(short version, BigInteger datapathid, Flow flow) {
303         Instructions instructions = flow.getInstructions();
304         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> sortedInstructions =
305                 INSTRUCTION_ORDERING.sortedCopy(instructions.getInstruction());
306
307         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : sortedInstructions) {
308             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
309                     .getInstruction();
310
311             if (curInstruction instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase) {
312                 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase) curInstruction;
313                 ApplyActions applyActions = applyActionscase.getApplyActions();
314
315                 final ActionConvertorData data = new ActionConvertorData(version);
316                 data.setDatapathId(datapathid);
317                 data.setIpProtocol(FlowConvertorUtil.getIpProtocolFromFlow(flow));
318                 Optional<List<Action>> result = ConvertorManager.getInstance().convert(applyActions.getAction(), data);
319                 return result.orElse(Collections.emptyList());
320             }
321         }
322
323         return null;
324     }
325
326     // check if set vlanid action is present in the flow
327     private static boolean isSetVlanIdActionCasePresent(Flow flow) {
328         // we are trying to find if there is a set-vlan-id action (OF1.0) action present in the flow.
329         // If yes,then we would need to two flows
330         if (flow.getInstructions() != null && flow.getInstructions().getInstruction() != null) {
331             for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction :
332                     flow.getInstructions().getInstruction()) {
333                 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction =
334                         instruction.getInstruction();
335
336                 if (curInstruction instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase) {
337                     org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase) curInstruction;
338                     ApplyActions applyActions = applyActionscase.getApplyActions();
339                     for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action action :
340                             applyActions.getAction()) {
341                         if (action.getAction() instanceof SetVlanIdActionCase) {
342                             return true;
343                         }
344                     }
345                 }
346             }
347         }
348         return false;
349     }
350
351
352     /**
353      * A) If user provided flow's match includes vlan match Â and action has set_vlan_field
354      * Install following rules
355      * Â  Â 1) match on (OFPVID_PRESENT |value) without mask + action [set_field]
356      * <p/>
357      * B) if user provided flow's match doesn't include vlan match but action has set_vlan field
358      * Â  Â  1) Match on (OFPVID_NONE ) without mask + action [push vlan tag + set_field]
359      * Â  Â  2) Match on (OFPVID_PRESENT) with mask (OFPVID_PRESENT ) + action [ set_field]
360      */
361     private static List<FlowModInputBuilder> handleSetVlanIdForOF13(Flow srcFlow, short version, BigInteger datapathId) {
362         List<FlowModInputBuilder> list = new ArrayList<>(2);
363
364         final Match srcMatch = Preconditions.checkNotNull(srcFlow.getMatch());
365         final VlanMatch srcVlanMatch = srcMatch.getVlanMatch();
366         if (srcVlanMatch != null) {
367             //create flow with setfield and match
368             // match on vlan tag or vlanid with no mask
369             VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder(srcVlanMatch);
370             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
371             vlanIdBuilder.setVlanIdPresent(srcVlanMatch.getVlanId().isVlanIdPresent());
372             vlanIdBuilder.setVlanId(srcVlanMatch.getVlanId().getVlanId());
373             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
374             Match match = new MatchBuilder(srcMatch).setVlanMatch(vlanMatchBuilder.build()).build();
375
376             Optional<? extends Flow> optional = injectMatchToFlow(srcFlow, match);
377             if (optional.isPresent()) {
378                 list.add(toFlowModInput(optional.get(), version, datapathId));
379             }
380         } else {
381             // create 2 flows
382             //flow 1
383             // match on no vlan tag with no mask
384             Match match1 = new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_FALSE).build();
385
386             Optional<? extends Flow> optional1 = injectMatchAndAction(srcFlow, match1);
387             if (optional1.isPresent()) {
388                 list.add(toFlowModInput(optional1.get(), version, datapathId));
389             }
390
391             //flow2
392             // match on vlan tag with mask
393             Match match2 = new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_TRUE).build();
394             Optional<? extends Flow> optional2 = injectMatchToFlow(srcFlow, match2);
395             if (optional2.isPresent()) {
396                 list.add(toFlowModInput(optional2.get(), version, datapathId));
397             }
398         }
399         return list;
400     }
401
402
403     private static Optional<? extends Flow> injectMatchToFlow(Flow sourceFlow, Match match) {
404         if (sourceFlow instanceof AddFlowInput) {
405             return Optional.of(new AddFlowInputBuilder(sourceFlow).setMatch(match).build());
406         } else if (sourceFlow instanceof RemoveFlowInput) {
407             return Optional.of(new RemoveFlowInputBuilder(sourceFlow).setMatch(match).build());
408         } else if (sourceFlow instanceof UpdatedFlow) {
409             return Optional.of(new UpdatedFlowBuilder(sourceFlow).setMatch(match).build());
410         } else {
411             return Optional.empty();
412         }
413     }
414
415     private static Optional<? extends Flow> injectMatchAndAction(Flow sourceFlow, Match match) {
416
417         Instructions instructions = (new InstructionsBuilder())
418                 .setInstruction(injectPushActionToInstruction(sourceFlow))
419                 .build();
420
421         if (sourceFlow instanceof AddFlowInput) {
422             return Optional.of(new AddFlowInputBuilder(sourceFlow)
423                     .setMatch(match).setInstructions(instructions).build());
424         } else if (sourceFlow instanceof RemoveFlowInput) {
425             return Optional.of(new RemoveFlowInputBuilder(sourceFlow)
426                     .setMatch(match).setInstructions(instructions).build());
427         } else if (sourceFlow instanceof UpdatedFlow) {
428             return Optional.of(new UpdatedFlowBuilder(sourceFlow)
429                     .setMatch(match).setInstructions(instructions).build());
430         } else {
431             return Optional.empty();
432         }
433     }
434
435     private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
436     injectPushActionToInstruction(final Flow sourceFlow) {
437
438         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> srcInstructionList =
439                 sourceFlow.getInstructions().getInstruction();
440
441         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> targetInstructionList = new ArrayList<>(srcInstructionList.size());
442         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> targetActionList = new ArrayList<>();
443
444         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder instructionBuilder =
445                 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder();
446
447         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction srcInstruction : srcInstructionList) {
448             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curSrcInstruction =
449                     srcInstruction.getInstruction();
450
451             if (curSrcInstruction instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase) {
452                 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase) curSrcInstruction;
453                 ApplyActions applyActions = applyActionscase.getApplyActions();
454                 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> srcActionList = applyActions.getAction();
455
456                 int offset = 0;
457                 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action actionItem : srcActionList) {
458                     // check if its a set-vlan-action. If yes, then add the injected-action
459
460                     if (actionItem.getAction() instanceof SetVlanIdActionCase) {
461                         SetVlanIdActionCase setVlanIdActionCase = (SetVlanIdActionCase) actionItem.getAction();
462
463                         PushVlanActionCaseBuilder pushVlanActionCaseBuilder = new PushVlanActionCaseBuilder();
464                         PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
465
466                         pushVlanActionBuilder.setCfi(new VlanCfi(1))
467                                 .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
468                                 .setEthernetType(PUSH_VLAN)
469                                 .setTag(PUSH_VLAN);
470                         pushVlanActionCaseBuilder.setPushVlanAction(pushVlanActionBuilder.build());
471                         PushVlanActionCase injectedAction = pushVlanActionCaseBuilder.build();
472
473                         ActionBuilder actionBuilder = new ActionBuilder();
474                         actionBuilder.setAction(injectedAction)
475                                 .setKey(actionItem.getKey())
476                                 .setOrder(actionItem.getOrder() + offset);
477
478                         targetActionList.add(actionBuilder.build());
479                         offset++;
480                     }
481
482                     if (offset > 0) {
483                         // we need to increment the order for all the actions added after injection
484                         ActionBuilder actionBuilder =
485                                 new ActionBuilder(actionItem);
486                         actionBuilder.setOrder(actionItem.getOrder() + offset);
487                         actionItem = actionBuilder.build();
488                     }
489
490                     targetActionList.add(actionItem);
491                 }
492
493                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
494                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
495                 applyActionsBuilder.setAction(targetActionList);
496                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
497
498                 instructionBuilder.setInstruction(applyActionsCaseBuilder.build());
499             } else {
500                 instructionBuilder.setInstruction(curSrcInstruction);
501             }
502
503             instructionBuilder
504                     .setKey(srcInstruction.getKey())
505                     .setOrder(srcInstruction.getOrder());
506             targetInstructionList.add(instructionBuilder.build());
507
508         }
509
510         return targetInstructionList;
511     }
512
513     @Override
514     public Class<?> getType() {
515         return Flow.class;
516     }
517
518     @Override
519     public List<FlowModInputBuilder> convert(Flow source, VersionDatapathIdConvertorData data) {
520         if (data.getVersion() >= OFConstants.OFP_VERSION_1_3 && isSetVlanIdActionCasePresent(source)) {
521             return handleSetVlanIdForOF13(source, data.getVersion(), data.getDatapathId());
522         } else {
523             return Collections.singletonList(toFlowModInput(source, data.getVersion(), data.getDatapathId()));
524         }
525     }
526 }