MS-SAL Statistics Manager - Minor bug fixing in group/meter notifications
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowStatsResponseConvertor.java
1 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
7 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter32;
8 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter64;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
23
24 /**
25  * Class is an utility class for converting flow related statistics messages coming from openflow 
26  * switch to MD-SAL messages.
27  * @author avishnoi@in.ibm.com
28  *
29  */
30 public class FlowStatsResponseConvertor {
31     
32     /**
33      * Method returns the list of MD-SAL format flow statistics, converted flow Openflow
34      * specific flow statistics. 
35      * @param allFlowStats
36      * @return
37      */
38     public List<FlowAndStatisticsMapList> toSALFlowStatsList(List<FlowStats> allFlowStats){
39         
40         List<FlowAndStatisticsMapList> convertedSALFlowStats = new ArrayList<FlowAndStatisticsMapList>();
41         
42         for(FlowStats flowStats : allFlowStats){
43             convertedSALFlowStats.add(toSALFlowStats(flowStats));
44         }
45         
46         return convertedSALFlowStats;
47     }
48
49     /**
50      * Method convert Openflow switch specific flow statistics to the MD-SAL format 
51      * flow statistics.
52      * @param flowStats
53      * @return
54      */
55     public FlowAndStatisticsMapList toSALFlowStats(FlowStats flowStats){
56         FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
57         salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
58         salFlowStatsBuilder.setCookie(flowStats.getCookie());
59
60         DurationBuilder time = new DurationBuilder();
61         time.setSecond(new Counter32(flowStats.getDurationSec()));
62         time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
63         salFlowStatsBuilder.setDuration(time.build());
64         
65         salFlowStatsBuilder.setFlags(
66                 new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
67                         flowStats.getFlags().isOFPFFRESETCOUNTS(),
68                         flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
69                         flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
70                         flowStats.getFlags().isOFPFFSENDFLOWREM()));
71         
72         salFlowStatsBuilder.setHardTimeout(flowStats.getHardTimeout());
73         salFlowStatsBuilder.setIdleTimeout(flowStats.getIdleTimeout());
74         salFlowStatsBuilder.setPacketCount(new Counter64(flowStats.getPacketCount()));
75         salFlowStatsBuilder.setPriority(flowStats.getPriority());
76         salFlowStatsBuilder.setTableId(flowStats.getTableId());
77         if(flowStats.getMatchV10() != null){
78             salFlowStatsBuilder.setMatch(MatchConvertorImpl.fromOFMatchV10ToSALMatch(flowStats.getMatchV10()));
79         }
80         if(flowStats.getMatch() != null){
81             salFlowStatsBuilder.setMatch(MatchConvertorImpl.fromOFMatchToSALMatch(flowStats.getMatch()));
82         }
83         if(flowStats.getInstructions()!= null){
84             salFlowStatsBuilder.setInstructions(toSALInstruction(flowStats.getInstructions()));
85         }
86         
87         return salFlowStatsBuilder.build();
88         
89     }
90     
91     /**
92      * Method convert Openflow 1.3+ specific instructions to MD-SAL format
93      * flow instruction
94      * Note: MD-SAL won't augment this data directly to the data store, 
95      * so key setting is not required. If user wants to augment this data
96      * directly to the data store, key setting is required for each instructions. 
97      * @param instructions
98      * @return
99      */
100     public Instructions toSALInstruction(
101             List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions> instructions) {
102         
103         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
104         
105         List<Instruction> salInstructionList = new ArrayList<Instruction>();
106         
107         for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions switchInst : instructions){
108             if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions){
109                 InstructionBuilder instBuilder = new InstructionBuilder();
110                 instBuilder.setInstruction(new ApplyActionsCaseBuilder().build());
111                 salInstructionList.add(instBuilder.build());
112             }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions){
113                 InstructionBuilder instBuilder = new InstructionBuilder();
114                 instBuilder.setInstruction(new ClearActionsCaseBuilder().build());
115                 salInstructionList.add(instBuilder.build());
116             }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable){
117                 InstructionBuilder instBuilder = new InstructionBuilder();
118                 instBuilder.setInstruction(new GoToTableCaseBuilder().build());
119                 salInstructionList.add(instBuilder.build());
120             }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions){
121                 InstructionBuilder instBuilder = new InstructionBuilder();
122                 instBuilder.setInstruction(new WriteActionsCaseBuilder().build());
123                 salInstructionList.add(instBuilder.build());
124             }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata){
125                 InstructionBuilder instBuilder = new InstructionBuilder();
126                 instBuilder.setInstruction(new WriteMetadataCaseBuilder().build());
127                 salInstructionList.add(instBuilder.build());
128             }
129         }
130         instructionsBuilder.setInstruction(salInstructionList);
131         return instructionsBuilder.build();
132     }
133 }