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