BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMDataBrokerImpl.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.md.sal.dom.broker.impl;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.util.Map.Entry;
13 import java.util.concurrent.atomic.AtomicLong;
14
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Optional;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.util.concurrent.CheckedFuture;
33 import com.google.common.util.concurrent.ListeningExecutorService;
34
35 public class DOMDataBrokerImpl extends AbstractDOMForwardedTransactionFactory<DOMStore> implements DOMDataBroker,
36         AutoCloseable {
37
38     private static final Logger LOG = LoggerFactory.getLogger(DOMDataBrokerImpl.class);
39
40     private final DOMDataCommitCoordinatorImpl coordinator;
41     private final AtomicLong txNum = new AtomicLong();
42     private final AtomicLong chainNum = new AtomicLong();
43
44     public DOMDataBrokerImpl(final ImmutableMap<LogicalDatastoreType, DOMStore> datastores,
45             final ListeningExecutorService executor) {
46         super(datastores);
47         this.coordinator = new DOMDataCommitCoordinatorImpl(executor);
48     }
49
50     @Override
51     protected Object newTransactionIdentifier() {
52         return "DOM-" + txNum.getAndIncrement();
53     }
54
55     @Override
56     public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
57             final YangInstanceIdentifier path, final DOMDataChangeListener listener, final DataChangeScope triggeringScope) {
58
59         DOMStore potentialStore = getTxFactories().get(store);
60         checkState(potentialStore != null, "Requested logical data store is not available.");
61         return potentialStore.registerChangeListener(path, listener, triggeringScope);
62     }
63
64     @Override
65     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
66         ImmutableMap.Builder<LogicalDatastoreType, DOMStoreTransactionChain> backingChainsBuilder = ImmutableMap
67                 .builder();
68         for (Entry<LogicalDatastoreType, DOMStore> entry : getTxFactories().entrySet()) {
69             backingChainsBuilder.put(entry.getKey(), entry.getValue().createTransactionChain());
70         }
71         long chainId = chainNum.getAndIncrement();
72         ImmutableMap<LogicalDatastoreType, DOMStoreTransactionChain> backingChains = backingChainsBuilder.build();
73         LOG.debug("Transactoin chain {} created with listener {}, backing store chains {}", chainId, listener,
74                 backingChains);
75         return new DOMDataBrokerTransactionChainImpl(chainId, backingChains, coordinator, listener);
76
77     }
78
79     @Override
80     public CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
81             final Iterable<DOMStoreThreePhaseCommitCohort> cohorts) {
82         LOG.debug("Transaction: {} submitted with cohorts {}.", transaction.getIdentifier(), cohorts);
83         return coordinator.submit(transaction, cohorts, Optional.<DOMDataCommitErrorListener> absent());
84     }
85
86 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.