2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.device;
11 import java.util.HashMap;
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;
21 * Created by Martin Bobak <mbobak@cisco.com> on 2.6.2015.
23 public class DeviceTransactionChainManagerProvider {
26 private static final Logger LOG = LoggerFactory.getLogger(DeviceTransactionChainManagerProvider.class);
27 private static final Map<NodeId, TransactionChainManager> txChManagers = new HashMap<>();
29 public TransactionChainManager provideTransactionChainManagerOrWaitForNotification(final ConnectionContext connectionContext,
30 final DataBroker dataBroker,
31 final ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler) {
32 final NodeId nodeId = connectionContext.getNodeId();
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() {
39 public void close() throws Exception {
40 txChManagers.remove(nodeId);
43 transactionChainManager = new TransactionChainManager(dataBroker, connectionContext, registration);
44 txChManagers.put(nodeId, transactionChainManager);
45 return transactionChainManager;
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);
53 LOG.info("There already exists one handler for connection described as {}. Transaction chain manager is in state {}. Will try again.",
55 transactionChainManager.getTransactionChainManagerStatus());
56 readyForNewTransactionChainHandler.onReadyForNewTransactionChain(connectionContext);
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);