Bug 6304 - Regression in Connection reconciliation functionality
[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.ConnectException;
22 import java.net.UnknownHostException;
23 import java.util.concurrent.atomic.AtomicInteger;
24
25 /**
26  * Copied from org.opendaylight.ovsdb.southbound.reconciliation.connection.ConnectionReconciliationTask
27  *
28  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 3/9/16.
29  */
30 public class ConnectionReconciliationTask extends ReconciliationTask {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ConnectionReconciliationTask.class);
33
34     private static final int RETRY_INTERVAL_FACTOR = 10000;
35     private static final int MAX_ATTEMPT = 10;
36
37     private AtomicInteger connectionAttempt = new AtomicInteger(0);
38
39     public ConnectionReconciliationTask(ReconciliationManager reconciliationManager, HwvtepConnectionManager
40             connectionManager, InstanceIdentifier<?> nodeIid, DataObject configData) {
41         super(reconciliationManager, connectionManager, nodeIid, configData);
42
43     }
44
45     @Override
46     public boolean reconcileConfiguration(HwvtepConnectionManager connectionManager) {
47         boolean result = false;
48         connectionAttempt.incrementAndGet();
49         InstanceIdentifier<Node> nIid = (InstanceIdentifier<Node>) nodeIid;
50         HwvtepGlobalAugmentation hwvtepNode = (HwvtepGlobalAugmentation)configData;
51
52         LOG.info("Retry({}) connection to Ovsdb Node {} ", connectionAttempt.get(), hwvtepNode.getConnectionInfo());
53         OvsdbClient client = null;
54         try {
55             client = connectionManager.connect(nIid, hwvtepNode);
56             if (client != null) {
57                 LOG.info("Successfully connected to Hwvtep Node {} ", hwvtepNode.getConnectionInfo());
58                 result = true;
59             } else {
60                 LOG.warn("Connection retry({}) failed for {}.",
61                         connectionAttempt.get(), hwvtepNode.getConnectionInfo());
62             }
63         } catch (UnknownHostException | ConnectException e) {
64             LOG.warn("Connection retry({}) failed with exception. ",connectionAttempt.get(), e);
65         }
66         return result;
67     }
68
69     @Override
70     public void doRetry(boolean wasLastAttemptSuccessful) {
71
72         if( !wasLastAttemptSuccessful && connectionAttempt.get() <= MAX_ATTEMPT ) {
73             reconciliationManager.enqueueForRetry(ConnectionReconciliationTask.this);
74         } else {
75             reconciliationManager.dequeue(this);
76         }
77     }
78
79     @Override
80     public void checkReadinessAndProcess() {
81         reconciliationManager.enqueue(this);
82     }
83
84     @Override
85     public long retryDelayInMills() {
86         return connectionAttempt.get() * RETRY_INTERVAL_FACTOR;
87     }
88 }