1a5c5a1d5f3473d87f372a5c50b55885e46c2c4b
[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 java.math.BigInteger;
15 import java.util.List;
16 import java.util.concurrent.atomic.AtomicLong;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.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.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, O, N extends Notification> extends AbstractMultipartService<I> implements BackwardCompatibleAtomicService<I, O> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(AbstractCompatibleStatService.class);
38
39     private final AtomicLong compatibilityXidSeed;
40     private final OpenflowVersion ofVersion;
41
42     public AbstractCompatibleStatService(RequestContextStack requestContextStack, DeviceContext deviceContext, AtomicLong compatibilityXidSeed) {
43         super(requestContextStack, deviceContext);
44         this.compatibilityXidSeed = compatibilityXidSeed;
45         ofVersion = OpenflowVersion.get(getVersion());
46     }
47
48     public OpenflowVersion getOfVersion() {
49         return ofVersion;
50     }
51
52     @Override
53     public ListenableFuture<RpcResult<O>> handleAndNotify(final I input, final NotificationPublishService notificationPublishService) {
54         // prepare emulated xid
55         final long emulatedXid = compatibilityXidSeed.incrementAndGet();
56         final TransactionId emulatedTxId = new TransactionId(BigInteger.valueOf(emulatedXid));
57
58         // do real processing
59         final ListenableFuture<RpcResult<List<MultipartReply>>> rpcResultListenableFuture = handleServiceCall(input);
60
61         // hook notification publishing
62         Futures.addCallback(rpcResultListenableFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
63             @Override
64             public void onSuccess(@Nullable RpcResult<List<MultipartReply>> result) {
65                 if (result != null && result.isSuccessful()) {
66                     // transform rpc result (raw multipart) to notification
67                     final N flowNotification = transformToNotification(result.getResult(), emulatedTxId);
68                     notificationPublishService.offerNotification(flowNotification);
69                 } else {
70                     LOG.debug("compatibility callback failed - NOT emitting notification: {}", input.getClass().getSimpleName());
71                 }
72             }
73
74             @Override
75             public void onFailure(Throwable t) {
76                 LOG.debug("compatibility callback crashed - NOT emitting notification: {}", input.getClass().getSimpleName(), t);
77             }
78         });
79
80         return RpcResultBuilder.<O>success(buildTxCapableResult(emulatedTxId)).buildFuture();
81     }
82
83     public abstract O buildTxCapableResult(TransactionId emulatedTxId);
84
85     public abstract N transformToNotification(List<MultipartReply> result, TransactionId emulatedTxId);
86 }