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