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