4dbcd265bf214c2e487e4aec70d78d09df2206cf
[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 java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorImpl;
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.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,BigInteger datapathid){
39         
40         List<FlowAndStatisticsMapList> convertedSALFlowStats = new ArrayList<FlowAndStatisticsMapList>();
41         
42         for(FlowStats flowStats : allFlowStats){
43             convertedSALFlowStats.add(toSALFlowStats(flowStats, datapathid));
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,BigInteger datapathid){
56         FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
57         salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
58         salFlowStatsBuilder.setCookie(new FlowCookie(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.setHardTimeout(flowStats.getHardTimeout());
66         salFlowStatsBuilder.setIdleTimeout(flowStats.getIdleTimeout());
67         salFlowStatsBuilder.setPacketCount(new Counter64(flowStats.getPacketCount()));
68         salFlowStatsBuilder.setPriority(flowStats.getPriority());
69         salFlowStatsBuilder.setTableId(flowStats.getTableId());
70         if(flowStats.getMatchV10() != null){
71             salFlowStatsBuilder.setMatch(MatchConvertorImpl.fromOFMatchV10ToSALMatch(flowStats.getMatchV10(),datapathid));
72             if(flowStats.getAction().size()!=0){
73                 salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(flowStats.getAction()));
74             }
75         }
76         if(flowStats.getMatch() != null){
77             salFlowStatsBuilder.setMatch(MatchConvertorImpl.fromOFMatchToSALMatch(flowStats.getMatch(),datapathid));
78             salFlowStatsBuilder.setFlags(
79                     new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
80                             flowStats.getFlags().isOFPFFRESETCOUNTS(),
81                             flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
82                             flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
83                             flowStats.getFlags().isOFPFFSENDFLOWREM()));
84         }
85         if(flowStats.getInstruction()!= null){
86             salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.toSALInstruction(flowStats.getInstruction()));
87         }
88         
89         return salFlowStatsBuilder.build();
90     }
91 }