18d79bf1b11b6402be6a14d1d8579eaf95ad4844
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / util / FxChainUtil.java
1 /**
2  * Copyright (c) 2016 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.applications.frsync.util;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.util.concurrent.FutureCallback;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yangtools.yang.common.RpcError;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Util methods for {@link com.google.common.util.concurrent.ListenableFuture} chaining.
25  */
26 public final class FxChainUtil {
27
28     private static final Logger LOG = LoggerFactory.getLogger(FxChainUtil.class);
29
30     private FxChainUtil() {
31         throw new IllegalStateException("This class should not be instantiated.");
32     }
33
34
35     public static FutureCallback<RpcResult<Void>> logResultCallback(final NodeId nodeId, final String prefix) {
36         return new FutureCallback<RpcResult<Void>>() {
37             @Override
38             public void onSuccess(@Nullable final RpcResult<Void> result) {
39                 if (result != null) {
40                     if (result.isSuccessful()) {
41                         LOG.debug(prefix + " finished successfully: {}", nodeId.getValue());
42                     } else {
43                         final Collection<RpcError> errors = MoreObjects.firstNonNull(result.getErrors(),
44                                 ImmutableList.<RpcError>of());
45                         LOG.debug(prefix + " failed: {} -> {}", nodeId.getValue(), Arrays.toString(errors.toArray()));
46                     }
47                 } else {
48                     LOG.debug(prefix + " reconciliation failed: {} -> null result", nodeId.getValue());
49                 }
50             }
51
52             @Override
53             public void onFailure(final Throwable failure) {
54                 LOG.debug(prefix + " reconciliation failed seriously: {}", nodeId.getValue(), failure);
55             }
56         };
57     }
58 }