f23c107070ab160e92d0adf2a6a0bf6ecf2bc09c
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / ForwardedBindingDataBroker.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.binding.impl;
9
10 import org.opendaylight.controller.md.sal.binding.api.BindingDataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.BindingDataReadOnlyTransaction;
12 import org.opendaylight.controller.md.sal.binding.api.BindingDataReadWriteTransaction;
13 import org.opendaylight.controller.md.sal.binding.api.BindingDataWriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.sal.core.api.model.SchemaService;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
27
28 import com.google.common.base.Optional;
29 import com.google.common.util.concurrent.ListenableFuture;
30
31 /**
32  * The DataBrokerImpl simply defers to the DOMDataBroker for all its operations.
33  * All transactions and listener registrations are wrapped by the DataBrokerImpl
34  * to allow binding aware components to use the DataBroker transparently.
35  *
36  * Besides this the DataBrokerImpl and it's collaborators also cache data that
37  * is already transformed from the binding independent to binding aware format
38  *
39  * TODO : All references in this class to CompositeNode should be switched to
40  * NormalizedNode once the MappingService is updated
41  *
42  */
43 public class ForwardedBindingDataBroker extends AbstractForwardedDataBroker implements BindingDataBroker {
44
45     public ForwardedBindingDataBroker(final DOMDataBroker domDataBroker, final BindingIndependentMappingService mappingService, final SchemaService schemaService) {
46         super(domDataBroker, mappingService,schemaService);
47     }
48
49     @Override
50     public BindingDataReadOnlyTransaction newReadOnlyTransaction() {
51         return new BindingDataReadTransactionImpl(getDelegate().newReadOnlyTransaction(),getCodec());
52     }
53
54     @Override
55     public BindingDataReadWriteTransaction newReadWriteTransaction() {
56         return new BindingDataReadWriteTransactionImpl(getDelegate().newReadWriteTransaction(),getCodec());
57     }
58
59     @Override
60     public BindingDataWriteTransaction newWriteOnlyTransaction() {
61         return new BindingDataWriteTransactionImpl<DOMDataWriteTransaction>(getDelegate().newWriteOnlyTransaction(),getCodec());
62     }
63
64     private abstract class AbstractBindingTransaction<T extends AsyncTransaction<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>>>
65             extends AbstractForwardedTransaction<T> implements AsyncTransaction<InstanceIdentifier<?>, DataObject> {
66
67         protected AbstractBindingTransaction(final T delegate, final BindingToNormalizedNodeCodec codec) {
68             super(delegate, codec);
69         }
70
71         @Override
72         public Object getIdentifier() {
73             return getDelegate().getIdentifier();
74         }
75
76     }
77
78     private class BindingDataReadTransactionImpl extends AbstractBindingTransaction<DOMDataReadOnlyTransaction> implements
79             BindingDataReadOnlyTransaction {
80
81         protected BindingDataReadTransactionImpl(final DOMDataReadOnlyTransaction delegate,
82                 final BindingToNormalizedNodeCodec codec) {
83             super(delegate, codec);
84         }
85
86         @Override
87         public ListenableFuture<Optional<DataObject>> read(final LogicalDatastoreType store,
88                 final InstanceIdentifier<?> path) {
89             return doRead(getDelegate(), store, path);
90         }
91
92         @Override
93         public void close() {
94             getDelegate().close();
95         }
96     }
97
98     private class BindingDataWriteTransactionImpl<T extends DOMDataWriteTransaction> extends
99             AbstractBindingTransaction<T> implements BindingDataWriteTransaction {
100
101         protected BindingDataWriteTransactionImpl(final T delegate, final BindingToNormalizedNodeCodec codec) {
102             super(delegate, codec);
103
104         }
105
106         @Override
107         public boolean cancel() {
108             return doCancel(getDelegate());
109         }
110
111         @Override
112         public void put(final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
113             doPut(getDelegate(), store, path, data);
114         }
115
116         @Override
117         public void merge(final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
118             doMerge(getDelegate(), store, path, data);
119         }
120
121         @Override
122         public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
123             doDelete(getDelegate(), store, path);
124         }
125
126         @Override
127         public ListenableFuture<RpcResult<TransactionStatus>> commit() {
128             return doCommit(getDelegate());
129         }
130     }
131
132     private class BindingDataReadWriteTransactionImpl extends
133             BindingDataWriteTransactionImpl<DOMDataReadWriteTransaction> implements BindingDataReadWriteTransaction {
134
135         protected BindingDataReadWriteTransactionImpl(final DOMDataReadWriteTransaction delegate,
136                 final BindingToNormalizedNodeCodec codec) {
137             super(delegate, codec);
138         }
139
140         @Override
141         public ListenableFuture<Optional<DataObject>> read(final LogicalDatastoreType store,
142                 final InstanceIdentifier<?> path) {
143             return doRead(getDelegate(), store, path);
144         }
145     }
146 }

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