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