Bug 5540 - Remove ConvertorManager singleton
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / AbstractDirectStatisticsService.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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.AsyncFunction;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.List;
17 import java.util.concurrent.Future;
18 import javax.annotation.Nullable;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
23 import org.opendaylight.openflowplugin.impl.services.AbstractMultipartService;
24 import org.opendaylight.openflowplugin.impl.services.RequestInputUtils;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.StoreStatsGrouping;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33
34 /**
35  * The abstract direct statistics service.
36  * This abstract service provides wrappers and tools for all other derived statistics services.
37  *
38  * @param <I> the input type parameter
39  * @param <O> the output type parameter
40  */
41 public abstract class AbstractDirectStatisticsService<I extends StoreStatsGrouping, O> extends AbstractMultipartService<I> {
42
43     private final Function<RpcResult<List<MultipartReply>>, RpcResult<O>> resultTransformFunction =
44             new Function<RpcResult<List<MultipartReply>>, RpcResult<O>>() {
45                 @Nullable
46                 @Override
47                 public RpcResult<O> apply(@Nullable RpcResult<List<MultipartReply>> input) {
48                     Preconditions.checkNotNull(input);
49                     final O reply = buildReply(input.getResult(), input.isSuccessful());
50                     return RpcResultBuilder.success(reply).build();
51                 }
52             };
53
54     private final AsyncFunction<RpcResult<O>, RpcResult<O>> resultStoreFunction =
55             new AsyncFunction<RpcResult<O>, RpcResult<O>>() {
56                 @Nullable
57                 @Override
58                 public ListenableFuture<RpcResult<O>> apply(@Nullable RpcResult<O> input) throws Exception {
59                     Preconditions.checkNotNull(input);
60
61                     if (input.isSuccessful()) {
62                         storeStatistics(input.getResult());
63                         getTxFacade().submitTransaction(); // TODO: If submitTransaction will ever return future, chain it
64                     }
65
66                     return Futures.immediateFuture(input);
67                 }
68             };
69
70     private final MultipartType multipartType;
71     private final ConvertorExecutor convertorExecutor;
72     private final OpenflowVersion ofVersion = OpenflowVersion.get(getVersion());
73     /**
74      * Instantiates a new Abstract direct statistics service.
75      *
76      * @param multipartType       the multipart type
77      * @param requestContextStack the request context stack
78      * @param deviceContext       the device context
79      * @param convertorExecutor
80      */
81     protected AbstractDirectStatisticsService(MultipartType multipartType, RequestContextStack requestContextStack,
82                                               DeviceContext deviceContext, ConvertorExecutor convertorExecutor) {
83         super(requestContextStack, deviceContext);
84         this.multipartType = multipartType;
85         this.convertorExecutor = convertorExecutor;
86     }
87
88     protected ConvertorExecutor getConvertorExecutor() {
89         return convertorExecutor;
90     }
91
92     /**
93      * Handle input and reply future.
94      *
95      * @param input the input
96      * @return the future
97      */
98     public Future<RpcResult<O>> handleAndReply(final I input) {
99         final ListenableFuture<RpcResult<List<MultipartReply>>> rpcReply = handleServiceCall(input);
100         ListenableFuture<RpcResult<O>> rpcResult = Futures.transform(rpcReply, resultTransformFunction);
101
102         if (Boolean.TRUE.equals(input.isStoreStats())) {
103             rpcResult = Futures.transform(rpcResult, resultStoreFunction);
104         }
105
106         return rpcResult;
107     }
108
109     @Override
110     protected OfHeader buildRequest(Xid xid, I input) throws Exception {
111         return RequestInputUtils.createMultipartHeader(multipartType, xid.getValue(), getVersion())
112                 .setMultipartRequestBody(buildRequestBody(input))
113                 .build();
114     }
115
116     /**
117      * Gets openflow version.
118      *
119      * @return the openflow version
120      */
121     protected OpenflowVersion getOfVersion() {
122         return ofVersion;
123     }
124
125     /**
126      * Build multipart request body.
127      *
128      * @param input the input
129      * @return the multipart request body
130      */
131     protected abstract MultipartRequestBody buildRequestBody(I input);
132
133     /**
134      * Build output from multipart reply input.
135      *
136      * @param input the input
137      * @return the output
138      */
139     protected abstract O buildReply(List<MultipartReply> input, boolean success);
140
141     /**
142      * Store statistics.
143      * TODO: Remove dependency on deviceContext from derived methods
144      * TODO: Return future, so we will be able to chain it
145      *
146      * @param output the output
147      * @throws Exception the exception
148      */
149     protected abstract void storeStatistics(O output) throws Exception;
150 }