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
8 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
10 import static java.util.Objects.requireNonNull;
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;
30 * The abstract direct statistics service.
31 * This abstract service provides wrappers and tools for all other derived statistics services.
33 * @param <I> the input type parameter
34 * @param <O> the output type parameter
36 abstract class AbstractDirectStatisticsService<I extends StoreStatsGrouping,
37 O extends DataContainer,
39 extends AbstractMultipartService<I, T> {
41 private final MultipartType multipartType;
42 private final OpenflowVersion ofVersion = OpenflowVersion.get(getVersion());
43 private final ConvertorExecutor convertorExecutor;
44 private final MultipartWriterProvider multipartWriterProvider;
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
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;
66 * Handle input and reply future.
68 * @param input the input
71 final ListenableFuture<RpcResult<O>> handleAndReply(final I input) {
72 final var rpcResult = Futures.transform(handleServiceCall(input), this::transformResult,
73 MoreExecutors.directExecutor());
75 return Boolean.TRUE.equals(input.getStoreStats())
76 ? Futures.transform(rpcResult, this::storeResult, MoreExecutors.directExecutor()) : rpcResult;
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();
85 private RpcResult<O> storeResult(final RpcResult<O> input) {
86 requireNonNull(input);
88 if (input.isSuccessful()) {
89 multipartWriterProvider.lookup(multipartType).ifPresent(writer -> {
90 writer.write(input.getResult(), true);
91 getTxFacade().submitTransaction();
100 * @return multipart type
102 protected MultipartType getMultipartType() {
103 return multipartType;
107 * Get convertor executor.
108 * @return convertor executor
110 protected ConvertorExecutor getConvertorExecutor() {
111 return convertorExecutor;
115 * Gets openflow version.
117 * @return the openflow version
119 protected OpenflowVersion getOfVersion() {
124 * Build output from multipart reply input.
126 * @param input the input
129 protected abstract O buildReply(List<T> input, boolean success);