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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.openflowplugin.api.OFConstants;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
15 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
16 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowStatsResponseConvertor;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import java.util.ArrayList;
45 import java.util.List;
48 * The Flow direct statistics service.
50 public class FlowDirectStatisticsService extends AbstractDirectStatisticsService<GetFlowStatisticsInput, GetFlowStatisticsOutput> {
51 private final FlowStatsResponseConvertor flowStatsConvertor = new FlowStatsResponseConvertor();
54 * Instantiates a new Flow direct statistics service.
56 * @param requestContextStack the request context stack
57 * @param deviceContext the device context
59 public FlowDirectStatisticsService(RequestContextStack requestContextStack, DeviceContext deviceContext) {
60 super(MultipartType.OFPMPFLOW, requestContextStack, deviceContext);
64 protected MultipartRequestBody buildRequestBody(GetFlowStatisticsInput input) {
65 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
67 if (input.getTableId() != null) {
68 mprFlowRequestBuilder.setTableId(input.getTableId());
70 mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
73 if (input.getOutPort() != null) {
74 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
76 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
79 if (input.getOutGroup() != null) {
80 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
82 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
85 if (input.getCookie() != null) {
86 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
88 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
91 if (input.getCookieMask() != null) {
92 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
94 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
97 MatchReactor.getInstance().convert(input.getMatch(), getVersion(), mprFlowRequestBuilder, getDatapathId());
99 return new MultipartRequestFlowCaseBuilder()
100 .setMultipartRequestFlow(mprFlowRequestBuilder.build())
105 protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
106 final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
109 for (final MultipartReply mpReply : input) {
110 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
111 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
113 final List<FlowAndStatisticsMapList> statsListPart = flowStatsConvertor.toSALFlowStatsList(replyBody.getFlowStats(), getDatapathId(), getOfVersion());
115 for (final FlowAndStatisticsMapList part : statsListPart) {
116 final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId flowId =
117 new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId(generateFlowId(part).getValue());
119 statsList.add(new FlowAndStatisticsMapListBuilder(part)
120 .setKey(new FlowAndStatisticsMapListKey(flowId))
127 return new GetFlowStatisticsOutputBuilder()
128 .setFlowAndStatisticsMapList(statsList)
133 protected void storeStatistics(GetFlowStatisticsOutput output) throws Exception {
134 final InstanceIdentifier<FlowCapableNode> nodePath = getDeviceContext()
135 .getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
137 for (final FlowAndStatisticsMapList flowStatistics : output.getFlowAndStatisticsMapList()) {
138 final FlowId flowId = generateFlowId(flowStatistics);
139 final FlowKey flowKey = new FlowKey(flowId);
141 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
142 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
144 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
145 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build())
148 final InstanceIdentifier<Flow> flowStatisticsPath = nodePath
149 .child(Table.class, new TableKey(flowStatistics.getTableId()))
150 .child(Flow.class, flowKey);
152 getDeviceContext().writeToTransactionWithParentsSlow(LogicalDatastoreType.OPERATIONAL, flowStatisticsPath, flowBuilder.build());
156 private FlowId generateFlowId(FlowAndStatisticsMapList flowStatistics) {
157 final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder()
158 .setFlowStatistics(new FlowStatisticsBuilder(flowStatistics).build());
160 final FlowBuilder flowBuilder = new FlowBuilder(flowStatistics)
161 .addAugmentation(FlowStatisticsData.class, flowStatisticsDataBld.build());
163 final short tableId = flowStatistics.getTableId();
164 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
165 return getDeviceContext().getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey, tableId);