Bump MRI upstreams
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / FrmReconciliationServiceImpl.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.applications.frm.impl;
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 com.google.common.util.concurrent.SettableFuture;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.ReconcileNodeInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.ReconcileNodeOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.ReconcileNodeOutputBuilder;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
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 @Singleton
35 public class FrmReconciliationServiceImpl implements FrmReconciliationService {
36
37     private static final Logger LOG = LoggerFactory.getLogger(FrmReconciliationServiceImpl.class);
38
39     private final ForwardingRulesManagerImpl forwardingRulesManagerImpl;
40
41     @Inject
42     public FrmReconciliationServiceImpl(ForwardingRulesManagerImpl forwardingRulesManagerImpl) {
43         this.forwardingRulesManagerImpl = forwardingRulesManagerImpl;
44     }
45
46     private static Node buildNode(long nodeIid) {
47         NodeId nodeId = new NodeId("openflow:" + nodeIid);
48         Node nodeDpn = new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
49         return nodeDpn;
50     }
51
52     @Override
53     public ListenableFuture<RpcResult<ReconcileNodeOutput>> reconcileNode(ReconcileNodeInput input) {
54         LOG.debug("Triggering reconciliation for node: {}", input.getNodeId());
55         Node nodeDpn = buildNode(input.getNodeId().longValue());
56         InstanceIdentifier<FlowCapableNode> connectedNode = InstanceIdentifier.builder(Nodes.class)
57                 .child(Node.class, nodeDpn.key()).augmentation(FlowCapableNode.class).build();
58         SettableFuture<RpcResult<ReconcileNodeOutput>> rpcResult = SettableFuture.create();
59         ListenableFuture<Boolean> futureResult = forwardingRulesManagerImpl
60                 .getNodeListener().reconcileConfiguration(connectedNode);
61         Futures.addCallback(futureResult, new ResultCallBack(futureResult, rpcResult),
62                 MoreExecutors.directExecutor());
63         LOG.debug("Completing reconciliation for node: {}", input.getNodeId());
64         return rpcResult;
65     }
66
67     private static class ResultCallBack implements FutureCallback<Boolean> {
68         private final SettableFuture<RpcResult<ReconcileNodeOutput>> futureResult;
69
70         ResultCallBack(ListenableFuture<Boolean> rpcResult,
71                        SettableFuture<RpcResult<ReconcileNodeOutput>> futureResult) {
72             this.futureResult = futureResult;
73         }
74
75         @Override
76         public void onSuccess(Boolean result) {
77             if (result) {
78                 ReconcileNodeOutput output = new ReconcileNodeOutputBuilder().setResult(result).build();
79                 futureResult.set(RpcResultBuilder.success(output).build());
80             } else {
81                 futureResult.set(RpcResultBuilder.<ReconcileNodeOutput>failed()
82                         .withError(ErrorType.APPLICATION, "Error while triggering reconciliation").build());
83             }
84
85         }
86
87         @Override
88         public void onFailure(Throwable error) {
89             LOG.error("initReconciliation failed", error);
90             futureResult.set(RpcResultBuilder.<ReconcileNodeOutput>failed()
91                     .withError(ErrorType.RPC, "Error while calling RPC").build());
92         }
93     }
94 }