Merge "Remove dependency of impl on onf-extensions"
[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.Objects;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
19 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
20 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
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.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>, FlowStatsResponseConvertorData> {
58
59     private static final Set<Class<?>> 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      * @param ipProtocol ip protocol
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,
69                                                       final short version,
70                                                       final Short ipProtocol) {
71         ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(version);
72         actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION);
73         actionResponseConvertorData.setIpProtocol(ipProtocol);
74
75         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
76         List<Instruction> salInstructionList = new ArrayList<>();
77
78         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
79         ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
80
81         final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>> actions = getConvertorExecutor().convert(
82                 actionsList, actionResponseConvertorData);
83
84         applyActionsBuilder.setAction(FlowConvertorUtil.wrapActionList(actions.orElse(Collections.emptyList())));
85         applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
86
87         InstructionBuilder instBuilder = new InstructionBuilder();
88         instBuilder.setInstruction(applyActionsCaseBuilder.build());
89         instBuilder.setKey(new InstructionKey(0));
90         instBuilder.setOrder(0);
91         salInstructionList.add(instBuilder.build());
92
93         instructionsBuilder.setInstruction(salInstructionList);
94         return instructionsBuilder.build();
95     }
96
97     @Override
98     public Collection<Class<?>> getTypes() {
99         return TYPES;
100     }
101
102     @Override
103     public List<FlowAndStatisticsMapList> convert(List<FlowStats> source, FlowStatsResponseConvertorData data) {
104         final List<FlowAndStatisticsMapList> result = new ArrayList<>();
105
106         for (FlowStats flowStats : source) {
107             // Convert Openflow switch specific flow statistics to the MD-SAL format flow statistics
108             FlowAndStatisticsMapListBuilder salFlowStatsBuilder = new FlowAndStatisticsMapListBuilder();
109             salFlowStatsBuilder.setByteCount(new Counter64(flowStats.getByteCount()));
110
111             if (flowStats.getCookie() != null) {
112                 salFlowStatsBuilder.setCookie(new FlowCookie(flowStats.getCookie()));
113             }
114
115             DurationBuilder time = new DurationBuilder();
116             time.setSecond(new Counter32(flowStats.getDurationSec()));
117             time.setNanosecond(new Counter32(flowStats.getDurationNsec()));
118             salFlowStatsBuilder.setDuration(time.build());
119
120             salFlowStatsBuilder.setHardTimeout(flowStats.getHardTimeout());
121             salFlowStatsBuilder.setIdleTimeout(flowStats.getIdleTimeout());
122             salFlowStatsBuilder.setPacketCount(new Counter64(flowStats.getPacketCount()));
123             salFlowStatsBuilder.setPriority(flowStats.getPriority());
124             salFlowStatsBuilder.setTableId(flowStats.getTableId());
125
126             Short ipProtocol = null;
127
128             if (flowStats.getMatchV10() != null) {
129                 final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(flowStats.getMatchV10(), data);
130
131                 if (matchBuilderOptional.isPresent()) {
132                     if (Objects.nonNull(matchBuilderOptional.get().getIpMatch())) {
133                         ipProtocol = matchBuilderOptional.get().getIpMatch().getIpProtocol();
134                     }
135
136                     salFlowStatsBuilder.setMatch(matchBuilderOptional.get().build());
137                 }
138
139                 if (flowStats.getAction() != null && flowStats.getAction().size() != 0) {
140                     salFlowStatsBuilder.setInstructions(wrapOF10ActionsToInstruction(flowStats.getAction(), data.getVersion(), ipProtocol));
141                 }
142             }
143
144             if (flowStats.getMatch() != null) {
145                 final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(flowStats.getMatch(), data);
146
147                 if (matchBuilderOptional.isPresent()) {
148                     final MatchBuilder matchBuilder = matchBuilderOptional.get();
149
150                     final AugmentTuple<Match> matchExtensionWrap =
151                             MatchExtensionHelper.processAllExtensions(
152                                     flowStats.getMatch().getMatchEntry(),
153                                     OpenflowVersion.get(data.getVersion()),
154                                     data.getMatchPath());
155
156                     if (matchExtensionWrap != null) {
157                         matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
158                     }
159
160                     salFlowStatsBuilder.setMatch(matchBuilder.build());
161                 }
162
163
164                 salFlowStatsBuilder.setFlags(
165                         new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
166                                 flowStats.getFlags().isOFPFFRESETCOUNTS(),
167                                 flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
168                                 flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
169                                 flowStats.getFlags().isOFPFFSENDFLOWREM()));
170             }
171
172             if (flowStats.getInstruction() != null) {
173                 final VersionConvertorData simpleConvertorData = new VersionConvertorData(data.getVersion());
174                 final Optional<Instructions> instructions = getConvertorExecutor().convert(
175                         flowStats.getInstruction(), simpleConvertorData);
176
177                 salFlowStatsBuilder.setInstructions(instructions.orElse(new InstructionsBuilder()
178                         .setInstruction(Collections.emptyList()).build()));
179             }
180
181             result.add(salFlowStatsBuilder.build());
182         }
183
184         return result;
185     }
186 }