d93e9546fda24a15bea5ea28f9290ba44a036707
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / AbstractDOMDataBroker.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 import com.google.common.collect.ImmutableMap;
12 import java.util.Collections;
13 import java.util.EnumMap;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.concurrent.atomic.AtomicLong;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class AbstractDOMDataBroker extends AbstractDOMForwardedTransactionFactory<DOMStore> implements DOMDataBroker, AutoCloseable {
36     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMDataBroker.class);
37
38     private final AtomicLong txNum = new AtomicLong();
39     private final AtomicLong chainNum = new AtomicLong();
40     private final Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> extensions;
41     private volatile AutoCloseable closeable;
42
43     protected AbstractDOMDataBroker(final Map<LogicalDatastoreType, DOMStore> datastores) {
44         super(datastores);
45
46         boolean treeChange = true;
47         for (DOMStore ds : datastores.values()) {
48             if (!(ds instanceof DOMStoreTreeChangePublisher)) {
49                 treeChange = false;
50                 break;
51             }
52         }
53
54         if (treeChange) {
55             extensions = ImmutableMap.<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension>of(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
56                 @Nonnull
57                 @Override
58                 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(@Nonnull final DOMDataTreeIdentifier treeId, @Nonnull 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     @Nonnull
102     @Override
103     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
104         return extensions;
105     }
106
107     @Override
108     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
109         checkNotClosed();
110
111         final Map<LogicalDatastoreType, DOMStoreTransactionChain> backingChains = new EnumMap<>(LogicalDatastoreType.class);
112         for (Entry<LogicalDatastoreType, DOMStore> entry : getTxFactories().entrySet()) {
113             backingChains.put(entry.getKey(), entry.getValue().createTransactionChain());
114         }
115
116         final long chainId = chainNum.getAndIncrement();
117         LOG.debug("Transactoin chain {} created with listener {}, backing store chains {}", chainId, listener,
118                 backingChains);
119         return new DOMDataBrokerTransactionChainImpl(chainId, backingChains, this, listener);
120     }
121 }

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