Added notification for Flow add/delete/update
[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 java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13
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;
38
39 public class OFToMDSalFlowConvertor {
40     
41     /**
42      * Method convert Openflow 1.3+ specific instructions to MD-SAL format
43      * flow instruction
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. 
47      * @param instructions
48      * @return
49      */
50     public static Instructions toSALInstruction(
51             List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions> instructions) {
52         
53         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
54         
55         List<Instruction> salInstructionList = new ArrayList<Instruction>();
56         
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){
59                 
60                 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
61                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
62                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
63                 
64                 
65                 applyActionsBuilder.setAction(
66                             wrapActionList(
67                                     ActionConvertor.toMDSalActions(actionsInstruction.getActionsList()
68                                             )
69                                             ));
70                 
71                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
72                 
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();
78                 
79                 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
80                 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
81                 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
82                 
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){
86                 
87                 TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
88                 
89                 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
90                 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
91                 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
92                 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
93                 
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){
98                 
99                 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
100                 
101                 InstructionBuilder instBuilder = new InstructionBuilder();
102                 
103                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
104                 MeterBuilder meterBuilder = new MeterBuilder();
105                 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
106                 meterCaseBuilder.setMeter(meterBuilder.build());
107                 
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){
111                 
112                 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
113                 
114                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
115                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
116                 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getActionsList())));
117                 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
118                 
119                 InstructionBuilder instBuilder = new InstructionBuilder();
120                 instBuilder.setInstruction(writeActionsCaseBuilder.build());
121                 salInstructionList.add(instBuilder.build());
122             
123             }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata){
124
125                 MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
126                 
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());
132                 
133                 InstructionBuilder instBuilder = new InstructionBuilder();
134                 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
135                 salInstructionList.add(instBuilder.build());
136             }
137         }
138         instructionsBuilder.setInstruction(salInstructionList);
139         return instructionsBuilder.build();
140     }
141     
142     /*
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
145      * for actions. 
146      */
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<>(); 
149         
150         int actionKey = 0;
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());
157             actionKey++;
158         }
159         
160         return actions;
161     }
162
163
164 }