52837183b5c933a4b71f491d63d16961e75899ac
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / AbstractDOMBroker.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.controller.cluster.databroker;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collections;
14 import java.util.EnumMap;
15 import java.util.Map;
16 import java.util.concurrent.atomic.AtomicLong;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 abstract class AbstractDOMBroker extends AbstractDOMTransactionFactory<DOMStore>
35         implements DOMDataBroker, AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBroker.class);
38
39     private final AtomicLong txNum = new AtomicLong();
40     private final AtomicLong chainNum = new AtomicLong();
41     private final Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> extensions;
42     private volatile AutoCloseable closeable;
43
44     protected AbstractDOMBroker(final Map<LogicalDatastoreType, DOMStore> datastores) {
45         super(datastores);
46
47         boolean treeChange = true;
48         for (DOMStore ds : datastores.values()) {
49             if (!(ds instanceof DOMStoreTreeChangePublisher)) {
50                 treeChange = false;
51                 break;
52             }
53         }
54
55         if (treeChange) {
56             extensions = ImmutableMap.<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension>of(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
57                 @Override
58                 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(final DOMDataTreeIdentifier treeId, final L listener) {
59                     DOMStore publisher = getTxFactories().get(treeId.getDatastoreType());
60                     checkState(publisher != null, "Requested logical data store is not available.");
61
62                     return ((DOMStoreTreeChangePublisher) publisher).registerTreeChangeListener(treeId.getRootIdentifier(), listener);
63                 }
64             });
65         } else {
66             extensions = Collections.emptyMap();
67         }
68     }
69
70     public void setCloseable(final AutoCloseable closeable) {
71         this.closeable = closeable;
72     }
73
74     @Override
75     public void close() {
76         super.close();
77
78         if (closeable != null) {
79             try {
80                 closeable.close();
81             } catch (Exception e) {
82                 LOG.debug("Error closing instance", e);
83             }
84         }
85     }
86
87     @Override
88     protected Object newTransactionIdentifier() {
89         return "DOM-" + txNum.getAndIncrement();
90     }
91
92     @Override
93     public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
94                                                                                   final YangInstanceIdentifier path, final DOMDataChangeListener listener, final DataChangeScope triggeringScope) {
95
96         DOMStore potentialStore = getTxFactories().get(store);
97         checkState(potentialStore != null, "Requested logical data store is not available.");
98         return potentialStore.registerChangeListener(path, listener, triggeringScope);
99     }
100
101     @Override
102     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
103         return extensions;
104     }
105
106     @Override
107     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
108         checkNotClosed();
109
110         final Map<LogicalDatastoreType, DOMStoreTransactionChain> backingChains = new EnumMap<>(LogicalDatastoreType.class);
111         for (Map.Entry<LogicalDatastoreType, DOMStore> entry : getTxFactories().entrySet()) {
112             backingChains.put(entry.getKey(), entry.getValue().createTransactionChain());
113         }
114
115         final long chainId = chainNum.getAndIncrement();
116         LOG.debug("Transaction chain {} created with listener {}, backing store chains {}", chainId, listener,
117                 backingChains);
118         return new DOMBrokerTransactionChain(chainId, backingChains, this, listener);
119     }
120 }

©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.