2 * Copyright IBM Corporation, 2013. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
39 public class OFToMDSalFlowConvertor {
42 * Method convert Openflow 1.3+ specific instructions to MD-SAL format
44 * Note: MD-SAL won't augment this data directly to the data store,
45 * so key setting is not required. If user wants to augment this data
46 * directly to the data store, key setting is required for each instructions.
50 public static Instructions toSALInstruction(
51 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions> instructions) {
53 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
55 List<Instruction> salInstructionList = new ArrayList<Instruction>();
57 for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions switchInst : instructions){
58 if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions){
60 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
61 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
62 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
65 applyActionsBuilder.setAction(
67 ActionConvertor.toMDSalActions(actionsInstruction.getActionsList()
71 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
73 InstructionBuilder instBuilder = new InstructionBuilder();
74 instBuilder.setInstruction(new ApplyActionsCaseBuilder().build());
75 salInstructionList.add(instBuilder.build());
76 }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions){
77 InstructionBuilder instBuilder = new InstructionBuilder();
79 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
80 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
81 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
83 instBuilder.setInstruction(clearActionsCaseBuilder.build());
84 salInstructionList.add(instBuilder.build());
85 }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable){
87 TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
89 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
90 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
91 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
92 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
94 InstructionBuilder instBuilder = new InstructionBuilder();
95 instBuilder.setInstruction(goToTableCaseBuilder.build());
96 salInstructionList.add(instBuilder.build());
97 }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter){
99 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
101 InstructionBuilder instBuilder = new InstructionBuilder();
103 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
104 MeterBuilder meterBuilder = new MeterBuilder();
105 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
106 meterCaseBuilder.setMeter(meterBuilder.build());
108 instBuilder.setInstruction(meterCaseBuilder.build());
109 salInstructionList.add(instBuilder.build());
110 }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions){
112 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
114 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
115 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
116 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getActionsList())));
117 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
119 InstructionBuilder instBuilder = new InstructionBuilder();
120 instBuilder.setInstruction(writeActionsCaseBuilder.build());
121 salInstructionList.add(instBuilder.build());
123 }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata){
125 MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
127 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
128 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
129 writeMetadataBuilder.setMetadata(new BigInteger(metadataInstruction.getMetadata()));
130 writeMetadataBuilder.setMetadataMask(new BigInteger(metadataInstruction.getMetadataMask()));
131 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
133 InstructionBuilder instBuilder = new InstructionBuilder();
134 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
135 salInstructionList.add(instBuilder.build());
138 instructionsBuilder.setInstruction(salInstructionList);
139 return instructionsBuilder.build();
143 * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
144 * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
147 private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList){
148 List<Action> actions = new ArrayList<>();
151 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList){
152 ActionBuilder wrappedAction = new ActionBuilder();
153 wrappedAction.setAction(action);
154 wrappedAction.setKey(new ActionKey(actionKey));
155 wrappedAction.setOrder(actionKey);
156 actions.add(wrappedAction.build());