BUG-1421: VLAN tag
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowConvertor.java
1 /**
2  * Copyright (c) 2013 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;
10
11 import com.google.common.base.Objects;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Ordering;
14
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;
75
76 import java.math.BigInteger;
77 import java.util.ArrayList;
78 import java.util.List;
79
80 /**
81  * Utility class for converting a MD-SAL Flow into the OF flow mod
82  */
83 public class FlowConvertor {
84     private static final Logger logger = LoggerFactory.getLogger(FlowConvertor.class);
85
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;
110     /** OxmMatch type */
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>();
114
115
116     /**
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
120      */
121     public static List<FlowModInputBuilder> toFlowModInputs(Flow srcFlow, short version, BigInteger datapathId) {
122         List<FlowModInputBuilder> list = new ArrayList<>();
123
124         if (version >= OFConstants.OFP_VERSION_1_3 && isSetVlanIdActionCasePresent(srcFlow)) {
125             list.addAll(handleSetVlanIdForOF13(srcFlow, version, datapathId));
126         } else {
127             list.add(toFlowModInput(srcFlow, version, datapathId));
128         }
129         return list;
130     }
131
132     public static FlowModInputBuilder toFlowModInput(Flow flow, short version, BigInteger datapathid) {
133
134         FlowModInputBuilder flowMod = new FlowModInputBuilder();
135         if (flow.getCookie() != null) {
136             flowMod.setCookie(flow.getCookie().getValue());
137         } else {
138             flowMod.setCookie(DEFAULT_COOKIE);
139         }
140
141         if (flow.getCookieMask() != null) {
142             flowMod.setCookieMask(flow.getCookieMask().getValue());
143         } else {
144             flowMod.setCookieMask(DEFAULT_COOKIE_MASK);
145         }
146
147         if (flow.getTableId() != null) {
148             flowMod.setTableId(new TableId(flow.getTableId().longValue()));
149         } else {
150             flowMod.setTableId(DEFAULT_TABLE_ID);
151         }
152
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);
158             } else {
159                 flowMod.setCommand(FlowModCommand.OFPFCDELETE);
160             }
161         } else if (flow instanceof UpdatedFlow) {
162             if (Objects.firstNonNull(flow.isStrict(), Boolean.FALSE)) {
163                 flowMod.setCommand(FlowModCommand.OFPFCMODIFYSTRICT);
164             } else {
165                 flowMod.setCommand(FlowModCommand.OFPFCMODIFY);
166             }
167         }
168
169         if (flow.getIdleTimeout() != null) {
170             flowMod.setIdleTimeout(flow.getIdleTimeout());
171         } else {
172             flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
173         }
174         if (flow.getHardTimeout() != null) {
175             flowMod.setHardTimeout(flow.getHardTimeout());
176         } else {
177             flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
178         }
179         if (flow.getPriority() != null) {
180             flowMod.setPriority(flow.getPriority());
181         } else {
182             flowMod.setPriority(DEFAULT_PRIORITY);
183         }
184         if (flow.getBufferId() != null) {
185             flowMod.setBufferId(flow.getBufferId());
186         } else {
187             flowMod.setBufferId(DEFAULT_BUFFER_ID);
188         }
189
190         if (flow.getOutPort() != null) {
191             flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
192         } else {
193             flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
194         }
195         if (flow.getOutGroup() != null) {
196             flowMod.setOutGroup(flow.getOutGroup());
197         } else {
198             flowMod.setOutGroup(DEFAULT_OUT_GROUP);
199         }
200
201         
202         // convert and inject flowFlags
203         FlowFlagReactor.getInstance().convert(flow.getFlags(), version, flowMod,datapathid);
204
205         // convert and inject match
206         MatchReactor.getInstance().convert(flow.getMatch(), version, flowMod,datapathid);
207
208         if (flow.getInstructions() != null) {
209             flowMod.setInstruction(toInstructions(flow.getInstructions(), version,datapathid));
210             flowMod.setAction(getActions(flow.getInstructions(), version,datapathid));
211         }
212         flowMod.setVersion(version);
213         
214         return flowMod;
215     }
216
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<>();
221
222         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
223                 .getInstruction()) {
224             InstructionBuilder instructionBuilder = new InstructionBuilder();
225             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
226                     .getInstruction();
227             if (curInstruction instanceof GoToTableCase) {
228                 GoToTableCase goToTablecase = (GoToTableCase) curInstruction;
229                 GoToTable goToTable = goToTablecase.getGoToTable();
230                 instructionBuilder
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());
236             }
237
238             else if (curInstruction instanceof WriteMetadataCase) {
239                 WriteMetadataCase writeMetadatacase = (WriteMetadataCase) curInstruction;
240                 WriteMetadata writeMetadata = writeMetadatacase.getWriteMetadata();
241                 instructionBuilder
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));
246                 metadataBuilder
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());
251             }
252
253             else if (curInstruction instanceof WriteActionsCase) {
254                 WriteActionsCase writeActionscase = (WriteActionsCase) curInstruction;
255                 WriteActions writeActions = writeActionscase.getWriteActions();
256                 instructionBuilder
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());
263             }
264
265             else if (curInstruction instanceof ApplyActionsCase) {
266                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
267                 ApplyActions applyActions = applyActionscase.getApplyActions();
268                 instructionBuilder
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());
275             }
276
277             else if (curInstruction instanceof ClearActionsCase) {
278                 ClearActionsCase clearActionscase = (ClearActionsCase) curInstruction;
279                 ClearActions clearActions = clearActionscase.getClearActions();
280                 instructionBuilder
281                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class);
282                 instructionsList.add(instructionBuilder.build());
283             }
284
285             else if (curInstruction instanceof MeterCase) {
286                 MeterCase metercase = (MeterCase) curInstruction;
287                 Meter meter = metercase.getMeter();
288                 instructionBuilder
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());
295             }
296         }
297         return instructionsList;
298     }
299     
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) {
303
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());
307
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
310                     .getInstruction();
311
312             if (curInstruction instanceof ApplyActionsCase) {
313                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
314                 ApplyActions applyActions = applyActionscase.getApplyActions();
315                 return ActionConvertor.getActions(applyActions.getAction(), version,datapathid);
316             }
317         }
318         return null;
319     }
320
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();
331
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) {
338                             isPresent = true;
339                             break;
340                         }
341                     }
342                 }
343             }
344         }
345         return isPresent;
346     }
347
348
349     /**
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]
353      *
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]
357      */
358     private static List<FlowModInputBuilder> handleSetVlanIdForOF13(Flow srcFlow, short version, BigInteger datapathId) {
359         List<FlowModInputBuilder> list = new ArrayList<>();
360
361         VlanMatch srcVlanMatch = srcFlow.getMatch().getVlanMatch();
362         boolean hasVlanMatch = (srcFlow.getMatch() != null && srcVlanMatch != null);
363         if (hasVlanMatch) {
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();
372
373             Optional<? extends Flow> optional = injectMatchToFlow(srcFlow, match);
374             if (optional.isPresent()) {
375                 list.add(toFlowModInput(optional.get(), version, datapathId));
376             }
377         } else {
378             // create 2 flows
379             //flow 1
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();
387
388             Optional<? extends Flow> optional1 = injectMatchAndAction(srcFlow, match1);
389             if (optional1.isPresent()) {
390                 list.add(toFlowModInput(optional1.get(), version, datapathId));
391             }
392
393             //flow2
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));
404             }
405         }
406         return list;
407     }
408
409
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());
417         } else {
418             return Optional.<Flow>absent();
419         }
420     }
421
422     private static Optional<? extends Flow> injectMatchAndAction(Flow sourceFlow, Match match) {
423
424         Instructions instructions = (new InstructionsBuilder())
425             .setInstruction(injectPushActionToInstruction(sourceFlow))
426             .build();
427
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());
437         } else {
438             return Optional.<Flow>absent();
439         }
440     }
441
442     private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
443         injectPushActionToInstruction(final Flow sourceFlow) {
444
445         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> srcInstructionList =
446             sourceFlow.getInstructions().getInstruction();
447
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<>();
450
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();
453         
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();
459             
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();
464
465                 int offset = 0;
466                 for (int j=0; j < srcActionList.size(); j++) {
467                     // check if its a set-vlan-action. If yes, then add the injected-action
468
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();
472
473                         PushVlanActionCaseBuilder pushVlanActionCaseBuilder = new PushVlanActionCaseBuilder();
474                         PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
475
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();
484
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);
489
490                         targetActionList.add(actionBuilder.build());
491                         offset ++;
492                     }
493
494                     if (offset > 0) {
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();
500                     }
501                     
502                     targetActionList.add(actionItem);
503                 }
504
505                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
506                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
507                 applyActionsBuilder.setAction(targetActionList);
508                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
509                     
510                 instructionBuilder.setInstruction(applyActionsCaseBuilder.build());
511             } else {
512                 instructionBuilder.setInstruction(curSrcInstruction);
513             }
514             
515             instructionBuilder
516                 .setKey(srcInstruction.getKey())
517                 .setOrder(srcInstruction.getOrder());
518             targetInstructionList.add(instructionBuilder.build());
519             
520         }
521
522         return targetInstructionList;
523     }
524
525 }