BUG-3579: device disconnection cleanup
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceTransactionChainManagerProvider.java
1 /*
2  * Copyright (c) 2015 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.impl.device;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16 import org.opendaylight.yangtools.concepts.Registration;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Created by Martin Bobak <mbobak@cisco.com> on 2.6.2015.
22  */
23 public class DeviceTransactionChainManagerProvider {
24
25
26     private static final Logger LOG = LoggerFactory.getLogger(DeviceTransactionChainManagerProvider.class);
27     private static final Map<NodeId, TransactionChainManager> txChManagers = new HashMap<>();
28
29     public TransactionChainManager provideTransactionChainManagerOrWaitForNotification(final ConnectionContext connectionContext,
30                                                                                        final DataBroker dataBroker,
31                                                                                        final ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler) {
32         final NodeId nodeId = connectionContext.getNodeId();
33         synchronized (this) {
34             TransactionChainManager transactionChainManager = txChManagers.get(nodeId);
35             if (null == transactionChainManager) {
36                 LOG.info("Creating new transaction chain for device {}", nodeId.toString());
37                 Registration registration = new Registration() {
38                     @Override
39                     public void close() throws Exception {
40                         txChManagers.remove(nodeId);
41                     }
42                 };
43                 transactionChainManager = new TransactionChainManager(dataBroker, connectionContext, registration);
44                 txChManagers.put(nodeId, transactionChainManager);
45                 return transactionChainManager;
46             } else {
47                 try {
48                     if (!transactionChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler)) {
49                         if (TransactionChainManager.TransactionChainManagerStatus.WORKING.equals(transactionChainManager.getTransactionChainManagerStatus())) {
50                             LOG.info("There already exists one handler for connection described as {}. Connection is working will not try again.", nodeId);
51                             connectionContext.closeConnection(false);
52                         } else {
53                             LOG.info("There already exists one handler for connection described as {}. Transaction chain manager is in state {}. Will try again.",
54                                     nodeId,
55                                     transactionChainManager.getTransactionChainManagerStatus());
56                             readyForNewTransactionChainHandler.onReadyForNewTransactionChain(connectionContext);
57                         }
58                     }
59                 } catch (Exception e) {
60                     LOG.info("Transaction closed handler registration for node {} failed because we most probably hit previous transaction chain  manager's close process. Will try again.", nodeId);
61                     readyForNewTransactionChainHandler.onReadyForNewTransactionChain(connectionContext);
62                 }
63             }
64         }
65         return null;
66     }
67
68
69 }