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.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.math.BigInteger;
16 import java.util.List;
17 import java.util.concurrent.Future;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesConvertor;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeatures;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34 import org.slf4j.Logger;
40 public class SalTableServiceImpl extends CommonService implements SalTableService {
42 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
45 public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
46 class FunctionImpl implements Function<UpdateTableOutput> {
49 public Future<RpcResult<UpdateTableOutput>> apply(final BigInteger IDConnection) {
51 final SettableFuture<RpcResult<UpdateTableOutput>> result = SettableFuture.create();
53 final long xid = deviceContext.getNextXid().getValue();
55 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
56 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
57 final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
59 requestBuilder.setTableFeatures(ofTableFeatureList);
60 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
62 // Set request body to main multipart request
63 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
65 mprInput.setMultipartRequestBody(caseBuilder.build());
67 final Future<RpcResult<Void>> resultFromOFLib = provideConnectionAdapter(PRIMARY_CONNECTION)
68 .multipartRequest(mprInput.build());
69 final ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters
70 .listenInPoolThread(resultFromOFLib);
72 Futures.addCallback(resultLib, new ResultCallback<UpdateTableOutput>(result) {
74 public UpdateTableOutput createResult() {
75 final UpdateTableOutputBuilder queueStatsFromPortBuilder = new UpdateTableOutputBuilder()
76 .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
77 return queueStatsFromPortBuilder.build();
85 return ServiceCallProcessingUtil.<UpdateTableOutput> handleServiceCall(rpcContext, PRIMARY_CONNECTION,
86 provideWaitTime(), new FunctionImpl());
89 private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
90 final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
91 mprInput.setType(multipart);
92 mprInput.setVersion(version);
94 mprInput.setFlags(new MultipartRequestFlags(false));
98 private abstract static class ResultCallback<T> implements FutureCallback<RpcResult<Void>> {
100 private final SettableFuture<RpcResult<T>> result;
105 public ResultCallback(final SettableFuture<RpcResult<T>> result) {
106 this.result = result;
109 public abstract T createResult();
112 public void onSuccess(final RpcResult<Void> resultArg) {
113 result.set(RpcResultBuilder.success(createResult()).build());
117 public void onFailure(final Throwable t) {
118 result.set(RpcResultBuilder
120 .withWarning(ErrorType.RPC, OFConstants.ERROR_TAG_TIMEOUT, "something wrong happened",
121 OFConstants.APPLICATION_TAG, "", t).build());