Merge "Bug 1007 - Statistics manager loses threads to uncaught exceptions"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / AbstractForwardedTransaction.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.common.api.data.AsyncTransaction;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
13 import org.opendaylight.yangtools.concepts.Delegator;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23
24
25 abstract class AbstractForwardedTransaction<T extends AsyncTransaction<InstanceIdentifier, NormalizedNode<?, ?>>>
26         implements Delegator<T>, Identifiable<Object> {
27
28     private final T delegate;
29     private final BindingToNormalizedNodeCodec codec;
30
31     public AbstractForwardedTransaction(final T delegateTx, final BindingToNormalizedNodeCodec codec) {
32         this.delegate = Preconditions.checkNotNull(delegateTx, "Delegate must not be null");
33         this.codec = Preconditions.checkNotNull(codec, "Codec must not be null");
34     }
35
36
37     @Override
38     public final  Object getIdentifier() {
39         return delegate.getIdentifier();
40     }
41
42     @Override
43     public final  T getDelegate() {
44         return delegate;
45     }
46
47     @SuppressWarnings("unchecked")
48     protected final <S extends AsyncTransaction<InstanceIdentifier, NormalizedNode<?, ?>>> S getDelegateChecked(final Class<S> txType) {
49         Preconditions.checkState(txType.isInstance(delegate));
50         return (S) delegate;
51     }
52
53     protected final BindingToNormalizedNodeCodec getCodec() {
54         return codec;
55     }
56
57     protected final ListenableFuture<Optional<DataObject>> doRead(final DOMDataReadTransaction readTx,
58             final LogicalDatastoreType store, final org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> path) {
59         return Futures.transform(readTx.read(store, codec.toNormalized(path)), codec.deserializeFunction(path));
60     }
61 }