0f696787f7b0ce8f077a3a54a1ff2891975a8783
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / multilayer / MultiLayerAggregateFlowMultipartService.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.services.multilayer;
10
11 import static java.util.Objects.requireNonNull;
12
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;
47
48 public class MultiLayerAggregateFlowMultipartService extends AbstractAggregateFlowMultipartService<MultipartReply> {
49
50     private final TranslatorLibrary translatorLibrary;
51     private final ConvertorExecutor convertorExecutor;
52     private final VersionConvertorData data;
53
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());
62     }
63
64
65     @Override
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));
75
76         final Uint8 version = getVersion();
77         if (OFConstants.OFP_VERSION_1_3.equals(version)) {
78
79             if (input.getCookie() == null) {
80                 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
81             } else {
82                 mprAggregateRequestBuilder
83                         .setCookie(MoreObjects.firstNonNull(input.getCookie().getValue(), OFConstants.DEFAULT_COOKIE));
84             }
85
86             if (input.getCookieMask() == null) {
87                 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
88             } else {
89                 mprAggregateRequestBuilder.setCookieMask(
90                         MoreObjects.firstNonNull(input.getCookieMask().getValue(), OFConstants.DEFAULT_COOKIE_MASK));
91             }
92             Uint32 outGroup = MoreObjects.firstNonNull(input.getOutGroup(), OFConstants.OFPG_ANY);
93             mprAggregateRequestBuilder.setOutGroup(outGroup);
94         } else {
95             mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
96             mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
97             mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
98         }
99
100         // convert and inject match
101         final Optional<Object> conversionMatch = convertorExecutor.convert(input.getMatch(), data);
102         MatchInjector.inject(conversionMatch, mprAggregateRequestBuilder, data.getVersion());
103
104         FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
105
106         // Set request body to main multipart request
107         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder.build());
108
109         final MultipartRequestInputBuilder mprInput = RequestInputUtils
110                 .createMultipartHeader(MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
111
112         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
113
114         return mprInput.build();
115     }
116
117     @Override
118     public ListenableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> handleAndReply(
119             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
120         return Futures.transform(handleServiceCall(input),
121             result -> {
122                 if (requireNonNull(result).isSuccessful()) {
123                     final MessageTranslator<MultipartReply, AggregatedFlowStatistics>
124                              messageTranslator = translatorLibrary.lookupTranslator(
125                                  new TranslatorKey(getVersion(),
126                                                MultipartReplyAggregateCase.class.getName()));
127
128                     return RpcResultBuilder.success(
129                              new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder()
130                                      .setAggregatedFlowStatistics(result.getResult().stream()
131                                                                           .map(multipartReply ->
132                                                                                        messageTranslator
133                                                                                   .translate(
134                                                                                           multipartReply,
135                                                                                           getDeviceInfo(),
136                                                                                           null))
137                                                                           .collect(Collectors.toList()))
138                                      .build())
139                              .build();
140                 }
141
142                 return RpcResultBuilder
143                              .<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>failed()
144                              .withRpcErrors(result.getErrors()).build();
145             }, MoreExecutors.directExecutor());
146     }
147 }