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.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;
51 * The Flow direct statistics service.
53 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
54 private final FlowStatsResponseConvertorData data;
57 * Instantiates a new Flow direct statistics service.
58 * @param requestContextStack the request context stack
59 * @param deviceContext the device context
60 * @param convertorExecutor
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);
70 protected MultipartRequestBody buildRequestBody(GetFlowStatisticsInput input) {
71 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
73 if (input.getTableId() != null) {
74 mprFlowRequestBuilder.setTableId(input.getTableId());
76 mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
79 if (input.getOutPort() != null) {
80 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
82 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
85 if (input.getOutGroup() != null) {
86 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
88 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
91 if (input.getCookie() != null) {
92 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
94 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
97 if (input.getCookieMask() != null) {
98 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
100 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
103 MatchReactor.getInstance().convert(input.getMatch(), getVersion(), mprFlowRequestBuilder, getConvertorExecutor());
105 return new MultipartRequestFlowCaseBuilder()
106 .setMultipartRequestFlow(mprFlowRequestBuilder.build())
111 protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
112 final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
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);
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());
126 statsList.add(new FlowAndStatisticsMapListBuilder(part)
127 .setKey(new FlowAndStatisticsMapListKey(flowId))
135 return new GetFlowStatisticsOutputBuilder()
136 .setFlowAndStatisticsMapList(statsList)
141 protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
142 final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
144 for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
145 final FlowId flowId = generateFlowId(flowStatistics);
146 final FlowKey flowKey = new FlowKey(flowId);
148 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
149 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
151 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
152 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
155 final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
156 .child(Table.class, new TableKey(flowStatistics.getTableId()))
157 .child(Flow.class, flowKey);
159 getTxFacade().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
163 private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
164 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
165 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
167 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
168 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
170 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
171 return getDeviceRegistry().getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);