c3e8fc29d5c9e1fea3a1687a349b0240e7bc31a5
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / FlowStatsResponseConvertor.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.extension.api.AugmentTuple;
11 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
12 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
13 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter32;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter64;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
25
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31  * Class is an utility class for converting flow related statistics messages coming from openflow
32  * switch to MD-SAL messages.
33  * @author avishnoi@in.ibm.com
34  *
35  */
36 public class FlowStatsResponseConvertor {
37
38     /**
39      * Method returns the list of MD-SAL format flow statistics, converted flow Openflow
40      * specific flow statistics.
41      * @param allFlowStats
42      * @return
43      */
44     public List<FlowAndStatisticsMapList> toSALFlowStatsList(List<FlowStats> allFlowStats, BigInteger datapathid, OpenflowVersion ofVersion){
45
46         List<FlowAndStatisticsMapList> convertedSALFlowStats = new ArrayList<FlowAndStatisticsMapList>();
47
48         for(FlowStats flowStats : allFlowStats){
49             convertedSALFlowStats.add(toSALFlowStats(flowStats, datapathid, ofVersion));
50         }
51
52         return convertedSALFlowStats;
53     }
54
55     /**
56      * Method convert Openflow switch specific flow statistics to the MD-SAL format
57      * flow statistics.
58      * @param flowStats
59      * @return
60      */
61     public FlowAndStatisticsMapList toSALFlowStats(FlowStats flowStats, BigInteger datapathid, OpenflowVersion ofVersion){
62         FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
63         salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
64         if(flowStats.getCookie() != null) {
65             salFlowStatsBuilder.setCookie(new FlowCookie(flowStats.getCookie()));
66         }
67         DurationBuilder time = new DurationBuilder();
68         time.setSecond(new Counter32(flowStats.getDurationSec()));
69         time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
70         salFlowStatsBuilder.setDuration(time.build());
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(),datapathid, OpenflowVersion.OF10));
79             if(flowStats.getAction() != null && flowStats.getAction().size()!=0){
80                 salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(flowStats.getAction(), ofVersion));
81             }
82         }
83         if(flowStats.getMatch() != null){
84             MatchBuilder matchBuilder = MatchConvertorImpl.fromOFMatchToSALMatch(flowStats.getMatch(),datapathid, ofVersion);
85             
86             AugmentTuple<Match> matchExtensionWrap = 
87                     MatchExtensionHelper.processAllExtensions(
88                             flowStats.getMatch().getMatchEntries(), ofVersion, MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
89             if (matchExtensionWrap != null) {
90                 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
91             }
92             
93             
94             salFlowStatsBuilder.setMatch(matchBuilder.build());
95             salFlowStatsBuilder.setFlags(
96                     new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
97                             flowStats.getFlags().isOFPFFRESETCOUNTS(),
98                             flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
99                             flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
100                             flowStats.getFlags().isOFPFFSENDFLOWREM()));
101         }
102         if(flowStats.getInstruction()!= null){
103             salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.toSALInstruction(flowStats.getInstruction(), ofVersion));
104         }
105
106         return salFlowStatsBuilder.build();
107     }
108 }