Wrap service handlers to method handleServiceCall.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / OpendaylightFlowStatisticsServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 package org.opendaylight.openflowplugin.impl.services;
9
10 import com.google.common.base.Function;
11 import java.math.BigInteger;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.Future;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
20 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * @author joe
44  */
45 public class OpendaylightFlowStatisticsServiceImpl extends CommonService implements OpendaylightFlowStatisticsService {
46
47     private static final Logger LOG = LoggerFactory.getLogger(OpendaylightFlowStatisticsServiceImpl.class);
48
49     @Override
50     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
51             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
52
53         return this.<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput, Void> handleServiceCall(
54                 PRIMARY_CONNECTION, new Function<BigInteger, Future<RpcResult<Void>>>() {
55
56                     @Override
57                     public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
58                         final Xid xid = deviceContext.getNextXid();
59
60                         // Create multipart request body for fetch all the group stats
61                         final MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
62                         final MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
63                         mprAggregateRequestBuilder.setTableId(input.getTableId().getValue());
64                         mprAggregateRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
65                         mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
66                         mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
67                         mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
68
69                         FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
70
71                         // Set request body to main multipart request
72                         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
73                                 .build());
74                         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
75                                 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
76
77                         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
78
79                         final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
80                                 .getConnectionAdapter().multipartRequest(mprInput.build());
81
82                         return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
83                     }
84                 });
85
86     }
87
88     @Override
89     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
90             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
91         return this.<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput, Void> handleServiceCall(
92                 PRIMARY_CONNECTION, new Function<BigInteger, Future<RpcResult<Void>>>() {
93
94                     @Override
95                     public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
96                         final Xid xid = deviceContext.getNextXid();
97                         final MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
98                         final MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
99                         mprAggregateRequestBuilder.setTableId(input.getTableId());
100                         mprAggregateRequestBuilder.setOutPort(input.getOutPort().longValue());
101                         // TODO: repeating code
102                         if (version == OFConstants.OFP_VERSION_1_3) {
103                             mprAggregateRequestBuilder.setCookie(input.getCookie().getValue());
104                             mprAggregateRequestBuilder.setCookieMask(input.getCookieMask().getValue());
105                             mprAggregateRequestBuilder.setOutGroup(input.getOutGroup());
106                         } else {
107                             mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
108                             mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
109                             mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
110                         }
111
112                         MatchReactor.getInstance().convert(input.getMatch(), version, mprAggregateRequestBuilder,
113                                 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
114
115                         FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
116
117                         // Set request body to main multipart request
118                         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
119                                 .build());
120
121                         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
122                                 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
123
124                         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
125                         final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
126                                 .getConnectionAdapter().multipartRequest(mprInput.build());
127                         return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
128                     }
129                 });
130
131     }
132
133     @Override
134     public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
135             final GetAllFlowStatisticsFromFlowTableInput input) {
136         return this.<GetAllFlowStatisticsFromFlowTableOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
137                 new Function<BigInteger, Future<RpcResult<Void>>>() {
138
139                     @Override
140                     public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
141                         final Xid xid = deviceContext.getNextXid();
142
143                         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
144                         mprFlowRequestBuilder.setTableId(input.getTableId().getValue());
145                         mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
146                         mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
147                         mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
148                         mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
149                         FlowCreatorUtil.setWildcardedFlowMatch(version, mprFlowRequestBuilder);
150
151                         final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
152                         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
153
154                         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
155                                 MultipartType.OFPMPFLOW, xid.getValue(), version);
156
157                         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
158                         final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
159                                 .getConnectionAdapter().multipartRequest(mprInput.build());
160                         return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
161                     }
162                 });
163     }
164
165     @Override
166     public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
167             final GetAllFlowsStatisticsFromAllFlowTablesInput input) {
168         return this.<GetAllFlowsStatisticsFromAllFlowTablesOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
169                 new Function<BigInteger, Future<RpcResult<Void>>>() {
170
171                     @Override
172                     public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
173                         final Xid xid = deviceContext.getNextXid();
174
175                         final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
176                         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
177                         mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
178                         mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
179                         mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
180                         mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
181                         mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
182                         FlowCreatorUtil.setWildcardedFlowMatch(version, mprFlowRequestBuilder);
183
184                         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
185                                 MultipartType.OFPMPFLOW, xid.getValue(), version);
186
187                         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
188                         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
189                         final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
190                                 .getConnectionAdapter().multipartRequest(mprInput.build());
191                         return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
192                     }
193                 });
194     }
195
196     @Override
197     public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
198             final GetFlowStatisticsFromFlowTableInput input) {
199         return this.<GetFlowStatisticsFromFlowTableOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
200                 new Function<BigInteger, Future<RpcResult<Void>>>() {
201
202                     @Override
203                     public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
204                         final Xid xid = deviceContext.getNextXid();
205
206                         final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
207                         final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
208                         mprFlowRequestBuilder.setTableId(input.getTableId());
209
210                         if (input.getOutPort() != null) {
211                             mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
212                         } else {
213                             mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
214                         }
215
216                         if (input.getOutGroup() != null) {
217                             mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
218                         } else {
219                             mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
220                         }
221
222                         if (input.getCookie() != null) {
223                             mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
224                         } else {
225                             mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
226                         }
227
228                         if (input.getCookieMask() != null) {
229                             mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
230                         } else {
231                             mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
232                         }
233
234                         // convert and inject match
235                         MatchReactor.getInstance().convert(input.getMatch(), version, mprFlowRequestBuilder,
236                                 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
237
238                         // Set request body to main multipart request
239                         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
240                         final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
241                                 MultipartType.OFPMPFLOW, xid.getValue(), version);
242                         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
243                         final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
244                                 .getConnectionAdapter().multipartRequest(mprInput.build());
245                         return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
246                     }
247                 });
248     }
249
250     private <T extends DataObject> void convertRpcResultToRequestFuture(final RequestContext<T> requestContext,
251             final ListenableFuture<RpcResult<Void>> futureResultFromOfLib) {
252         final RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext, deviceContext);
253         rpcResultConvertor.processResultFromOfJava(futureResultFromOfLib);
254     }
255
256 }