Bug 1999: fix msgspy
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcTaskUtil.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.core.sal;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.concurrent.Future;
13
14 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
15 import org.opendaylight.controller.sal.common.util.RpcErrors;
16 import org.opendaylight.controller.sal.common.util.Rpcs;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
19 import org.opendaylight.openflowplugin.openflow.md.core.MessageFactory;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
23 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.opendaylight.yangtools.yang.common.RpcError;
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
30 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32
33 import com.google.common.base.Objects;
34 import com.google.common.collect.Lists;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.Futures;
37 import com.google.common.util.concurrent.ListenableFuture;
38 import com.google.common.util.concurrent.SettableFuture;
39
40 /**
41  * 
42  */
43 public abstract class OFRpcTaskUtil {
44
45     /**
46      * @param taskContext 
47      * @param isBarrier 
48      * @param cookie 
49      * @return rpcResult of given type, containing wrapped errors of barrier sending (if any) or success
50      */
51     public static Collection<RpcError> manageBarrier(OFRpcTaskContext taskContext, Boolean isBarrier,
52             SwitchConnectionDistinguisher cookie) {
53         Collection<RpcError> errors = null;
54         if (Objects.firstNonNull(isBarrier, Boolean.FALSE)) {
55             Future<RpcResult<BarrierOutput>> barrierFuture = sendBarrier(taskContext.getSession(), cookie, taskContext.getMessageService());
56             try {
57                 RpcResult<BarrierOutput> barrierResult = barrierFuture.get(
58                         taskContext.getMaxTimeout(), taskContext.getMaxTimeoutUnit());
59                 if (!barrierResult.isSuccessful()) {
60                     errors = barrierResult.getErrors();
61                 }
62             } catch (Exception e) {
63                 RpcError rpcError = RpcErrors.getRpcError(
64                         OFConstants.APPLICATION_TAG, OFConstants.ERROR_TAG_TIMEOUT, 
65                         "barrier sending failed", ErrorSeverity.WARNING, 
66                         "switch failed to respond on barrier request - message ordering is not preserved", ErrorType.RPC, e);
67                 errors = Lists.newArrayList(rpcError);
68             }
69         } 
70         
71         if (errors == null) {
72             errors = Collections.emptyList();
73         }
74         
75         return errors;
76     }
77
78     /**
79      * @param session
80      * @param cookie
81      * @param messageService
82      * @return barrier response
83      */
84     private static Future<RpcResult<BarrierOutput>> sendBarrier(SessionContext session, 
85             SwitchConnectionDistinguisher cookie, IMessageDispatchService messageService) {
86         BarrierInput barrierInput = MessageFactory.createBarrier(
87                 session.getFeatures().getVersion(), session.getNextXid());
88         return messageService.barrier(barrierInput, cookie);
89     }
90
91     /**
92      * @param result rpcResult with success = false, errors = given collection
93      * @param barrierErrors
94      */
95     public static <T> void wrapBarrierErrors(SettableFuture<RpcResult<T>> result,
96             Collection<RpcError> barrierErrors) {
97         result.set(Rpcs.<T>getRpcResult(false, barrierErrors));
98     }
99     
100     /**
101      * @param task of rpc
102      * @param originalResult
103      * @param notificationProviderService
104      * @param notificationComposer lazy notification composer
105      */
106     public static <R, N extends Notification, INPUT extends DataContainer> void hookFutureNotification(
107             final OFRpcTask<INPUT, R> task,
108             ListenableFuture<R> originalResult, 
109             final NotificationProviderService notificationProviderService, 
110             final NotificationComposer<N> notificationComposer) {
111         Futures.addCallback(originalResult, new FutureCallback<R>() {
112             @Override
113             public void onSuccess(R result) {
114                 if (null != notificationProviderService) {
115                     notificationProviderService.publish(notificationComposer.compose());
116                 }
117                 task.getTaskContext().getMessageSpy().spyMessage(
118                         task.getInput(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_SUCCESS);
119             }
120             
121             @Override
122             public void onFailure(Throwable t) {
123                 task.getTaskContext().getMessageSpy().spyMessage(
124                         task.getInput(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
125             }
126         });
127     }
128
129 }