2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
20 import org.opendaylight.openflowplugin.impl.services.AbstractMultipartService;
21 import org.opendaylight.openflowplugin.impl.services.RequestInputUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.StoreStatsGrouping;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.MultipartRequestBody;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import javax.annotation.Nullable;
31 import java.util.List;
32 import java.util.concurrent.Future;
35 * The abstract direct statistics service.
36 * This abstract service provides wrappers and tools for all other derived statistics services.
38 * @param <I> the input type parameter
39 * @param <O> the output type parameter
41 public abstract class AbstractDirectStatisticsService<I extends StoreStatsGrouping, O> extends AbstractMultipartService<I> {
43 private final Function<RpcResult<List<MultipartReply>>, RpcResult<O>> resultTransformFunction =
44 new Function<RpcResult<List<MultipartReply>>, RpcResult<O>>() {
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();
54 private final AsyncFunction<RpcResult<O>, RpcResult<O>> resultStoreFunction =
55 new AsyncFunction<RpcResult<O>, RpcResult<O>>() {
58 public ListenableFuture<RpcResult<O>> apply(@Nullable RpcResult<O> input) throws Exception {
59 Preconditions.checkNotNull(input);
61 if (input.isSuccessful()) {
62 storeStatistics(input.getResult());
63 getDeviceContext().submitTransaction(); // TODO: If submitTransaction will ever return future, chain it
66 return Futures.immediateFuture(input);
70 private final MultipartType multipartType;
71 private final OpenflowVersion ofVersion = OpenflowVersion.get(getVersion());
74 * Instantiates a new Abstract direct statistics service.
76 * @param multipartType the multipart type
77 * @param requestContextStack the request context stack
78 * @param deviceContext the device context
80 protected AbstractDirectStatisticsService(MultipartType multipartType, RequestContextStack requestContextStack, DeviceContext deviceContext) {
81 super(requestContextStack, deviceContext);
82 this.multipartType = multipartType;
86 * Handle input and reply future.
88 * @param input the input
91 public Future<RpcResult<O>> handleAndReply(final I input) {
92 final ListenableFuture<RpcResult<List<MultipartReply>>> rpcReply = handleServiceCall(input);
93 ListenableFuture<RpcResult<O>> rpcResult = Futures.transform(rpcReply, resultTransformFunction);
95 if (input.isStoreStats()) {
96 rpcResult = Futures.transform(rpcResult, resultStoreFunction);
103 protected OfHeader buildRequest(Xid xid, I input) throws Exception {
104 return RequestInputUtils.createMultipartHeader(multipartType, xid.getValue(), getVersion())
105 .setMultipartRequestBody(buildRequestBody(input))
110 * Gets openflow version.
112 * @return the openflow version
114 protected OpenflowVersion getOfVersion() {
119 * Build multipart request body.
121 * @param input the input
122 * @return the multipart request body
124 protected abstract MultipartRequestBody buildRequestBody(I input);
127 * Build output from multipart reply input.
129 * @param input the input
132 protected abstract O buildReply(List<MultipartReply> input, boolean success);
136 * TODO: Remove dependency on deviceContext from derived methods
137 * TODO: Return future, so we will be able to chain it
139 * @param output the output
140 * @throws Exception the exception
142 protected abstract void storeStatistics(O output) throws Exception;