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