Bug 5540 - PortConvertor, MatchConvertor
[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 java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
18 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
19 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
31
32 /**
33  * Class is an utility class for converting flow related statistics messages coming from openflow
34  * switch to MD-SAL messages.
35  * @author avishnoi@in.ibm.com
36  *
37  */
38 public class FlowStatsResponseConvertor {
39
40     /**
41      * Method returns the list of MD-SAL format flow statistics, converted flow Openflow
42      * specific flow statistics.
43      * @param allFlowStats all flow stats
44      * @param datapathid  datapath id
45      * @param ofVersion  openflow version
46      * @return list of flow and statistics mapping
47      */
48     public List<FlowAndStatisticsMapList> toSALFlowStatsList(List<FlowStats> allFlowStats, BigInteger datapathid, OpenflowVersion ofVersion){
49
50         List<FlowAndStatisticsMapList> convertedSALFlowStats = new ArrayList<FlowAndStatisticsMapList>();
51
52         for(FlowStats flowStats : allFlowStats){
53             convertedSALFlowStats.add(toSALFlowStats(flowStats, datapathid, ofVersion));
54         }
55
56         return convertedSALFlowStats;
57     }
58
59     /**
60      * Method convert Openflow switch specific flow statistics to the MD-SAL format
61      * flow statistics.
62      * @param flowStats flow statis
63      * @param datapathid  datapath id
64      * @param ofVersion  openflow version
65      * @return flow and statistics map
66      */
67     public FlowAndStatisticsMapList toSALFlowStats(FlowStats flowStats, BigInteger datapathid, OpenflowVersion ofVersion){
68         FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
69         salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
70         if(flowStats.getCookie() != null) {
71             salFlowStatsBuilder.setCookie(new FlowCookie(flowStats.getCookie()));
72         }
73         DurationBuilder time = new DurationBuilder();
74         time.setSecond(new Counter32(flowStats.getDurationSec()));
75         time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
76         salFlowStatsBuilder.setDuration(time.build());
77
78         salFlowStatsBuilder.setHardTimeout(flowStats.getHardTimeout());
79         salFlowStatsBuilder.setIdleTimeout(flowStats.getIdleTimeout());
80         salFlowStatsBuilder.setPacketCount(new Counter64(flowStats.getPacketCount()));
81         salFlowStatsBuilder.setPriority(flowStats.getPriority());
82         salFlowStatsBuilder.setTableId(flowStats.getTableId());
83         if(flowStats.getMatchV10() != null){
84             VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(OFConstants.OFP_VERSION_1_0);
85             data.setDatapathId(datapathid);
86
87             Optional<MatchBuilder> matchBuilder = ConvertorManager.getInstance().convert(flowStats.getMatchV10(), data);
88
89             if (matchBuilder.isPresent()) {
90                 salFlowStatsBuilder.setMatch(matchBuilder.get().build());
91             }
92
93             if(flowStats.getAction() != null && flowStats.getAction().size()!=0){
94                 salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(flowStats.getAction(), ofVersion));
95             }
96         }
97         if(flowStats.getMatch() != null){
98             VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(ofVersion.getVersion());
99             data.setDatapathId(datapathid);
100
101             Optional<MatchBuilder> matchBuilderOptional = ConvertorManager.getInstance().convert(flowStats.getMatch(), data);
102
103             if (matchBuilderOptional.isPresent()) {
104                 MatchBuilder matchBuilder = matchBuilderOptional.get();
105
106                 AugmentTuple<Match> matchExtensionWrap =
107                         MatchExtensionHelper.processAllExtensions(
108                                 flowStats.getMatch().getMatchEntry(), ofVersion, MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
109
110                 if (matchExtensionWrap != null) {
111                     matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
112                 }
113
114                 salFlowStatsBuilder.setMatch(matchBuilder.build());
115             }
116
117             salFlowStatsBuilder.setFlags(
118                     new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
119                             flowStats.getFlags().isOFPFFRESETCOUNTS(),
120                             flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
121                             flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
122                             flowStats.getFlags().isOFPFFSENDFLOWREM()));
123         }
124         if(flowStats.getInstruction()!= null){
125             salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.toSALInstruction(flowStats.getInstruction(), ofVersion));
126         }
127
128         return salFlowStatsBuilder.build();
129     }
130 }