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