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