BUG 5479: HWVtep Southbound doesn't retry connection
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / reconciliation / connection / ConnectionReconciliationTask.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.ovsdb.hwvtepsouthbound.reconciliation.connection;
9
10 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.ReconciliationManager;
11 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.ReconciliationTask;
12 import org.opendaylight.ovsdb.lib.OvsdbClient;
13 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionManager;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.net.UnknownHostException;
22 import java.util.concurrent.atomic.AtomicInteger;
23
24 /**
25  * Copied from org.opendaylight.ovsdb.southbound.reconciliation.connection.ConnectionReconciliationTask
26  *
27  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 3/9/16.
28  */
29 public class ConnectionReconciliationTask extends ReconciliationTask {
30
31     private static final Logger LOG = LoggerFactory.getLogger(ConnectionReconciliationTask.class);
32
33     private static final int RETRY_INTERVAL_FACTOR = 10000;
34     private static final int MAX_ATTEMPT = 10;
35
36     private AtomicInteger connectionAttempt = new AtomicInteger(0);
37
38     public ConnectionReconciliationTask(ReconciliationManager reconciliationManager, HwvtepConnectionManager
39             connectionManager, InstanceIdentifier<?> nodeIid, DataObject configData) {
40         super(reconciliationManager, connectionManager, nodeIid, configData);
41
42     }
43
44     @Override
45     public boolean reconcileConfiguration(HwvtepConnectionManager connectionManager) {
46         boolean result = false;
47         connectionAttempt.incrementAndGet();
48         InstanceIdentifier<Node> nIid = (InstanceIdentifier<Node>) nodeIid;
49         HwvtepGlobalAugmentation hwvtepNode = (HwvtepGlobalAugmentation)configData;
50
51         LOG.info("Retry({}) connection to Ovsdb Node {} ", connectionAttempt.get(), hwvtepNode.getConnectionInfo());
52         OvsdbClient client = null;
53         try {
54             client = connectionManager.connect(nIid, hwvtepNode);
55             if (client != null) {
56                 LOG.info("Successfully connected to Hwvtep Node {} ", hwvtepNode.getConnectionInfo());
57                 result = true;
58             } else {
59                 LOG.warn("Connection retry({}) failed for {}.",
60                         connectionAttempt.get(), hwvtepNode.getConnectionInfo());
61             }
62         } catch (UnknownHostException e) {
63             LOG.warn("Connection retry({}) failed with exception. ",connectionAttempt.get(), e);
64         }
65         return result;
66     }
67
68     @Override
69     public void doRetry(boolean wasLastAttemptSuccessful) {
70
71         if( !wasLastAttemptSuccessful && connectionAttempt.get() <= MAX_ATTEMPT ) {
72             reconciliationManager.enqueueForRetry(ConnectionReconciliationTask.this);
73         } else {
74             reconciliationManager.dequeue(this);
75         }
76     }
77
78     @Override
79     public void checkReadinessAndProcess() {
80         reconciliationManager.enqueue(this);
81     }
82
83     @Override
84     public long retryDelayInMills() {
85         return connectionAttempt.get() * RETRY_INTERVAL_FACTOR;
86     }
87 }