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