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

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