84c8b69e2a51821c5e7d66f43b495d295563c250
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / flow / 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.flow;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.Set;
17 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
18 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
19 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
20 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
21 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
43 import org.opendaylight.yangtools.yang.binding.DataContainer;
44
45 /**
46  * Converts flow related statistics messages coming from openflow switch to MD-SAL messages.
47  *
48  * Example usage:
49  * <pre>
50  * {@code
51  * VersionDatapathIdConvertorData data = new VersionDatapathIdConvertorData(version);
52  * data.setDatapathId(datapathId);
53  * Optional<List<FlowAndStatisticsMapList>> salFlowStats = convertorManager.convert(ofFlowStats, data);
54  * }
55  * </pre>
56  */
57 public class FlowStatsResponseConvertor extends Convertor<List<FlowStats>, List<FlowAndStatisticsMapList>, VersionDatapathIdConvertorData> {
58
59     private static final Set<Class<? extends DataContainer>> TYPES = Collections.singleton(FlowStats.class);
60
61     /**
62      * Method wraps openflow 1.0 actions list to Apply Action Instructions
63      *
64      * @param actionsList list of action
65      * @return OF10 actions as an instructions
66      */
67     private Instructions wrapOF10ActionsToInstruction(List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action> actionsList, final short version) {
68         ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(version);
69         actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION);
70
71         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
72         List<Instruction> salInstructionList = new ArrayList<>();
73
74         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
75         ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
76
77         final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>> actions = getConvertorExecutor().convert(
78                 actionsList, actionResponseConvertorData);
79
80         applyActionsBuilder.setAction(FlowConvertorUtil.wrapActionList(actions.orElse(Collections.emptyList())));
81         applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
82
83         InstructionBuilder instBuilder = new InstructionBuilder();
84         instBuilder.setInstruction(applyActionsCaseBuilder.build());
85         instBuilder.setKey(new InstructionKey(0));
86         instBuilder.setOrder(0);
87         salInstructionList.add(instBuilder.build());
88
89         instructionsBuilder.setInstruction(salInstructionList);
90         return instructionsBuilder.build();
91     }
92
93     @Override
94     public Collection<Class<? extends DataContainer>> getTypes() {
95         return TYPES;
96     }
97
98     @Override
99     public List<FlowAndStatisticsMapList> convert(List<FlowStats> source, VersionDatapathIdConvertorData data) {
100         final List<FlowAndStatisticsMapList> result = new ArrayList<>();
101
102         for (FlowStats flowStats : source) {
103             // Convert Openflow switch specific flow statistics to the MD-SAL format flow statistics
104             FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
105             salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
106
107             if (flowStats.getCookie() != null) {
108                 salFlowStatsBuilder.setCookie(new FlowCookie(flowStats.getCookie()));
109             }
110
111             DurationBuilder time = new DurationBuilder();
112             time.setSecond(new Counter32(flowStats.getDurationSec()));
113             time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
114             salFlowStatsBuilder.setDuration(time.build());
115
116             salFlowStatsBuilder.setHardTimeout(flowStats.getHardTimeout());
117             salFlowStatsBuilder.setIdleTimeout(flowStats.getIdleTimeout());
118             salFlowStatsBuilder.setPacketCount(new Counter64(flowStats.getPacketCount()));
119             salFlowStatsBuilder.setPriority(flowStats.getPriority());
120             salFlowStatsBuilder.setTableId(flowStats.getTableId());
121
122             if (flowStats.getMatchV10() != null) {
123                 final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(flowStats.getMatchV10(), data);
124
125                 if (matchBuilderOptional.isPresent()) {
126                     salFlowStatsBuilder.setMatch(matchBuilderOptional.get().build());
127                 }
128
129                 if (flowStats.getAction() != null && flowStats.getAction().size() != 0) {
130                     salFlowStatsBuilder.setInstructions(wrapOF10ActionsToInstruction(flowStats.getAction(), data.getVersion()));
131                 }
132             }
133
134             if (flowStats.getMatch() != null) {
135                 final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(flowStats.getMatch(), data);
136
137                 if (matchBuilderOptional.isPresent()) {
138                     final MatchBuilder matchBuilder = matchBuilderOptional.get();
139
140                     final AugmentTuple<Match> matchExtensionWrap =
141                             MatchExtensionHelper.processAllExtensions(
142                                     flowStats.getMatch().getMatchEntry(),
143                                     OpenflowVersion.get(data.getVersion()),
144                                     MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
145
146                     if (matchExtensionWrap != null) {
147                         matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
148                     }
149
150                     salFlowStatsBuilder.setMatch(matchBuilder.build());
151                 }
152
153
154                 salFlowStatsBuilder.setFlags(
155                         new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
156                                 flowStats.getFlags().isOFPFFRESETCOUNTS(),
157                                 flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
158                                 flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
159                                 flowStats.getFlags().isOFPFFSENDFLOWREM()));
160             }
161
162             if (flowStats.getInstruction() != null) {
163                 final VersionConvertorData simpleConvertorData = new VersionConvertorData(data.getVersion());
164                 final Optional<Instructions> instructions = getConvertorExecutor().convert(
165                         flowStats.getInstruction(), simpleConvertorData);
166
167                 salFlowStatsBuilder.setInstructions(instructions.orElse(new InstructionsBuilder()
168                         .setInstruction(Collections.emptyList()).build()));
169             }
170
171             result.add(salFlowStatsBuilder.build());
172         }
173
174         return result;
175     }
176 }