OFJResult2RequestCtxFuture uses listenable future
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalTableServiceImpl.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.services;
9
10 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
11
12 import com.google.common.base.Function;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.JdkFutureAdapters;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
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;
35 import java.math.BigInteger;
36 import java.util.List;
37 import java.util.concurrent.Future;
38
39 /**
40  * @author joe
41  */
42 public class SalTableServiceImpl extends CommonService implements SalTableService {
43
44     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
45
46     @Override
47     public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
48         class FunctionImpl implements Function<DataCrate<UpdateTableOutput>,ListenableFuture<RpcResult<UpdateTableOutput>>> {
49
50             @Override
51             public ListenableFuture<RpcResult<UpdateTableOutput>> apply(final DataCrate<UpdateTableOutput> data) {
52
53                 final SettableFuture<RpcResult<UpdateTableOutput>> result = SettableFuture.create();
54
55
56                 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
57                 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
58                 final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
59                         .getUpdatedTable());
60                 requestBuilder.setTableFeatures(ofTableFeatureList);
61                 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
62
63                 // Set request body to main multipart request
64                 final Xid xid = deviceContext.getNextXid();
65                 data.getRequestContext().setXid(xid);
66                 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
67                         xid.getValue());
68                 mprInput.setMultipartRequestBody(caseBuilder.build());
69
70                 final Future<RpcResult<Void>> resultFromOFLib = provideConnectionAdapter(PRIMARY_CONNECTION)
71                         .multipartRequest(mprInput.build());
72                 final ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters
73                         .listenInPoolThread(resultFromOFLib);
74
75                 Futures.addCallback(resultLib, new ResultCallback<UpdateTableOutput>(result) {
76                     @Override
77                     public UpdateTableOutput createResult() {
78                         final UpdateTableOutputBuilder queueStatsFromPortBuilder = new UpdateTableOutputBuilder()
79                                 .setTransactionId(new TransactionId(BigInteger.valueOf(xid.getValue())));
80                         return queueStatsFromPortBuilder.build();
81                     }
82                 });
83
84                 return result;
85             }
86         }
87
88         return this.<UpdateTableOutput, UpdateTableOutput>handleServiceCall( PRIMARY_CONNECTION,
89                  new FunctionImpl());
90     }
91
92     private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
93         final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
94         mprInput.setType(multipart);
95         mprInput.setVersion(version);
96         mprInput.setXid(xid);
97         mprInput.setFlags(new MultipartRequestFlags(false));
98         return mprInput;
99     }
100
101     private abstract static class ResultCallback<T> implements FutureCallback<RpcResult<Void>> {
102
103         private final SettableFuture<RpcResult<T>> result;
104
105         /**
106          * @param result
107          */
108         public ResultCallback(final SettableFuture<RpcResult<T>> result) {
109             this.result = result;
110         }
111
112         public abstract T createResult();
113
114         @Override
115         public void onSuccess(final RpcResult<Void> resultArg) {
116             result.set(RpcResultBuilder.success(createResult()).build());
117         }
118
119         @Override
120         public void onFailure(final Throwable t) {
121             result.set(RpcResultBuilder
122                     .<T>failed()
123                     .withWarning(ErrorType.RPC, OFConstants.ERROR_TAG_TIMEOUT, "something wrong happened",
124                             OFConstants.APPLICATION_TAG, "", t).build());
125         }
126     }
127
128 }