b0cbfb0a8d5482433a77a2871cfe212b4f1ca4cf
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowConvertor.java
1 /**
2  * Copyright (c) 2013 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.List;
14
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flowflag.FlowFlagReactor;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
17 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.ActionList;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTable;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.Meter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActions;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.InstructionsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MatchTypeBase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Utility class for converting a MD-SAL Flow into the OF flow mod
59  */
60 public class FlowConvertor {
61     private static final Logger logger = LoggerFactory.getLogger(FlowConvertor.class);
62
63     // Default values for when things are null
64     private static final BigInteger DEFAULT_COOKIE = BigInteger.ZERO;
65     private static final BigInteger DEFAULT_COOKIE_MASK = BigInteger.ZERO;
66     private static final TableId DEFAULT_TABLE_ID = new TableId(new Long(0));
67     private static final Integer DEFAULT_IDLE_TIMEOUT = new Integer(5 * 60);
68     private static final Integer DEFAULT_HARD_TIMEOUT = new Integer(10 * 60);
69     private static final Integer DEFAULT_PRIORITY = Integer.parseInt("8000", 16);
70     private static final Long DEFAULT_BUFFER_ID = Long.parseLong("ffffffff", 16);
71     private static final Long OFPP_ANY = Long.parseLong("ffffffff", 16);
72     private static final Long DEFAULT_OUT_PORT = OFPP_ANY;
73     private static final Long OFPG_ANY = Long.parseLong("ffffffff", 16);
74     private static final Long DEFAULT_OUT_GROUP = OFPG_ANY;
75     /** flow flag: remove */
76     public static final boolean DEFAULT_OFPFF_FLOW_REM = true;
77     /** flow flag: check overlap */
78     public static final boolean DEFAULT_OFPFF_CHECK_OVERLAP = false;
79     /** flow flag: reset counts */
80     public static final boolean DEFAULT_OFPFF_RESET_COUNTS = false;
81     /** flow flag: don't keep track of packet counts */
82     public static final boolean DEFAULT_OFPFF_NO_PKT_COUNTS = false;
83     /** flow flag: don't keep track of byte counts */
84     public static final boolean DEFAULT_OFPFF_NO_BYT_COUNTS = false;
85     /** flow flag: emergency [OFP-1.0] */
86     public static final boolean DEFAULT_OFPFF_EMERGENCY = false;
87     /** OxmMatch type */
88     public static final Class<? extends MatchTypeBase> DEFAULT_MATCH_TYPE = OxmMatchType.class;
89     /** default match entries - empty */
90     public static final List<MatchEntries> DEFAULT_MATCH_ENTRIES = new ArrayList<MatchEntries>();
91
92     public static FlowModInput toFlowModInput(Flow flow, short version) {
93         FlowModInputBuilder flowMod = new FlowModInputBuilder();
94         if (flow.getCookie() != null) {
95             flowMod.setCookie(flow.getCookie());
96         } else {
97             flowMod.setCookie(DEFAULT_COOKIE);
98         }
99
100         if (flow.getCookieMask() != null) {
101             flowMod.setCookieMask(new BigInteger(flow.getCookieMask().toString()));
102         } else {
103             flowMod.setCookieMask(DEFAULT_COOKIE_MASK);
104         }
105
106         if (flow.getTableId() != null) {
107             flowMod.setTableId(new TableId(flow.getTableId().longValue()));
108         } else {
109             flowMod.setTableId(DEFAULT_TABLE_ID);
110         }
111
112         if (flow instanceof AddFlowInput) {
113             flowMod.setCommand(FlowModCommand.OFPFCADD);
114         } else if (flow instanceof RemoveFlowInput) {
115             if (flow.isStrict() != null && flow.isStrict()) {
116                 flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT);
117             } else {
118                 flowMod.setCommand(FlowModCommand.OFPFCDELETE);
119             }
120         } else if (flow instanceof UpdatedFlow) {
121             if (flow.isStrict() != null && flow.isStrict()) {
122                 flowMod.setCommand(FlowModCommand.OFPFCMODIFYSTRICT);
123             } else {
124                 flowMod.setCommand(FlowModCommand.OFPFCMODIFY);
125             }
126         }
127         if (flow.getIdleTimeout() != null) {
128             flowMod.setIdleTimeout(flow.getIdleTimeout());
129         } else {
130             flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
131         }
132         if (flow.getHardTimeout() != null) {
133             flowMod.setHardTimeout(flow.getHardTimeout());
134         } else {
135             flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
136         }
137         if (flow.getPriority() != null) {
138             flowMod.setPriority(flow.getPriority());
139         } else {
140             flowMod.setPriority(DEFAULT_PRIORITY);
141         }
142         if (flow.getBufferId() != null) {
143             flowMod.setBufferId(flow.getBufferId());
144         } else {
145             flowMod.setBufferId(DEFAULT_BUFFER_ID);
146         }
147
148         if (flow.getOutPort() != null) {
149             flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
150         } else {
151             flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
152         }
153         if (flow.getOutGroup() != null) {
154             flowMod.setOutGroup(flow.getOutGroup());
155         } else {
156             flowMod.setOutGroup(DEFAULT_OUT_GROUP);
157         }
158
159         
160         // convert and inject flowFlags
161         FlowFlagReactor.getInstance().convert(flow.getFlags(), version, flowMod);
162
163         // convert and inject match
164         MatchReactor.getInstance().convert(flow.getMatch(), version, flowMod);
165
166         if (flow.getInstructions() != null) {
167             flowMod.setInstructions(toInstructions(flow.getInstructions(), version));
168             flowMod.setActionsList(getActionsList(flow.getInstructions(), version));
169         }
170         flowMod.setVersion(version);
171         
172         return flowMod.build();
173     }
174
175     private static List<Instructions> toInstructions(
176             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions,
177             short version) {
178         List<Instructions> instructionsList = new ArrayList<>();
179
180         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
181                 .getInstruction()) {
182             InstructionsBuilder instructionBuilder = new InstructionsBuilder();
183             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
184                     .getInstruction();
185             if (curInstruction instanceof GoToTableCase) {
186                 GoToTableCase goToTablecase = (GoToTableCase) curInstruction;
187                 GoToTable goToTable = goToTablecase.getGoToTable();
188                 instructionBuilder
189                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class);
190                 TableIdInstructionBuilder tableBuilder = new TableIdInstructionBuilder();
191                 tableBuilder.setTableId(goToTable.getTableId());
192                 instructionBuilder.addAugmentation(TableIdInstruction.class, tableBuilder.build());
193                 instructionsList.add(instructionBuilder.build());
194             }
195
196             else if (curInstruction instanceof WriteMetadataCase) {
197                 WriteMetadataCase writeMetadatacase = (WriteMetadataCase) curInstruction;
198                 WriteMetadata writeMetadata = writeMetadatacase.getWriteMetadata();
199                 instructionBuilder
200                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class);
201                 MetadataInstructionBuilder metadataBuilder = new MetadataInstructionBuilder();
202                 metadataBuilder.setMetadata(ByteUtil.convertBigIntegerTo64Bit(writeMetadata.getMetadata()));
203                 metadataBuilder
204                         .setMetadataMask(ByteUtil.convertBigIntegerTo64Bit(writeMetadata.getMetadataMask()));
205                 instructionBuilder.addAugmentation(MetadataInstruction.class, metadataBuilder.build());
206                 instructionsList.add(instructionBuilder.build());
207             }
208
209             else if (curInstruction instanceof WriteActionsCase) {
210                 WriteActionsCase writeActionscase = (WriteActionsCase) curInstruction;
211                 WriteActions writeActions = writeActionscase.getWriteActions();
212                 instructionBuilder
213                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class);
214                 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
215                 actionsInstructionBuilder.setActionsList(ActionConvertor.getActionList(writeActions.getAction(),
216                         version));
217                 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
218                 instructionsList.add(instructionBuilder.build());
219             }
220
221             else if (curInstruction instanceof ApplyActionsCase) {
222                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
223                 ApplyActions applyActions = applyActionscase.getApplyActions();
224                 instructionBuilder
225                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
226                 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
227                 actionsInstructionBuilder.setActionsList(ActionConvertor.getActionList(applyActions.getAction(),
228                         version));
229                 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
230                 instructionsList.add(instructionBuilder.build());
231             }
232
233             else if (curInstruction instanceof ClearActionsCase) {
234                 ClearActionsCase clearActionscase = (ClearActionsCase) curInstruction;
235                 ClearActions clearActions = clearActionscase.getClearActions();
236                 instructionBuilder
237                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class);
238                 ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
239                 actionsInstructionBuilder.setActionsList(ActionConvertor.getActionList(clearActions.getAction(),
240                         version));
241                 instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
242                 instructionsList.add(instructionBuilder.build());
243             }
244
245             else if (curInstruction instanceof MeterCase) {
246                 MeterCase metercase = (MeterCase) curInstruction;
247                 Meter meter = metercase.getMeter();
248                 instructionBuilder
249                         .setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class);
250                 MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();
251                 Long meterId = meter.getMeterId().getValue();
252                 meterBuilder.setMeterId(meterId);
253                 instructionBuilder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());
254                 instructionsList.add(instructionBuilder.build());
255             }
256         }
257         return instructionsList;
258     }
259     
260     private static List<ActionsList> getActionsList(
261             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions instructions,
262             short version) {
263
264         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction instruction : instructions
265                 .getInstruction()) {
266             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction curInstruction = instruction
267                     .getInstruction();
268
269             if (curInstruction instanceof ApplyActionsCase) {
270                 ApplyActionsCase applyActionscase = (ApplyActionsCase) curInstruction;
271                 ApplyActions applyActions = applyActionscase.getApplyActions();
272                 return ActionConvertor.getActionList(applyActions.getAction(), version);
273             }
274
275         }
276         return null;
277     }
278 }