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;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesConvertor;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
25 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;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
30 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import java.math.BigInteger;
35 import java.util.List;
36 import java.util.concurrent.Future;
41 public class SalTableServiceImpl extends CommonService implements SalTableService {
43 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
46 public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
47 class FunctionImpl implements Function<BigInteger,Future<RpcResult<UpdateTableOutput>>> {
50 public Future<RpcResult<UpdateTableOutput>> apply(final BigInteger IDConnection) {
52 final SettableFuture<RpcResult<UpdateTableOutput>> result = SettableFuture.create();
54 final long xid = deviceContext.getNextXid().getValue();
56 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
57 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
58 final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
60 requestBuilder.setTableFeatures(ofTableFeatureList);
61 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
63 // Set request body to main multipart request
64 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
66 mprInput.setMultipartRequestBody(caseBuilder.build());
68 final Future<RpcResult<Void>> resultFromOFLib = provideConnectionAdapter(PRIMARY_CONNECTION)
69 .multipartRequest(mprInput.build());
70 final ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters
71 .listenInPoolThread(resultFromOFLib);
73 Futures.addCallback(resultLib, new ResultCallback<UpdateTableOutput>(result) {
75 public UpdateTableOutput createResult() {
76 final UpdateTableOutputBuilder queueStatsFromPortBuilder = new UpdateTableOutputBuilder()
77 .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
78 return queueStatsFromPortBuilder.build();
86 return this.<UpdateTableOutput, UpdateTableOutput>handleServiceCall( PRIMARY_CONNECTION,
90 private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
91 final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
92 mprInput.setType(multipart);
93 mprInput.setVersion(version);
95 mprInput.setFlags(new MultipartRequestFlags(false));
99 private abstract static class ResultCallback<T> implements FutureCallback<RpcResult<Void>> {
101 private final SettableFuture<RpcResult<T>> result;
106 public ResultCallback(final SettableFuture<RpcResult<T>> result) {
107 this.result = result;
110 public abstract T createResult();
113 public void onSuccess(final RpcResult<Void> resultArg) {
114 result.set(RpcResultBuilder.success(createResult()).build());
118 public void onFailure(final Throwable t) {
119 result.set(RpcResultBuilder
121 .withWarning(ErrorType.RPC, OFConstants.ERROR_TAG_TIMEOUT, "something wrong happened",
122 OFConstants.APPLICATION_TAG, "", t).build());