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