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.concepts.Builder;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 * Simple DataBroker backed reader allowing to delegate reads to different brokers.
35 public final class BindingBrokerReader<D extends DataObject, B extends Builder<D>>
36 implements Reader<D, B>, AutoCloseable {
38 private final InstanceIdentifier<D> instanceIdentifier;
39 private final DataBroker dataBroker;
40 private final LogicalDatastoreType datastoreType;
41 private final ReflexiveReaderCustomizer<D, B> reflexiveReaderCustomizer;
43 public BindingBrokerReader(final InstanceIdentifier<D> instanceIdentifier,
44 final DataBroker dataBroker,
45 final LogicalDatastoreType datastoreType,
46 final Class<B> builderClass) {
47 this.reflexiveReaderCustomizer = new ReflexiveReaderCustomizer<>(instanceIdentifier.getTargetType(), builderClass);
48 this.instanceIdentifier = instanceIdentifier;
49 this.dataBroker = dataBroker;
50 this.datastoreType = datastoreType;
55 public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
56 @Nonnull final ReadContext ctx) throws ReadFailedException {
57 try (final ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
58 final CheckedFuture<? extends Optional<? extends DataObject>, org.opendaylight.controller.md.sal.common.api.data.ReadFailedException>
59 read = readOnlyTransaction.read(datastoreType, id);
61 return read.checkedGet();
62 } catch (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException e) {
63 throw new ReadFailedException(id, e);
69 public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final D readValue) {
70 reflexiveReaderCustomizer.merge(parentBuilder, readValue);
75 public B getBuilder(final InstanceIdentifier<D> id) {
76 return reflexiveReaderCustomizer.getBuilder(id);
80 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
81 @Nonnull final B builder,
82 @Nonnull final ReadContext ctx) throws ReadFailedException {
83 throw new UnsupportedOperationException("Not supported");
88 public InstanceIdentifier<D> getManagedDataObjectType() {
89 return instanceIdentifier;
93 public void close() throws Exception {