2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.util.read;
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;
32 * Simple DataBroker backed reader allowing to delegate reads to different brokers
34 public final class BindingBrokerReader<D extends DataObject> implements Reader<D> {
36 private final InstanceIdentifier<D> instanceIdentifier;
37 private final DataBroker dataBroker;
38 private final LogicalDatastoreType datastoreType;
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);
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);
55 return read.checkedGet();
56 } catch (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException e) {
57 throw new ReadFailedException(id, e);
64 public InstanceIdentifier<D> getManagedDataObjectType() {
65 return instanceIdentifier;