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