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