Decompose RPC implementation classes
[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 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.List;
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.md.util.OpenflowVersion;
19 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
20 import org.opendaylight.openflowplugin.impl.services.AbstractMultipartService;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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.OfHeader;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28
29 /**
30  * The abstract direct statistics service.
31  * This abstract service provides wrappers and tools for all other derived statistics services.
32  *
33  * @param <I> the input type parameter
34  * @param <O> the output type parameter
35  */
36 abstract class AbstractDirectStatisticsService<I extends StoreStatsGrouping,
37                                                O extends DataContainer,
38                                                T extends OfHeader>
39         extends AbstractMultipartService<I, T> {
40
41     private final MultipartType multipartType;
42     private final OpenflowVersion ofVersion = OpenflowVersion.get(getVersion());
43     private final ConvertorExecutor convertorExecutor;
44     private final MultipartWriterProvider multipartWriterProvider;
45
46     /**
47      * Instantiates a new Abstract direct statistics service.
48      * @param multipartType           the multipart type
49      * @param requestContextStack      the request context stack
50      * @param deviceContext            the device context
51      * @param convertorExecutor        convertor executor
52      * @param multipartWriterProvider statistics writer provider
53      */
54     AbstractDirectStatisticsService(final MultipartType multipartType,
55                                     final RequestContextStack requestContextStack,
56                                     final DeviceContext deviceContext,
57                                     final ConvertorExecutor convertorExecutor,
58                                     final MultipartWriterProvider multipartWriterProvider) {
59         super(requestContextStack, deviceContext);
60         this.multipartType = multipartType;
61         this.convertorExecutor = convertorExecutor;
62         this.multipartWriterProvider = multipartWriterProvider;
63     }
64
65     /**
66      * Handle input and reply future.
67      *
68      * @param input the input
69      * @return the future
70      */
71     final ListenableFuture<RpcResult<O>> handleAndReply(final I input) {
72         final var rpcResult = Futures.transform(handleServiceCall(input), this::transformResult,
73             MoreExecutors.directExecutor());
74
75         return Boolean.TRUE.equals(input.getStoreStats())
76             ? Futures.transform(rpcResult, this::storeResult, MoreExecutors.directExecutor()) : rpcResult;
77     }
78
79     private RpcResult<O> transformResult(final RpcResult<List<T>> input) {
80         return requireNonNull(input).isSuccessful()
81                 ? RpcResultBuilder.success(buildReply(input.getResult(), input.isSuccessful())).build()
82                 : RpcResultBuilder.<O>failed().withRpcErrors(input.getErrors()).build();
83     }
84
85     private RpcResult<O> storeResult(final RpcResult<O> input) {
86         requireNonNull(input);
87
88         if (input.isSuccessful()) {
89             multipartWriterProvider.lookup(multipartType).ifPresent(writer -> {
90                 writer.write(input.getResult(), true);
91                 getTxFacade().submitTransaction();
92             });
93         }
94
95         return input;
96     }
97
98     /**
99      * Get multipart type.
100      * @return multipart type
101      */
102     protected MultipartType getMultipartType() {
103         return multipartType;
104     }
105
106     /**
107      * Get convertor executor.
108      * @return convertor executor
109      */
110     protected ConvertorExecutor getConvertorExecutor() {
111         return convertorExecutor;
112     }
113
114     /**
115      * Gets openflow version.
116      *
117      * @return the openflow version
118      */
119     protected OpenflowVersion getOfVersion() {
120         return ofVersion;
121     }
122
123     /**
124      * Build output from multipart reply input.
125      *
126      * @param input the input
127      * @return the output
128      */
129     protected abstract O buildReply(List<T> input, boolean success);
130
131 }