2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.multilayer;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
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.device.Xid;
18 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
19 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
20 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
21 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractFlowDirectStatisticsService;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.FlowStatsResponseConvertorData;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchInjector;
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.statistics.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
39 public class FlowDirectStatisticsService extends AbstractFlowDirectStatisticsService<MultipartReply> {
41 private final FlowStatsResponseConvertorData data;
43 public FlowDirectStatisticsService(final RequestContextStack requestContextStack,
44 final DeviceContext deviceContext,
45 final ConvertorExecutor convertorExecutor,
46 final MultipartWriterProvider statisticsWriterProvider) {
47 super(requestContextStack, deviceContext, convertorExecutor, statisticsWriterProvider);
48 data = new FlowStatsResponseConvertorData(getVersion());
49 data.setDatapathId(getDatapathId());
50 data.setMatchPath(MatchPath.RPCFLOWSSTATISTICS_FLOWANDSTATISTICSMAPLIST_MATCH);
54 protected GetFlowStatisticsOutput buildReply(List<MultipartReply> input, boolean success) {
55 final List<FlowAndStatisticsMapList> statsList = new ArrayList<>();
58 for (final MultipartReply mpReply : input) {
59 final MultipartReplyFlowCase caseBody = (MultipartReplyFlowCase) mpReply.getMultipartReplyBody();
60 final MultipartReplyFlow replyBody = caseBody.getMultipartReplyFlow();
61 final Optional<List<FlowAndStatisticsMapList>> statsListPart = getConvertorExecutor().convert(
62 replyBody.getFlowStats(), data);
64 if (statsListPart.isPresent()) {
65 for (final FlowAndStatisticsMapList part : statsListPart.get()) {
66 final FlowId flowId = new FlowId(generateFlowId(part).getValue());
67 statsList.add(new FlowAndStatisticsMapListBuilder(part)
68 .setKey(new FlowAndStatisticsMapListKey(flowId))
76 return new GetFlowStatisticsOutputBuilder()
77 .setFlowAndStatisticsMapList(statsList)
82 protected OfHeader buildRequest(final Xid xid, final GetFlowStatisticsInput input) {
83 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
85 if (input.getTableId() != null) {
86 mprFlowRequestBuilder.setTableId(input.getTableId());
88 mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
91 if (input.getOutPort() != null) {
92 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
94 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
97 if (input.getOutGroup() != null) {
98 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
100 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
103 if (input.getCookie() != null) {
104 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
106 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
109 if (input.getCookieMask() != null) {
110 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
112 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
115 // convert and inject match
116 final Optional<Object> conversionMatch = getConvertorExecutor().convert(input.getMatch(), data);
117 MatchInjector.inject(conversionMatch, mprFlowRequestBuilder, data.getVersion());
119 return RequestInputUtils.createMultipartHeader(getMultipartType(), xid.getValue(), getVersion())
120 .setMultipartRequestBody(new MultipartRequestFlowCaseBuilder()
121 .setMultipartRequestFlow(mprFlowRequestBuilder.build())