Fix spotbugs logging complaints
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / services / compatibility / AbstractCompatibleStatService.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.compatibility;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.math.BigInteger;
15 import java.util.List;
16 import java.util.concurrent.atomic.AtomicLong;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
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.md.util.OpenflowVersion;
22 import org.opendaylight.openflowplugin.api.openflow.statistics.compatibility.BackwardCompatibleAtomicService;
23 import org.opendaylight.openflowplugin.impl.services.AbstractMultipartService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Pulled up common functionality of notification emitting stats services (backward compatibility relic).
35  */
36 public abstract class AbstractCompatibleStatService<I extends DataContainer, O, N extends Notification> extends
37         AbstractMultipartService<I, MultipartReply> implements BackwardCompatibleAtomicService<I, O> {
38
39     private static final Logger LOG = LoggerFactory.getLogger(AbstractCompatibleStatService.class);
40
41     private final AtomicLong compatibilityXidSeed;
42     private final OpenflowVersion ofVersion;
43
44     public AbstractCompatibleStatService(RequestContextStack requestContextStack,
45                                          DeviceContext deviceContext,
46                                          AtomicLong compatibilityXidSeed) {
47         super(requestContextStack, deviceContext);
48         this.compatibilityXidSeed = compatibilityXidSeed;
49         ofVersion = OpenflowVersion.get(getVersion());
50     }
51
52     public OpenflowVersion getOfVersion() {
53         return ofVersion;
54     }
55
56     @Override
57     public ListenableFuture<RpcResult<O>> handleAndNotify(final I input,
58                                                           final NotificationPublishService notificationPublishService) {
59         // prepare emulated xid
60         final long emulatedXid = compatibilityXidSeed.incrementAndGet();
61         final TransactionId emulatedTxId = new TransactionId(BigInteger.valueOf(emulatedXid));
62
63         // do real processing
64         final ListenableFuture<RpcResult<List<MultipartReply>>> rpcResultListenableFuture = handleServiceCall(input);
65
66         // hook notification publishing
67         Futures.addCallback(rpcResultListenableFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
68             @Override
69             public void onSuccess(@Nullable RpcResult<List<MultipartReply>> result) {
70                 if (result != null && result.isSuccessful()) {
71                     // transform rpc result (raw multipart) to notification
72                     final N flowNotification = transformToNotification(result.getResult(), emulatedTxId);
73                     notificationPublishService.offerNotification(flowNotification);
74                 } else {
75                     LOG.debug("compatibility callback failed - NOT emitting notification: {}",
76                             input.getClass().getSimpleName());
77                 }
78             }
79
80             @Override
81             public void onFailure(Throwable throwable) {
82                 LOG.debug("compatibility callback crashed - NOT emitting notification: {}",
83                         input.getClass().getSimpleName(), throwable);
84             }
85         }, MoreExecutors.directExecutor());
86
87         return RpcResultBuilder.success(buildTxCapableResult(emulatedTxId)).buildFuture();
88     }
89
90     public abstract O buildTxCapableResult(TransactionId emulatedTxId);
91
92     public abstract N transformToNotification(List<MultipartReply> result, TransactionId emulatedTxId);
93 }