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.services;
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;
45 public class OpendaylightFlowStatisticsServiceImpl extends CommonService implements OpendaylightFlowStatisticsService {
47 private static final Logger LOG = LoggerFactory.getLogger(OpendaylightFlowStatisticsServiceImpl.class);
50 public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
51 final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
53 return this.<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput, Void> handleServiceCall(
54 PRIMARY_CONNECTION, new Function<BigInteger, Future<RpcResult<Void>>>() {
57 public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
58 final Xid xid = deviceContext.getNextXid();
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);
69 FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
71 // Set request body to main multipart request
72 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
74 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
75 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
77 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
79 final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
80 .getConnectionAdapter().multipartRequest(mprInput.build());
82 return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
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>>>() {
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());
107 mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
108 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
109 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
112 MatchReactor.getInstance().convert(input.getMatch(), version, mprAggregateRequestBuilder,
113 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
115 FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
117 // Set request body to main multipart request
118 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder
121 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
122 MultipartType.OFPMPAGGREGATE, xid.getValue(), version);
124 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
125 final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
126 .getConnectionAdapter().multipartRequest(mprInput.build());
127 return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
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>>>() {
140 public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
141 final Xid xid = deviceContext.getNextXid();
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);
151 final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
152 multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
154 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
155 MultipartType.OFPMPFLOW, xid.getValue(), version);
157 mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
158 final Future<RpcResult<Void>> resultFromOFLib = deviceContext.getPrimaryConnectionContext()
159 .getConnectionAdapter().multipartRequest(mprInput.build());
160 return JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
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>>>() {
172 public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
173 final Xid xid = deviceContext.getNextXid();
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);
184 final MultipartRequestInputBuilder mprInput = RequestInputUtils.createMultipartHeader(
185 MultipartType.OFPMPFLOW, xid.getValue(), version);
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);
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>>>() {
203 public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
204 final Xid xid = deviceContext.getNextXid();
206 final MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
207 final MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
208 mprFlowRequestBuilder.setTableId(input.getTableId());
210 if (input.getOutPort() != null) {
211 mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
213 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
216 if (input.getOutGroup() != null) {
217 mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
219 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
222 if (input.getCookie() != null) {
223 mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
225 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
228 if (input.getCookieMask() != null) {
229 mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
231 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
234 // convert and inject match
235 MatchReactor.getInstance().convert(input.getMatch(), version, mprFlowRequestBuilder,
236 deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId());
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);
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);