43334f06ac5b9aa1761998ffbc7e161725d43208
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / connect / dom / DomToBindingCommitHandler.java
1 package org.opendaylight.controller.sal.binding.impl.connect.dom;
2
3 import java.util.Map;
4 import java.util.concurrent.ConcurrentMap;
5
6 import org.opendaylight.controller.md.sal.common.api.RegistrationListener;
7 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
8 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandlerRegistration;
9 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
10 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
11 import org.opendaylight.controller.sal.common.util.CommitHandlerTransactions;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
13 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
15 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
16 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * @deprecated This is part of the legacy DataBrokerService
22  */
23 @Deprecated
24 class DomToBindingCommitHandler implements //
25     RegistrationListener<DataCommitHandlerRegistration<InstanceIdentifier<? extends DataObject>, DataObject>>, //
26     DataCommitHandler<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> {
27
28     private final Logger LOG = LoggerFactory.getLogger(DomToBindingCommitHandler.class);
29
30     private final ConcurrentMap<Object, DomToBindingTransaction> bindingOpenedTransactions;
31     private final ConcurrentMap<Object, BindingToDomTransaction> domOpenedTransactions;
32
33     DomToBindingCommitHandler(final ConcurrentMap<Object, DomToBindingTransaction> bindingOpenedTransactions,
34         final ConcurrentMap<Object, BindingToDomTransaction> domOpenedTransactions) {
35         this.bindingOpenedTransactions = bindingOpenedTransactions;
36         this.domOpenedTransactions = domOpenedTransactions;
37     }
38
39     private DataProviderService baDataService;
40     private BindingIndependentMappingService mappingService;
41
42     public void setBindingAwareDataService(final DataProviderService baDataService) {
43         this.baDataService = baDataService;
44     }
45
46     public void setMappingService(final BindingIndependentMappingService mappingService) {
47         this.mappingService = mappingService;
48     }
49
50     @Override
51     public void onRegister(final DataCommitHandlerRegistration<InstanceIdentifier<? extends DataObject>, DataObject> registration) {
52         mappingService.toDataDom(registration.getPath());
53     }
54
55     @Override
56     public void onUnregister(
57         final DataCommitHandlerRegistration<InstanceIdentifier<? extends DataObject>, DataObject> registration) {
58         // NOOP for now
59         // FIXME: do registration based on only active commit handlers.
60     }
61
62     @Override
63     public org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> requestCommit(
64         final DataModification<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> domTransaction) {
65         Object identifier = domTransaction.getIdentifier();
66
67         /**
68          * We checks if the transcation was originated in this mapper. If it
69          * was originated in this mapper we are returing allways success
70          * commit hanlder to prevent creating loop in two-phase commit and
71          * duplicating data.
72          */
73         if (domOpenedTransactions.containsKey(identifier)) {
74             return CommitHandlerTransactions.allwaysSuccessfulTransaction(domTransaction);
75         }
76
77         org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction baTransaction = createDomToBindingTransaction(domTransaction);
78         DomToBindingTransaction forwardedTransaction = new DomToBindingTransaction(baTransaction, domTransaction, bindingOpenedTransactions);
79         LOG.trace("Forwarding DOM Transaction: {} as Binding Transaction: {}.", domTransaction.getIdentifier(),
80             baTransaction.getIdentifier());
81         return forwardedTransaction;
82     }
83
84     private org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction createDomToBindingTransaction(
85         final DataModification<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> source) {
86         if (baDataService == null) {
87             final String msg = "Binding Aware Service is not initialized correctly! DOM to Binding Transaction cannot be created for ";
88             LOG.error(msg + "{}", source);
89             throw new IllegalStateException(msg + source);
90         }
91         if (mappingService == null) {
92             final String msg = "Mapping Service is not initialized correctly! DOM to Binding Transaction cannot be created for ";
93             LOG.error(msg + "{}", source);
94             throw new IllegalStateException(msg + source);
95         }
96
97         org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction target = baDataService
98             .beginTransaction();
99         for (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier entry : source.getRemovedConfigurationData()) {
100             try {
101
102                 InstanceIdentifier<?> baEntry = mappingService.fromDataDom(entry);
103                 target.removeConfigurationData(baEntry);
104             } catch (DeserializationException e) {
105                 LOG.error("Ommiting from BA transaction: {}.", entry, e);
106             }
107         }
108         for (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier entry : source.getRemovedOperationalData()) {
109             try {
110
111                 InstanceIdentifier<?> baEntry = mappingService.fromDataDom(entry);
112                 target.removeOperationalData(baEntry);
113             } catch (DeserializationException e) {
114                 LOG.error("Ommiting from BA transaction: {}.", entry, e);
115             }
116         }
117         for (Map.Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> entry : source
118             .getUpdatedConfigurationData().entrySet()) {
119             try {
120                 InstanceIdentifier<?> baKey = mappingService.fromDataDom(entry.getKey());
121                 DataObject baData = mappingService.dataObjectFromDataDom(baKey, entry.getValue());
122                 target.putConfigurationData(baKey, baData);
123             } catch (DeserializationException e) {
124                 LOG.error("Ommiting from BA transaction: {}.", entry.getKey(), e);
125             }
126         }
127         for (Map.Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> entry : source
128             .getUpdatedOperationalData().entrySet()) {
129             try {
130
131                 InstanceIdentifier<?> baKey = mappingService.fromDataDom(entry.getKey());
132                 DataObject baData = mappingService.dataObjectFromDataDom(baKey, entry.getValue());
133                 target.putOperationalData(baKey, baData);
134             } catch (DeserializationException e) {
135                 LOG.error("Ommiting from BA transaction: {}.", entry.getKey(), e);
136             }
137         }
138         return target;
139     }
140 }

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