Wrap service handlers to method handleServiceCall.
[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 com.google.common.base.Function;
11
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;
37
38 /**
39  * @author joe
40  */
41 public class SalTableServiceImpl extends CommonService implements SalTableService {
42
43     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
44
45     @Override
46     public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
47         class FunctionImpl implements Function<BigInteger,Future<RpcResult<UpdateTableOutput>>> {
48
49             @Override
50             public Future<RpcResult<UpdateTableOutput>> apply(final BigInteger IDConnection) {
51
52                 final SettableFuture<RpcResult<UpdateTableOutput>> result = SettableFuture.create();
53
54                 final long xid = deviceContext.getNextXid().getValue();
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 MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
65                         xid);
66                 mprInput.setMultipartRequestBody(caseBuilder.build());
67
68                 final Future<RpcResult<Void>> resultFromOFLib = provideConnectionAdapter(PRIMARY_CONNECTION)
69                         .multipartRequest(mprInput.build());
70                 final ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters
71                         .listenInPoolThread(resultFromOFLib);
72
73                 Futures.addCallback(resultLib, new ResultCallback<UpdateTableOutput>(result) {
74                     @Override
75                     public UpdateTableOutput createResult() {
76                         final UpdateTableOutputBuilder queueStatsFromPortBuilder = new UpdateTableOutputBuilder()
77                                 .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
78                         return queueStatsFromPortBuilder.build();
79                     }
80                 });
81
82                 return result;
83             }
84         }
85
86         return this.<UpdateTableOutput, UpdateTableOutput>handleServiceCall( PRIMARY_CONNECTION,
87                  new FunctionImpl());
88     }
89
90     private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
91         final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
92         mprInput.setType(multipart);
93         mprInput.setVersion(version);
94         mprInput.setXid(xid);
95         mprInput.setFlags(new MultipartRequestFlags(false));
96         return mprInput;
97     }
98
99     private abstract static class ResultCallback<T> implements FutureCallback<RpcResult<Void>> {
100
101         private final SettableFuture<RpcResult<T>> result;
102
103         /**
104          * @param result
105          */
106         public ResultCallback(final SettableFuture<RpcResult<T>> result) {
107             this.result = result;
108         }
109
110         public abstract T createResult();
111
112         @Override
113         public void onSuccess(final RpcResult<Void> resultArg) {
114             result.set(RpcResultBuilder.success(createResult()).build());
115         }
116
117         @Override
118         public void onFailure(final Throwable t) {
119             result.set(RpcResultBuilder
120                     .<T>failed()
121                     .withWarning(ErrorType.RPC, OFConstants.ERROR_TAG_TIMEOUT, "something wrong happened",
122                             OFConstants.APPLICATION_TAG, "", t).build());
123         }
124     }
125
126 }