BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / connect / dom / BindingToDomCommitHandler.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.data.DataCommitHandler;
7 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
8 import org.opendaylight.controller.sal.common.util.CommitHandlerTransactions;
9 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction;
10 import org.opendaylight.controller.sal.core.api.data.DataProviderService;
11 import org.opendaylight.yangtools.yang.binding.DataObject;
12 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
14 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * @deprecated This is part of the legacy DataBrokerService
20  */
21 @Deprecated
22 class BindingToDomCommitHandler implements
23     DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject> {
24
25     private final Logger LOG = LoggerFactory.getLogger(BindingToDomCommitHandler.class);
26
27     private final ConcurrentMap<Object, DomToBindingTransaction> bindingOpenedTransactions;
28     private final ConcurrentMap<Object, BindingToDomTransaction> domOpenedTransactions;
29     private org.opendaylight.controller.sal.core.api.data.DataProviderService biDataService;
30     private BindingIndependentMappingService mappingService;
31
32     BindingToDomCommitHandler(final ConcurrentMap<Object, DomToBindingTransaction> bindingOpenedTransactions,
33         final ConcurrentMap<Object, BindingToDomTransaction> domOpenedTransactions) {
34         this.bindingOpenedTransactions = bindingOpenedTransactions;
35         this.domOpenedTransactions = domOpenedTransactions;
36     }
37
38     public void setBindingIndependentDataService(final DataProviderService biDataService) {
39         this.biDataService = biDataService;
40     }
41
42     public void setMappingService(final BindingIndependentMappingService mappingService) {
43         this.mappingService = mappingService;
44     }
45
46     @Override
47     public org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction<InstanceIdentifier<? extends DataObject>, DataObject> requestCommit(
48         final DataModification<InstanceIdentifier<? extends DataObject>, DataObject> bindingTransaction) {
49
50         /**
51          * Transaction was created as DOM transaction, in that case we do
52          * not need to forward it back.
53          */
54         if (bindingOpenedTransactions.containsKey(bindingTransaction.getIdentifier())) {
55             return CommitHandlerTransactions.allwaysSuccessfulTransaction(bindingTransaction);
56         }
57         DataModificationTransaction domTransaction = createBindingToDomTransaction(bindingTransaction);
58         BindingToDomTransaction wrapped = new BindingToDomTransaction(domTransaction, bindingTransaction, domOpenedTransactions);
59         LOG.trace("Forwarding Binding Transaction: {} as DOM Transaction: {} .",
60             bindingTransaction.getIdentifier(), domTransaction.getIdentifier());
61         return wrapped;
62     }
63
64     private DataModificationTransaction createBindingToDomTransaction(
65         final DataModification<InstanceIdentifier<? extends DataObject>, DataObject> source) {
66         if (biDataService == null) {
67             final String msg = "Binding Independent Service is not initialized correctly! Binding to DOM Transaction cannot be created for ";
68             LOG.error(msg + "{}", source);
69             throw new IllegalStateException(msg + source);
70         }
71         if (mappingService == null) {
72             final String msg = "Mapping Service is not initialized correctly! Binding to DOM Transaction cannot be created for ";
73             LOG.error(msg + "{}", source);
74             throw new IllegalStateException(msg + source);
75         }
76         DataModificationTransaction target = biDataService.beginTransaction();
77         LOG.debug("Created DOM Transaction {} for {},", target.getIdentifier(), source.getIdentifier());
78         for (InstanceIdentifier<? extends DataObject> entry : source.getRemovedConfigurationData()) {
79             org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier biEntry = mappingService.toDataDom(entry);
80             target.removeConfigurationData(biEntry);
81             LOG.debug("Delete of Binding Configuration Data {} is translated to {}", entry, biEntry);
82         }
83         for (InstanceIdentifier<? extends DataObject> entry : source.getRemovedOperationalData()) {
84             org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier biEntry = mappingService.toDataDom(entry);
85             target.removeOperationalData(biEntry);
86             LOG.debug("Delete of Binding Operational Data {} is translated to {}", entry, biEntry);
87         }
88         for (Map.Entry<InstanceIdentifier<? extends DataObject>, DataObject> entry : source.getUpdatedConfigurationData()
89             .entrySet()) {
90             Map.Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> biEntry = mappingService
91                 .toDataDom(entry);
92             target.putConfigurationData(biEntry.getKey(), biEntry.getValue());
93             LOG.debug("Update of Binding Configuration Data {} is translated to {}", entry, biEntry);
94         }
95         for (Map.Entry<InstanceIdentifier<? extends DataObject>, DataObject> entry : source.getUpdatedOperationalData()
96             .entrySet()) {
97             Map.Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> biEntry = mappingService
98                 .toDataDom(entry);
99             target.putOperationalData(biEntry.getKey(), biEntry.getValue());
100             LOG.debug("Update of Binding Operational Data {} is translated to {}", entry, biEntry);
101         }
102         return target;
103     }
104 }

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