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