2 * Copyright (c) 2013, 2015 IBM Corporation and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
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;
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
38 public class FlowStatsResponseConvertor {
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
48 public List<FlowAndStatisticsMapList> toSALFlowStatsList(List<FlowStats> allFlowStats, BigInteger datapathid, OpenflowVersion ofVersion){
50 List<FlowAndStatisticsMapList> convertedSALFlowStats = new ArrayList<FlowAndStatisticsMapList>();
52 for(FlowStats flowStats : allFlowStats){
53 convertedSALFlowStats.add(toSALFlowStats(flowStats, datapathid, ofVersion));
56 return convertedSALFlowStats;
60 * Method convert Openflow switch specific flow statistics to the MD-SAL format
62 * @param flowStats flow statis
63 * @param datapathid datapath id
64 * @param ofVersion openflow version
65 * @return flow and statistics map
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()));
73 DurationBuilder time = new DurationBuilder();
74 time.setSecond(new Counter32(flowStats.getDurationSec()));
75 time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
76 salFlowStatsBuilder.setDuration(time.build());
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);
87 Optional<MatchBuilder> matchBuilder = ConvertorManager.getInstance().convert(flowStats.getMatchV10(), data);
89 if (matchBuilder.isPresent()) {
90 salFlowStatsBuilder.setMatch(matchBuilder.get().build());
93 if(flowStats.getAction() != null && flowStats.getAction().size()!=0){
94 salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(flowStats.getAction(), ofVersion));
97 if(flowStats.getMatch() != null){
98 VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(ofVersion.getVersion());
99 data.setDatapathId(datapathid);
101 Optional<MatchBuilder> matchBuilderOptional = ConvertorManager.getInstance().convert(flowStats.getMatch(), data);
103 if (matchBuilderOptional.isPresent()) {
104 MatchBuilder matchBuilder = matchBuilderOptional.get();
106 AugmentTuple<Match> matchExtensionWrap =
107 MatchExtensionHelper.processAllExtensions(
108 flowStats.getMatch().getMatchEntry(), ofVersion, MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
110 if (matchExtensionWrap != null) {
111 matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
114 salFlowStatsBuilder.setMatch(matchBuilder.build());
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()));
124 if(flowStats.getInstruction()!= null){
125 salFlowStatsBuilder.setInstructions(OFToMDSalFlowConvertor.toSALInstruction(flowStats.getInstruction(), ofVersion));
128 return salFlowStatsBuilder.build();