Revert "Fix statistics race condition on big flows"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / FlowDirectStatisticsService.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.impl.statistics.services.direct;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
19 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
20 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.FlowStatsResponseConvertorData;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49
50 /**
51  * The Flow direct statistics service.
52  */
53 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
54     private final FlowStatsResponseConvertorData data;
55
56     /**
57      * Instantiates a new Flow direct statistics service.
58      *  @param requestContextStack the request context stack
59      * @param deviceContext       the device context
60      * @param convertorExecutor
61      */
62     public FlowDirectStatisticsService(RequestContextStack requestContextStack, DeviceContext deviceContext, ConvertorExecutor convertorExecutor) {
63         super(MultipartType.OFPMPFLOW, requestContextStack, deviceContext, convertorExecutor);
64         data = new FlowStatsResponseConvertorData(getVersion());
65         data.setDatapathId(getDatapathId());
66         data.setMatchPath(MatchPath.RPCFLOWSSTATISTICS_FLOWANDSTATISTICSMAPLIST_MATCH);
67     }
68
69     @Override
70     protected MultipartRequestBody buildRequestBody(GetFlowStatisticsInput input) {
71         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
72
73         if (input.getTableId() != null) {
74             mprFlowRequestBuilder.setTableId(input.getTableId());
75         } else {
76             mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
77         }
78
79         if (input.getOutPort() != null) {
80             mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
81         } else {
82             mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
83         }
84
85         if (input.getOutGroup() != null) {
86             mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
87         } else {
88             mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
89         }
90
91         if (input.getCookie() != null) {
92             mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
93         } else {
94             mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
95         }
96
97         if (input.getCookieMask() != null) {
98             mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
99         } else {
100             mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
101         }
102
103         MatchReactor.getInstance().convert(input.getMatch(), getVersion(), mprFlowRequestBuilder, getConvertorExecutor());
104
105         return new MultipartRequestFlowCaseBuilder()
106                 .setMultipartRequestFlow(mprFlowRequestBuilder.build())
107                 .build();
108     }
109
110     @Override
111     protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
112         final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
113
114         if (success) {
115             for (final MultipartReply mpReply : input) {
116                 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
117                 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
118                 final Optional<List<FlowAndStatisticsMapList>> statsListPart = getConvertorExecutor().convert(
119                         replyBody.getFlowStats(), data);
120
121                 if (statsListPart.isPresent()) {
122                     for (final FlowAndStatisticsMapList part : statsListPart.get()) {
123                         final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId flowId =
124                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId(generateFlowId(part).getValue());
125
126                         statsList.add(new FlowAndStatisticsMapListBuilder(part)
127                                 .setKey(new FlowAndStatisticsMapListKey(flowId))
128                                 .setFlowId(flowId)
129                                 .build());
130                     }
131                 }
132             }
133         }
134
135         return new GetFlowStatisticsOutputBuilder()
136                 .setFlowAndStatisticsMapList(statsList)
137                 .build();
138     }
139
140     @Override
141     protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
142         final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
143
144         for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
145             final FlowId flowId = generateFlowId(flowStatistics);
146             final FlowKey flowKey = new FlowKey(flowId);
147
148             final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
149                     .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
150
151             final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
152                     .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
153                     .setKey(flowKey);
154
155             final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
156                     .child(Table.class, new TableKey(flowStatistics.getTableId()))
157                     .child(Flow.class, flowKey);
158
159             getTxFacade().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
160         }
161     }
162
163     private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
164         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
165                 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
166
167         final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
168                 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
169
170         final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(getVersion(), flowBuilder.build());
171         return getDeviceRegistry().getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
172     }
173 }