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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
18 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowStatsResponseConvertor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 * The Flow direct statistics service.
49 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
50 private final FlowStatsResponseConvertor flowStatsConvertor = new FlowStatsResponseConvertor();
53 * Instantiates a new Flow direct statistics service.
55 * @param requestContextStack the request context stack
56 * @param deviceContext the device context
58 public FlowDirectStatisticsService(RequestContextStack requestContextStack, DeviceContext deviceContext) {
59 super(MultipartType.OFPMPFLOW, requestContextStack, deviceContext);
63 protected MultipartRequestBody buildRequestBody(GetFlowStatisticsInput input) {
64 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
66 if (input.getTableId() != null) {
67 mprFlowRequestBuilder.setTableId(input.getTableId());
69 mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
72 if (input.getOutPort() != null) {
73 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
75 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
78 if (input.getOutGroup() != null) {
79 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
81 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
84 if (input.getCookie() != null) {
85 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
87 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
90 if (input.getCookieMask() != null) {
91 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
93 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
96 MatchReactor.getInstance().convert(input.getMatch(), getVersion(), mprFlowRequestBuilder, getDatapathId());
98 return new MultipartRequestFlowCaseBuilder()
99 .setMultipartRequestFlow(mprFlowRequestBuilder.build())
104 protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
105 final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
108 for (final MultipartReply mpReply : input) {
109 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
110 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
112 final List<FlowAndStatisticsMapList> statsListPart = flowStatsConvertor.toSALFlowStatsList(replyBody.getFlowStats(), getDatapathId(), getOfVersion());
114 for (final FlowAndStatisticsMapList part : statsListPart) {
115 final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId flowId =
116 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId(generateFlowId(part).getValue());
118 statsList.add(new FlowAndStatisticsMapListBuilder(part)
119 .setKey(new FlowAndStatisticsMapListKey(flowId))
126 return new GetFlowStatisticsOutputBuilder()
127 .setFlowAndStatisticsMapList(statsList)
132 protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
133 final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
135 for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
136 final FlowId flowId = generateFlowId(flowStatistics);
137 final FlowKey flowKey = new FlowKey(flowId);
139 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
140 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
142 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
143 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
146 final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
147 .child(Table.class, new TableKey(flowStatistics.getTableId()))
148 .child(Flow.class, flowKey);
150 getTxFacade().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
154 private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
155 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
156 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
158 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
159 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
161 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
162 return getDeviceRegistry().getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);