Deprecate old MD-SAL APIs for removal
[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
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.Map.Entry;
17 import java.util.concurrent.atomic.AtomicLong;
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.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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Deprecated(forRemoval = true)
34 public abstract class AbstractDOMDataBroker extends AbstractDOMForwardedTransactionFactory<DOMStore>
35        implements DOMDataBroker {
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(
56                     DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
57                         @Override
58                         public <L extends DOMDataTreeChangeListener> ListenerRegistration<L>
59                             registerDataTreeChangeListener(final DOMDataTreeIdentifier treeId, final L listener) {
60                                 DOMStore publisher = getTxFactories().get(treeId.getDatastoreType());
61                                 checkState(publisher != null,
62                                            "Requested logical data store is not available.");
63
64                                 return ((DOMStoreTreeChangePublisher) publisher)
65                                         .registerTreeChangeListener(treeId.getRootIdentifier(), listener);
66                             }
67                     });
68         } else {
69             extensions = Collections.emptyMap();
70         }
71     }
72
73     public void setCloseable(final AutoCloseable closeable) {
74         this.closeable = closeable;
75     }
76
77     @Override
78     @SuppressWarnings("checkstyle:IllegalCatch")
79     public void close() {
80         super.close();
81
82         if (closeable != null) {
83             try {
84                 closeable.close();
85             } catch (Exception e) {
86                 LOG.debug("Error closing instance", e);
87             }
88         }
89     }
90
91     @Override
92     protected Object newTransactionIdentifier() {
93         return "DOM-" + txNum.getAndIncrement();
94     }
95
96     @Override
97     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
98         return extensions;
99     }
100
101     @Override
102     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
103         checkNotClosed();
104
105         final Map<LogicalDatastoreType, DOMStoreTransactionChain> backingChains = new EnumMap<>(
106                 LogicalDatastoreType.class);
107         for (Entry<LogicalDatastoreType, DOMStore> entry : getTxFactories().entrySet()) {
108             backingChains.put(entry.getKey(), entry.getValue().createTransactionChain());
109         }
110
111         final long chainId = chainNum.getAndIncrement();
112         LOG.debug("Transactoin chain {} created with listener {}, backing store chains {}", chainId, listener,
113                   backingChains);
114         return new DOMDataBrokerTransactionChainImpl(chainId, backingChains, this, listener);
115     }
116 }

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