58695cf8bbd4e06a3bc840195d16d7d6f3614eb7
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.v3po.translate.util.read;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import io.fd.honeycomb.v3po.translate.read.ReadContext;
22 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
23 import io.fd.honeycomb.v3po.translate.read.Reader;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 /**
32  * Simple DataBroker backed reader allowing to delegate reads to different brokers
33  */
34 public final class BindingBrokerReader<D extends DataObject> implements Reader<D> {
35
36     private final InstanceIdentifier<D> instanceIdentifier;
37     private final DataBroker dataBroker;
38     private final LogicalDatastoreType datastoreType;
39
40     public BindingBrokerReader(final Class<D> managedDataObjectType, final DataBroker dataBroker,
41                                final LogicalDatastoreType datastoreType) {
42         this.dataBroker = dataBroker;
43         this.datastoreType = datastoreType;
44         this.instanceIdentifier = InstanceIdentifier.create(managedDataObjectType);
45     }
46
47     @Nonnull
48     @Override
49     public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
50                                                @Nonnull final ReadContext ctx) throws ReadFailedException {
51         try (final ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
52             final CheckedFuture<? extends Optional<? extends DataObject>, org.opendaylight.controller.md.sal.common.api.data.ReadFailedException>
53                 read = readOnlyTransaction.read(datastoreType, id);
54             try {
55                 return read.checkedGet();
56             } catch (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException e) {
57                 throw new ReadFailedException(id, e);
58             }
59         }
60     }
61
62     @Nonnull
63     @Override
64     public InstanceIdentifier<D> getManagedDataObjectType() {
65         return instanceIdentifier;
66     }
67 }