Minor refactoring due to change in the library model
[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.FlowModFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
22
23 /**
24  * Class is an utility class for converting flow related statistics messages coming from openflow 
25  * switch to MD-SAL messages.
26  * @author avishnoi@in.ibm.com
27  *
28  */
29 public class FlowStatsResponseConvertor {
30     
31     /**
32      * Method returns the list of MD-SAL format flow statistics, converted flow Openflow
33      * specific flow statistics. 
34      * @param allFlowStats
35      * @return
36      */
37     public List<FlowAndStatisticsMapList> toSALFlowStatsList(List<FlowStats> allFlowStats,BigInteger datapathid){
38         
39         List<FlowAndStatisticsMapList> convertedSALFlowStats = new ArrayList<FlowAndStatisticsMapList>();
40         
41         for(FlowStats flowStats : allFlowStats){
42             convertedSALFlowStats.add(toSALFlowStats(flowStats, datapathid));
43         }
44         
45         return convertedSALFlowStats;
46     }
47
48     /**
49      * Method convert Openflow switch specific flow statistics to the MD-SAL format 
50      * flow statistics.
51      * @param flowStats
52      * @return
53      */
54     public FlowAndStatisticsMapList toSALFlowStats(FlowStats flowStats,BigInteger datapathid){
55         FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
56         salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
57         salFlowStatsBuilder.setCookie(flowStats.getCookie());
58
59         DurationBuilder time = new DurationBuilder();
60         time.setSecond(new Counter32(flowStats.getDurationSec()));
61         time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
62         salFlowStatsBuilder.setDuration(time.build());
63         
64         salFlowStatsBuilder.setHardTimeout(flowStats.getHardTimeout());
65         salFlowStatsBuilder.setIdleTimeout(flowStats.getIdleTimeout());
66         salFlowStatsBuilder.setPacketCount(new Counter64(flowStats.getPacketCount()));
67         salFlowStatsBuilder.setPriority(flowStats.getPriority());
68         salFlowStatsBuilder.setTableId(flowStats.getTableId());
69         if(flowStats.getMatchV10() != null){
70             salFlowStatsBuilder.setMatch(MatchConvertorImpl.fromOFMatchV10ToSALMatch(flowStats.getMatchV10(),datapathid));
71             if(flowStats.getAction().size()!=0){
72                 salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(flowStats.getAction()));
73             }
74         }
75         if(flowStats.getMatch() != null){
76             salFlowStatsBuilder.setMatch(MatchConvertorImpl.fromOFMatchToSALMatch(flowStats.getMatch(),datapathid));
77             salFlowStatsBuilder.setFlags(
78                     new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
79                             flowStats.getFlags().isOFPFFRESETCOUNTS(),
80                             flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
81                             flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
82                             flowStats.getFlags().isOFPFFSENDFLOWREM()));
83         }
84         if(flowStats.getInstruction()!= null){
85             salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.toSALInstruction(flowStats.getInstruction()));
86         }
87         
88         return salFlowStatsBuilder.build();
89     }
90 }