401227b800973af342216de35ce9b02a7acc08c5
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / 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;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
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.go.to.table._case.GoToTable;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.Meter;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActions;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCaseBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice._goto.table._case.GotoTableBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.meter._case.MeterBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.write.actions._case.WriteActionsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.write.metadata._case.WriteMetadataBuilder;
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.rev150225.OxmMatchType;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75 import com.google.common.base.MoreObjects;
76 import com.google.common.base.Optional;
77 import com.google.common.base.Preconditions;
78 import com.google.common.collect.Ordering;
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 LOG = LoggerFactory.getLogger(FlowConvertor.class);
85
86     // Default values for when things are null
87     private static final TableId DEFAULT_TABLE_ID = new TableId(0L);
88     /**
89      * Default idle timeout
90      */
91     public static final Integer DEFAULT_IDLE_TIMEOUT = 0;
92     /**
93      * Default hard timeout
94      */
95     public static final Integer DEFAULT_HARD_TIMEOUT = 0;
96     /**
97      * Default priority
98      */
99     public static final Integer DEFAULT_PRIORITY = Integer.parseInt("8000", 16);
100     private static final Long DEFAULT_BUFFER_ID = OFConstants.OFP_NO_BUFFER;
101     private static final Long OFPP_ANY = Long.parseLong("ffffffff", 16);
102     private static final Long DEFAULT_OUT_PORT = OFPP_ANY;
103     private static final Long OFPG_ANY = Long.parseLong("ffffffff", 16);
104     private static final Long DEFAULT_OUT_GROUP = OFPG_ANY;
105     /**
106      * flow flag: remove
107      */
108     public static final boolean DEFAULT_OFPFF_FLOW_REM = false;
109     /**
110      * flow flag: check overlap
111      */
112     public static final boolean DEFAULT_OFPFF_CHECK_OVERLAP = false;
113     /**
114      * flow flag: reset counts
115      */
116     public static final boolean DEFAULT_OFPFF_RESET_COUNTS = false;
117     /**
118      * flow flag: don't keep track of packet counts
119      */
120     public static final boolean DEFAULT_OFPFF_NO_PKT_COUNTS = false;
121     /**
122      * flow flag: don't keep track of byte counts
123      */
124     public static final boolean DEFAULT_OFPFF_NO_BYT_COUNTS = false;
125     /**
126      * flow flag: emergency [OFP-1.0]
127      */
128     public static final boolean DEFAULT_OFPFF_EMERGENCY = false;
129     /**
130      * OxmMatch type
131      */
132     public static final Class<? extends MatchTypeBase> DEFAULT_MATCH_TYPE = OxmMatchType.class;
133     /**
134      * default match entries - empty
135      */
136     public static final List<MatchEntry> DEFAULT_MATCH_ENTRIES = new ArrayList<MatchEntry>();
137     private static final Integer PUSH_VLAN = 0x8100;
138
139     private static final Ordering<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> INSTRUCTION_ORDERING =
140             Ordering.from(OrderComparator.<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>build());
141
142     private static final VlanMatch VLAN_MATCH_FALSE;
143     private static final VlanMatch VLAN_MATCH_TRUE;
144
145     static {
146         final VlanId zeroVlan = new VlanId(0);
147         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
148         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
149         vlanIdBuilder.setVlanIdPresent(false);
150         vlanIdBuilder.setVlanId(zeroVlan);
151         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
152
153         VLAN_MATCH_FALSE = vlanMatchBuilder.build();
154
155         VlanMatchBuilder vlanMatchBuilder2 = new VlanMatchBuilder();
156         VlanIdBuilder vlanIdBuilder2 = new VlanIdBuilder();
157         vlanIdBuilder2.setVlanIdPresent(true);
158         vlanIdBuilder2.setVlanId(zeroVlan);
159         vlanMatchBuilder2.setVlanId(vlanIdBuilder2.build());
160
161         VLAN_MATCH_TRUE = vlanMatchBuilder2.build();
162     }
163
164     private FlowConvertor() {
165         //hiding implicit constructor
166     }
167
168     /**
169      * This method converts the SAL Flow to OF Flow.
170      * It checks if there is a set-vlan-id (1.0) action made on OF1.3.
171      * If yes its handled separately
172      *
173      * @param srcFlow source flow
174      * @param version openflow version
175      * @param datapathId datapath id
176      * @return list of flow mod build
177      */
178     public static List<FlowModInputBuilder> toFlowModInputs(Flow srcFlow, short version, BigInteger datapathId) {
179         if (version >= OFConstants.OFP_VERSION_1_3 && isSetVlanIdActionCasePresent(srcFlow)) {
180             return handleSetVlanIdForOF13(srcFlow, version, datapathId);
181         } else {
182             return Collections.singletonList(toFlowModInput(srcFlow, version, datapathId));
183         }
184     }
185
186     private static FlowModInputBuilder toFlowModInput(Flow flow, short version, BigInteger datapathid) {
187
188         FlowModInputBuilder flowMod = new FlowModInputBuilder();
189         salToOFFlowCookie(flow, flowMod);
190         salToOFFlowCookieMask(flow, flowMod);
191         salToOFFlowTableId(flow, flowMod);
192         salToOFFlowCommand(flow, flowMod);
193         salToOFFlowIdleTimeout(flow, flowMod);
194         salToOFFlowHardTimeout(flow, flowMod);
195         salToOFFlowPriority(flow, flowMod);
196         salToOFFlowBufferId(flow, flowMod);
197         salToOFFlowOutPort(flow, flowMod);
198         salToOFFlowOutGroup(flow, flowMod);
199
200         // convert and inject flowFlags
201         FlowFlagReactor.getInstance().convert(flow.getFlags(), version, flowMod);
202
203         // convert and inject match
204         MatchReactor.getInstance().convert(flow.getMatch(), version, flowMod);
205
206         if (flow.getInstructions() != null) {
207             flowMod.setInstruction(toInstructions(flow, version, datapathid));
208             flowMod.setAction(getActions(version, datapathid, flow));
209         }
210         flowMod.setVersion(version);
211
212         return flowMod;
213     }
214
215     private static void salToOFFlowOutGroup(Flow flow, FlowModInputBuilder flowMod) {
216         if (flow.getOutGroup() != null) {
217             flowMod.setOutGroup(flow.getOutGroup());
218         } else {
219             flowMod.setOutGroup(DEFAULT_OUT_GROUP);
220         }
221     }
222
223     private static void salToOFFlowOutPort(Flow flow, FlowModInputBuilder flowMod) {
224         if (flow.getOutPort() != null) {
225             flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
226         } else {
227             flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
228         }
229     }
230
231     private static void salToOFFlowBufferId(Flow flow, FlowModInputBuilder flowMod) {
232         if (flow.getBufferId() != null) {
233             flowMod.setBufferId(flow.getBufferId());
234         } else {
235             flowMod.setBufferId(DEFAULT_BUFFER_ID);
236         }
237     }
238
239     private static void salToOFFlowPriority(Flow flow, FlowModInputBuilder flowMod) {
240         if (flow.getPriority() != null) {
241             flowMod.setPriority(flow.getPriority());
242         } else {
243             flowMod.setPriority(DEFAULT_PRIORITY);
244         }
245     }
246
247     private static void salToOFFlowHardTimeout(Flow flow, FlowModInputBuilder flowMod) {
248         if (flow.getHardTimeout() != null) {
249             flowMod.setHardTimeout(flow.getHardTimeout());
250         } else {
251             flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
252         }
253     }
254
255     private static void salToOFFlowIdleTimeout(Flow flow, FlowModInputBuilder flowMod) {
256         if (flow.getIdleTimeout() != null) {
257             flowMod.setIdleTimeout(flow.getIdleTimeout());
258         } else {
259             flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
260         }
261     }
262
263     private static void salToOFFlowCommand(Flow flow, FlowModInputBuilder flowMod) {
264         if (flow instanceof AddFlowInput || flow instanceof UpdatedFlow) {
265             flowMod.setCommand(FlowModCommand.OFPFCADD);
266         } else if (flow instanceof RemoveFlowInput) {
267             if (MoreObjects.firstNonNull(flow.isStrict(), Boolean.FALSE)) {
268                 flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT);
269             } else {
270                 flowMod.setCommand(FlowModCommand.OFPFCDELETE);
271             }
272         }
273     }
274
275     private static void salToOFFlowTableId(Flow flow, FlowModInputBuilder flowMod) {
276         if (flow.getTableId() != null) {
277             flowMod.setTableId(new TableId(flow.getTableId().longValue()));
278         } else {
279             flowMod.setTableId(DEFAULT_TABLE_ID);
280         }
281     }
282
283     private static void salToOFFlowCookieMask(Flow flow, FlowModInputBuilder flowMod) {
284         if (flow.getCookieMask() != null) {
285             flowMod.setCookieMask(flow.getCookieMask().getValue());
286         } else {
287             flowMod.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
288         }
289     }
290
291     private static void salToOFFlowCookie(Flow flow, FlowModInputBuilder flowMod) {
292         if (flow.getCookie() != null) {
293             flowMod.setCookie(flow.getCookie().getValue());
294         } else {
295             flowMod.setCookie(OFConstants.DEFAULT_COOKIE);
296         }
297     }
298
299     private static List<Instruction> toInstructions(
300             Flow flow,
301             short version, BigInteger datapathid) {
302         List<Instruction> instructionsList = new ArrayList<>();
303
304         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions = flow.getInstructions();
305         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
306                 .getInstruction()) {
307             InstructionBuilder instructionBuilder = new InstructionBuilder();
308             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
309                     .getInstruction();
310             if (curInstruction instanceof GoToTableCase) {
311                 GoToTableCase goToTablecase = (GoToTableCase) curInstruction;
312                 GoToTable goToTable = goToTablecase.getGoToTable();
313                 GotoTableCaseBuilder gotoTableCaseBuilder = new GotoTableCaseBuilder();
314                 GotoTableBuilder gotoTableBuilder = new GotoTableBuilder();
315                 gotoTableBuilder.setTableId(goToTable.getTableId());
316                 gotoTableCaseBuilder.setGotoTable(gotoTableBuilder.build());
317                 instructionBuilder.setInstructionChoice(gotoTableCaseBuilder.build());
318                 instructionsList.add(instructionBuilder.build());
319             } else if (curInstruction instanceof WriteMetadataCase) {
320                 WriteMetadataCase writeMetadatacase = (WriteMetadataCase) curInstruction;
321                 WriteMetadata writeMetadata = writeMetadatacase.getWriteMetadata();
322
323                 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
324                 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
325                 writeMetadataBuilder.setMetadata(ByteUtil.convertBigIntegerToNBytes(writeMetadata.getMetadata(),
326                                                                              OFConstants.SIZE_OF_LONG_IN_BYTES));
327                 writeMetadataBuilder.setMetadataMask(ByteUtil.convertBigIntegerToNBytes(writeMetadata.getMetadataMask(),
328                                                                                      OFConstants.SIZE_OF_LONG_IN_BYTES));
329                 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
330                 instructionBuilder.setInstructionChoice(writeMetadataCaseBuilder.build());
331                 instructionsList.add(instructionBuilder.build());
332             } else if (curInstruction instanceof WriteActionsCase) {
333                 WriteActionsCase writeActionscase = (WriteActionsCase) curInstruction;
334                 WriteActions writeActions = writeActionscase.getWriteActions();
335                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
336                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
337                 writeActionsBuilder.setAction(ActionConvertor.getActions(writeActions.getAction(),version, datapathid, flow));
338                 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
339                 instructionBuilder.setInstructionChoice(writeActionsCaseBuilder.build());
340                 instructionsList.add(instructionBuilder.build());
341             } else if (curInstruction instanceof ApplyActionsCase) {
342                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
343                 ApplyActions applyActions = applyActionscase.getApplyActions();
344                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder applyActionsCaseBuilder =
345                         new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder();
346                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder applyActionsBuilder =
347                         new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder();
348                 applyActionsBuilder.setAction(ActionConvertor.getActions(applyActions.getAction(), version, datapathid, flow));
349                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
350                 instructionBuilder.setInstructionChoice(applyActionsCaseBuilder.build());
351                 instructionsList.add(instructionBuilder.build());
352             } else if (curInstruction instanceof ClearActionsCase) {
353                 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
354                 instructionBuilder.setInstructionChoice(clearActionsCaseBuilder.build());
355                 instructionsList.add(instructionBuilder.build());
356             } else if (curInstruction instanceof MeterCase) {
357                 MeterCase metercase = (MeterCase) curInstruction;
358                 Meter meter = metercase.getMeter();
359                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
360                 MeterBuilder meterBuilder = new MeterBuilder();
361                 Long meterId = meter.getMeterId().getValue();
362                 meterBuilder.setMeterId(meterId);
363                 meterCaseBuilder.setMeter(meterBuilder.build());
364                 instructionBuilder.setInstructionChoice(meterCaseBuilder.build());
365                 instructionsList.add(instructionBuilder.build());
366             }
367         }
368         return instructionsList;
369     }
370
371     private static List<Action> getActions(short version, BigInteger datapathid, Flow flow) {
372
373         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions = flow.getInstructions();
374         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> sortedInstructions =
375                 INSTRUCTION_ORDERING.sortedCopy(instructions.getInstruction());
376
377         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : sortedInstructions) {
378             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
379                     .getInstruction();
380
381             if (curInstruction instanceof ApplyActionsCase) {
382                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
383                 ApplyActions applyActions = applyActionscase.getApplyActions();
384                 return ActionConvertor.getActions(applyActions.getAction(), version, datapathid, flow);
385             }
386         }
387         return null;
388     }
389
390     // check if set vlanid action is present in the flow
391     private static boolean isSetVlanIdActionCasePresent(Flow flow) {
392         // we are trying to find if there is a set-vlan-id action (OF1.0) action present in the flow.
393         // If yes,then we would need to two flows
394         if (flow.getInstructions() != null) {
395             for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction :
396                     flow.getInstructions().getInstruction()) {
397                 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction =
398                         instruction.getInstruction();
399
400                 if (curInstruction instanceof ApplyActionsCase) {
401                     ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
402                     ApplyActions applyActions = applyActionscase.getApplyActions();
403                     for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action action :
404                             applyActions.getAction()) {
405                         if (action.getAction() instanceof SetVlanIdActionCase) {
406                             return true;
407                         }
408                     }
409                 }
410             }
411         }
412         return false;
413     }
414
415
416     /**
417      * A) If user provided flow's match includes vlan match  and action has set_vlan_field
418      * Install following rules
419      *    1) match on (OFPVID_PRESENT |value) without mask + action [set_field]
420      * <p/>
421      * B) if user provided flow's match doesn't include vlan match but action has set_vlan field
422      *     1) Match on (OFPVID_NONE ) without mask + action [push vlan tag + set_field]
423      *     2) Match on (OFPVID_PRESENT) with mask (OFPVID_PRESENT ) + action [ set_field]
424      */
425     private static List<FlowModInputBuilder> handleSetVlanIdForOF13(Flow srcFlow, short version, BigInteger datapathId) {
426         List<FlowModInputBuilder> list = new ArrayList<>(2);
427
428         final Match srcMatch = Preconditions.checkNotNull(srcFlow.getMatch());
429         final VlanMatch srcVlanMatch = srcMatch.getVlanMatch();
430         if (srcVlanMatch != null) {
431             //create flow with setfield and match
432             // match on vlan tag or vlanid with no mask
433             VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder(srcVlanMatch);
434             VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
435             vlanIdBuilder.setVlanIdPresent(srcVlanMatch.getVlanId().isVlanIdPresent());
436             vlanIdBuilder.setVlanId(srcVlanMatch.getVlanId().getVlanId());
437             vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
438             Match match = new MatchBuilder(srcMatch).setVlanMatch(vlanMatchBuilder.build()).build();
439
440             Optional<? extends Flow> optional = injectMatchToFlow(srcFlow, match);
441             if (optional.isPresent()) {
442                 list.add(toFlowModInput(optional.get(), version, datapathId));
443             }
444         } else {
445             // create 2 flows
446             //flow 1
447             // match on no vlan tag with no mask
448             Match match1 = new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_FALSE).build();
449
450             Optional<? extends Flow> optional1 = injectMatchAndAction(srcFlow, match1);
451             if (optional1.isPresent()) {
452                 list.add(toFlowModInput(optional1.get(), version, datapathId));
453             }
454
455             //flow2
456             // match on vlan tag with mask
457             Match match2 = new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_TRUE).build();
458             Optional<? extends Flow> optional2 = injectMatchToFlow(srcFlow, match2);
459             if (optional2.isPresent()) {
460                 list.add(toFlowModInput(optional2.get(), version, datapathId));
461             }
462         }
463         return list;
464     }
465
466
467     private static Optional<? extends Flow> injectMatchToFlow(Flow sourceFlow, Match match) {
468         if (sourceFlow instanceof AddFlowInput) {
469             return Optional.<AddFlowInput>of(new AddFlowInputBuilder(sourceFlow).setMatch(match).build());
470         } else if (sourceFlow instanceof RemoveFlowInput) {
471             return Optional.<RemoveFlowInput>of(new RemoveFlowInputBuilder(sourceFlow).setMatch(match).build());
472         } else if (sourceFlow instanceof UpdatedFlow) {
473             return Optional.<UpdatedFlow>of(new UpdatedFlowBuilder(sourceFlow).setMatch(match).build());
474         } else {
475             return Optional.<Flow>absent();
476         }
477     }
478
479     private static Optional<? extends Flow> injectMatchAndAction(Flow sourceFlow, Match match) {
480
481         Instructions instructions = (new InstructionsBuilder())
482                 .setInstruction(injectPushActionToInstruction(sourceFlow))
483                 .build();
484
485         if (sourceFlow instanceof AddFlowInput) {
486             return Optional.<AddFlowInput>of(new AddFlowInputBuilder(sourceFlow)
487                     .setMatch(match).setInstructions(instructions).build());
488         } else if (sourceFlow instanceof RemoveFlowInput) {
489             return Optional.<RemoveFlowInput>of(new RemoveFlowInputBuilder(sourceFlow)
490                     .setMatch(match).setInstructions(instructions).build());
491         } else if (sourceFlow instanceof UpdatedFlow) {
492             return Optional.<UpdatedFlow>of(new UpdatedFlowBuilder(sourceFlow)
493                     .setMatch(match).setInstructions(instructions).build());
494         } else {
495             return Optional.<Flow>absent();
496         }
497     }
498
499     private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
500     injectPushActionToInstruction(final Flow sourceFlow) {
501
502         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> srcInstructionList =
503                 sourceFlow.getInstructions().getInstruction();
504
505         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> targetInstructionList = new ArrayList<>(srcInstructionList.size());
506         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> targetActionList = new ArrayList<>();
507
508         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder instructionBuilder =
509                 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder();
510
511         for (int i = 0; i < srcInstructionList.size(); i++) {
512             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction srcInstruction =
513                     srcInstructionList.get(i);
514             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curSrcInstruction =
515                     srcInstruction.getInstruction();
516
517             if (curSrcInstruction instanceof ApplyActionsCase) {
518                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curSrcInstruction;
519                 ApplyActions applyActions = applyActionscase.getApplyActions();
520                 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> srcActionList = applyActions.getAction();
521
522                 int offset = 0;
523                 for (int j = 0; j < srcActionList.size(); j++) {
524                     // check if its a set-vlan-action. If yes, then add the injected-action
525
526                     org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action actionItem = srcActionList.get(j);
527                     if (actionItem.getAction() instanceof SetVlanIdActionCase) {
528                         SetVlanIdActionCase setVlanIdActionCase = (SetVlanIdActionCase) actionItem.getAction();
529
530                         PushVlanActionCaseBuilder pushVlanActionCaseBuilder = new PushVlanActionCaseBuilder();
531                         PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
532
533                         pushVlanActionBuilder.setCfi(new VlanCfi(1))
534                                 .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
535                                 .setEthernetType(PUSH_VLAN)
536                                 .setTag(PUSH_VLAN);
537                         pushVlanActionCaseBuilder.setPushVlanAction(pushVlanActionBuilder.build());
538                         PushVlanActionCase injectedAction = pushVlanActionCaseBuilder.build();
539
540                         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder actionBuilder = new ActionBuilder();
541                         actionBuilder.setAction(injectedAction)
542                                 .setKey(actionItem.getKey())
543                                 .setOrder(actionItem.getOrder() + offset);
544
545                         targetActionList.add(actionBuilder.build());
546                         offset++;
547                     }
548
549                     if (offset > 0) {
550                         // we need to increment the order for all the actions added after injection
551                         org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder actionBuilder =
552                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder(actionItem);
553                         actionBuilder.setOrder(actionItem.getOrder() + offset);
554                         actionItem = actionBuilder.build();
555                     }
556
557                     targetActionList.add(actionItem);
558                 }
559
560                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
561                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
562                 applyActionsBuilder.setAction(targetActionList);
563                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
564
565                 instructionBuilder.setInstruction(applyActionsCaseBuilder.build());
566             } else {
567                 instructionBuilder.setInstruction(curSrcInstruction);
568             }
569
570             instructionBuilder
571                     .setKey(srcInstruction.getKey())
572                     .setOrder(srcInstruction.getOrder());
573             targetInstructionList.add(instructionBuilder.build());
574
575         }
576
577         return targetInstructionList;
578     }
579
580 }