2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
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.match.MatchReactor;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 * The Flow direct statistics service.
52 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
53 private final FlowStatsResponseConvertorData data;
56 * Instantiates a new Flow direct statistics service.
57 * @param requestContextStack the request context stack
58 * @param deviceContext the device context
59 * @param convertorExecutor
61 public FlowDirectStatisticsService(RequestContextStack requestContextStack, DeviceContext deviceContext, ConvertorExecutor convertorExecutor) {
62 super(MultipartType.OFPMPFLOW, requestContextStack, deviceContext, convertorExecutor);
63 data = new FlowStatsResponseConvertorData(getVersion());
64 data.setDatapathId(getDatapathId());
65 data.setMatchPath(MatchPath.RPCFLOWSSTATISTICS_FLOWANDSTATISTICSMAPLIST_MATCH);
69 protected MultipartRequestBody buildRequestBody(GetFlowStatisticsInput input) {
70 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
72 if (input.getTableId() != null) {
73 mprFlowRequestBuilder.setTableId(input.getTableId());
75 mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
78 if (input.getOutPort() != null) {
79 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
81 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
84 if (input.getOutGroup() != null) {
85 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
87 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
90 if (input.getCookie() != null) {
91 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
93 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
96 if (input.getCookieMask() != null) {
97 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
99 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
102 MatchReactor.getInstance().convert(input.getMatch(), getVersion(), mprFlowRequestBuilder, getConvertorExecutor());
104 return new MultipartRequestFlowCaseBuilder()
105 .setMultipartRequestFlow(mprFlowRequestBuilder.build())
110 protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
111 final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
114 for (final MultipartReply mpReply : input) {
115 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
116 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
117 final Optional<List<FlowAndStatisticsMapList>> statsListPart = getConvertorExecutor().convert(
118 replyBody.getFlowStats(), data);
120 if (statsListPart.isPresent()) {
121 for (final FlowAndStatisticsMapList part : statsListPart.get()) {
122 final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId flowId =
123 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId(generateFlowId(part).getValue());
125 statsList.add(new FlowAndStatisticsMapListBuilder(part)
126 .setKey(new FlowAndStatisticsMapListKey(flowId))
134 return new GetFlowStatisticsOutputBuilder()
135 .setFlowAndStatisticsMapList(statsList)
140 protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
141 final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
143 for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
144 final FlowId flowId = generateFlowId(flowStatistics);
145 final FlowKey flowKey = new FlowKey(flowId);
147 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
148 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
150 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
151 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
154 final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
155 .child(Table.class, new TableKey(flowStatistics.getTableId()))
156 .child(Flow.class, flowKey);
158 getTxFacade().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
162 private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
163 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
164 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
166 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
167 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
169 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(getVersion(), flowBuilder.build());
170 getDeviceRegistry().getDeviceFlowRegistry().store(flowRegistryKey);
171 return getDeviceRegistry().getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey).getFlowId();