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.services.multilayer;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
26 import org.opendaylight.openflowplugin.impl.services.AbstractAggregateFlowMultipartService;
27 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
28 import org.opendaylight.openflowplugin.impl.util.FlowCreatorUtil;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchInjector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.opendaylight.yangtools.yang.common.Uint32;
46 import org.opendaylight.yangtools.yang.common.Uint8;
48 public class MultiLayerAggregateFlowMultipartService extends AbstractAggregateFlowMultipartService<MultipartReply> {
50 private final TranslatorLibrary translatorLibrary;
51 private final ConvertorExecutor convertorExecutor;
52 private final VersionConvertorData data;
54 public MultiLayerAggregateFlowMultipartService(final RequestContextStack requestContextStack,
55 final DeviceContext deviceContext,
56 final ConvertorExecutor convertorExecutor,
57 final TranslatorLibrary translatorLibrary) {
58 super(requestContextStack, deviceContext);
59 this.convertorExecutor = convertorExecutor;
60 this.translatorLibrary = translatorLibrary;
61 data = new VersionConvertorData(getVersion());
66 protected OfHeader buildRequest(final Xid xid,
67 final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
68 final MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder
69 = new MultipartRequestAggregateCaseBuilder();
70 final MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
71 final Uint8 tableId = MoreObjects.firstNonNull(input.getTableId(), OFConstants.OFPTT_ALL);
72 mprAggregateRequestBuilder.setTableId(tableId);
73 long outputPortValue = MoreObjects.firstNonNull(input.getOutPort(), OFConstants.OFPP_ANY).longValue();
74 mprAggregateRequestBuilder.setOutPort(Uint32.valueOf(outputPortValue));
76 final Uint8 version = getVersion();
77 if (OFConstants.OFP_VERSION_1_3.equals(version)) {
79 if (input.getCookie() == null) {
80 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
82 mprAggregateRequestBuilder
83 .setCookie(MoreObjects.firstNonNull(input.getCookie().getValue(), OFConstants.DEFAULT_COOKIE));
86 if (input.getCookieMask() == null) {
87 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
89 mprAggregateRequestBuilder.setCookieMask(
90 MoreObjects.firstNonNull(input.getCookieMask().getValue(), OFConstants.DEFAULT_COOKIE_MASK));
92 Uint32 outGroup = MoreObjects.firstNonNull(input.getOutGroup(), OFConstants.OFPG_ANY);
93 mprAggregateRequestBuilder.setOutGroup(outGroup);
95 mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
96 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
97 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
100 // convert and inject match
101 final Optional<Object> conversionMatch = convertorExecutor.convert(input.getMatch(), data);
102 MatchInjector.inject(conversionMatch, mprAggregateRequestBuilder, data.getVersion());
104 FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
106 // Set request body to main multipart request
107 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder.build());
109 final MultipartRequestInputBuilder mprInput = RequestInputUtils
110 .createMultipartHeader(MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
112 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
114 return mprInput.build();
118 public ListenableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> handleAndReply(
119 final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
120 return Futures.transform(handleServiceCall(input),
122 if (requireNonNull(result).isSuccessful()) {
123 final MessageTranslator<MultipartReply, AggregatedFlowStatistics>
124 messageTranslator = translatorLibrary.lookupTranslator(
125 new TranslatorKey(getVersion(),
126 MultipartReplyAggregateCase.class.getName()));
128 return RpcResultBuilder.success(
129 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder()
130 .setAggregatedFlowStatistics(result.getResult().stream()
131 .map(multipartReply ->
137 .collect(Collectors.toList()))
142 return RpcResultBuilder
143 .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
144 .withRpcErrors(result.getErrors()).build();
145 }, MoreExecutors.directExecutor());