Bug-835 - Reserve Ports should be logical ports
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / OFToMDSalFlowConvertor.java
1 /*
2  * Copyright IBM Corporation, 2013.  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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
9
10 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowVersion;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
36
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 public class OFToMDSalFlowConvertor {
42     
43     /**
44      * Method convert Openflow 1.3+ specific instructions to MD-SAL format
45      * flow instruction
46      * @param instructions
47      * @param ofVersion current ofp version
48      * @return
49      */
50     public static Instructions toSALInstruction(
51             List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction> instructions, OpenflowVersion ofVersion) {
52         
53         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
54         
55         List<Instruction> salInstructionList = new ArrayList<Instruction>();
56         int instructionTreeNodekey=0;
57         for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions){
58             if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class)){
59                 
60                 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
61                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
62                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
63                 
64                 
65                 applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion)));
66                 
67                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
68                 
69                 InstructionBuilder instBuilder = new InstructionBuilder();
70                 instBuilder.setInstruction(applyActionsCaseBuilder.build());
71                 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
72                 instBuilder.setOrder(instructionTreeNodekey);
73                 instructionTreeNodekey++;
74                 salInstructionList.add(instBuilder.build());
75             }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)){
76                 InstructionBuilder instBuilder = new InstructionBuilder();
77                 
78                 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
79                 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
80                 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
81                 
82                 instBuilder.setInstruction(clearActionsCaseBuilder.build());
83                 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
84                 instBuilder.setOrder(instructionTreeNodekey);
85                 instructionTreeNodekey++;
86
87                 salInstructionList.add(instBuilder.build());
88             }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)){
89                 
90                 TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
91                 
92                 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
93                 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
94                 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
95                 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
96                 
97                 InstructionBuilder instBuilder = new InstructionBuilder();
98                 instBuilder.setInstruction(goToTableCaseBuilder.build());
99                 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
100                 instBuilder.setOrder(instructionTreeNodekey);
101                 instructionTreeNodekey++;
102
103                 salInstructionList.add(instBuilder.build());
104             }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)){
105                 
106                 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
107                 
108                 InstructionBuilder instBuilder = new InstructionBuilder();
109                 
110                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
111                 MeterBuilder meterBuilder = new MeterBuilder();
112                 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
113                 meterCaseBuilder.setMeter(meterBuilder.build());
114                 
115                 instBuilder.setInstruction(meterCaseBuilder.build());
116                 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
117                 instBuilder.setOrder(instructionTreeNodekey);
118                 instructionTreeNodekey++;
119
120                 salInstructionList.add(instBuilder.build());
121             }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)){
122                 
123                 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
124                 
125                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
126                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
127                 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion)));
128                 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
129                 
130                 InstructionBuilder instBuilder = new InstructionBuilder();
131                 instBuilder.setInstruction(writeActionsCaseBuilder.build());
132                 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
133                 instBuilder.setOrder(instructionTreeNodekey);
134                 instructionTreeNodekey++;
135
136                 salInstructionList.add(instBuilder.build());
137             
138             }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)){
139
140                 MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
141                 
142                 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
143                 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
144                 writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
145                 writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
146                 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
147                 
148                 InstructionBuilder instBuilder = new InstructionBuilder();
149                 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
150                 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
151                 instBuilder.setOrder(instructionTreeNodekey);
152                 instructionTreeNodekey++;
153
154                 salInstructionList.add(instBuilder.build());
155             }
156         }
157         instructionsBuilder.setInstruction(salInstructionList);
158         return instructionsBuilder.build();
159     }
160     
161     /*
162      * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
163      * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
164      * for actions. 
165      */
166     private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList){
167         List<Action> actions = new ArrayList<>(); 
168         
169         int actionKey = 0;
170         for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList){
171             ActionBuilder wrappedAction = new ActionBuilder();
172             wrappedAction.setAction(action);
173             wrappedAction.setKey(new ActionKey(actionKey));
174             wrappedAction.setOrder(actionKey);
175             actions.add(wrappedAction.build());
176             actionKey++;
177         }
178         
179         return actions;
180     }
181
182     /**
183      * Method wraps openflow 1.0 actions list to Apply Action Instructions
184      * @param ofVersion current ofp version
185      */
186     
187     public static Instructions wrapOF10ActionsToInstruction(
188             List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action> actionsList, OpenflowVersion ofVersion) {
189         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
190         
191         List<Instruction> salInstructionList = new ArrayList<Instruction>();
192         
193         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
194         ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
195
196         applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsList, ofVersion)));
197                 
198         applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
199                 
200         InstructionBuilder instBuilder = new InstructionBuilder();
201         instBuilder.setInstruction(applyActionsCaseBuilder.build());
202         instBuilder.setKey(new InstructionKey(0));
203         instBuilder.setOrder(0);
204         salInstructionList.add(instBuilder.build());
205         
206         instructionsBuilder.setInstruction(salInstructionList);
207         return instructionsBuilder.build();
208     }
209     
210 }