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