2 * Copyright (c) 2015 Cisco Systems, Inc. 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
8 package org.opendaylight.openflowplugin.impl.statistics.services;
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;
49 public class OpendaylightFlowStatisticsServiceImpl extends CommonService implements OpendaylightFlowStatisticsService {
51 private static final Logger LOG = LoggerFactory.getLogger(OpendaylightFlowStatisticsServiceImpl.class);
53 public OpendaylightFlowStatisticsServiceImpl(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
54 super(requestContextStack, deviceContext);
58 public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
59 final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
62 return this.<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput, Void>handleServiceCall(
64 new Function<DataCrate<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>, ListenableFuture<RpcResult<Void>>>() {
67 public ListenableFuture<RpcResult<Void>> apply(final DataCrate<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> data) {
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);
78 FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
80 // Set request body to main multipart request
81 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
83 final Xid xid = deviceContext.getNextXid();
84 data.getRequestContext().setXid(xid);
85 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
86 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
88 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
90 final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
91 .getConnectionAdapter().multipartRequest(mprInput.build());
93 return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
100 public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
101 final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
104 return this.<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput, Void>handleServiceCall(
106 new Function<DataCrate<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>, ListenableFuture<RpcResult<Void>>>() {
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());
120 mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
121 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
122 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
125 MatchReactor.getInstance().convert(input.getMatch(), version, mprAggregateRequestBuilder,
126 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
128 FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
130 // Set request body to main multipart request
131 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
134 final Xid xid = deviceContext.getNextXid();
135 data.getRequestContext().setXid(xid);
136 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
137 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
139 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
140 final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
141 .getConnectionAdapter().multipartRequest(mprInput.build());
142 return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
149 public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
150 final GetAllFlowStatisticsFromFlowTableInput input) {
152 return this.<GetAllFlowStatisticsFromFlowTableOutput, Void>handleServiceCall(PRIMARY_CONNECTION,
153 new Function<DataCrate<GetAllFlowStatisticsFromFlowTableOutput>, ListenableFuture<RpcResult<Void>>>() {
156 public ListenableFuture<RpcResult<Void>> apply(final DataCrate<GetAllFlowStatisticsFromFlowTableOutput> data) {
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);
166 final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
167 multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
169 final Xid xid = deviceContext.getNextXid();
170 data.getRequestContext().setXid(xid);
171 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
172 MultipartType.OFPMPFLOW, xid.getValue(), version);
174 mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
175 final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
176 .getConnectionAdapter().multipartRequest(mprInput.build());
177 return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
183 public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
184 final GetAllFlowsStatisticsFromAllFlowTablesInput input) {
187 return this.<GetAllFlowsStatisticsFromAllFlowTablesOutput, Void>handleServiceCall(PRIMARY_CONNECTION,
188 new Function<DataCrate<GetAllFlowsStatisticsFromAllFlowTablesOutput>, ListenableFuture<RpcResult<Void>>>() {
191 public ListenableFuture<RpcResult<Void>> apply(final DataCrate<GetAllFlowsStatisticsFromAllFlowTablesOutput> data) {
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);
202 final Xid xid = deviceContext.getNextXid();
203 data.getRequestContext().setXid(xid);
204 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
205 MultipartType.OFPMPFLOW, xid.getValue(), version);
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);
217 public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
218 final GetFlowStatisticsFromFlowTableInput input) {
221 return this.<GetFlowStatisticsFromFlowTableOutput, Void>handleServiceCall(PRIMARY_CONNECTION,
222 new Function<DataCrate<GetFlowStatisticsFromFlowTableOutput>, ListenableFuture<RpcResult<Void>>>() {
225 public ListenableFuture<RpcResult<Void>> apply(final DataCrate<GetFlowStatisticsFromFlowTableOutput> data) {
227 final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
228 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
229 mprFlowRequestBuilder.setTableId(input.getTableId());
231 if (input.getOutPort() != null) {
232 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
234 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
237 if (input.getOutGroup() != null) {
238 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
240 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
243 if (input.getCookie() != null) {
244 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
246 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
249 if (input.getCookieMask() != null) {
250 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
252 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
255 // convert and inject match
256 MatchReactor.getInstance().convert(input.getMatch(), version, mprFlowRequestBuilder,
257 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
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);
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);