68aa3956ecad2e862c28470e91cdcbb543c99da9
[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.concepts.Builder;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 /**
33  * Simple DataBroker backed reader allowing to delegate reads to different brokers.
34  */
35 public final class BindingBrokerReader<D extends DataObject, B extends Builder<D>>
36         implements Reader<D, B>, AutoCloseable {
37
38     private final InstanceIdentifier<D> instanceIdentifier;
39     private final DataBroker dataBroker;
40     private final LogicalDatastoreType datastoreType;
41     private final ReflexiveReaderCustomizer<D, B> reflexiveReaderCustomizer;
42
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;
51     }
52
53     @Nonnull
54     @Override
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);
60             try {
61                 return read.checkedGet();
62             } catch (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException e) {
63                 throw new ReadFailedException(id, e);
64             }
65         }
66     }
67
68     @Override
69     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final D readValue) {
70         reflexiveReaderCustomizer.merge(parentBuilder, readValue);
71     }
72
73     @Nonnull
74     @Override
75     public B getBuilder(final InstanceIdentifier<D> id) {
76         return reflexiveReaderCustomizer.getBuilder(id);
77     }
78
79     @Override
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");
84     }
85
86     @Nonnull
87     @Override
88     public InstanceIdentifier<D> getManagedDataObjectType() {
89         return instanceIdentifier;
90     }
91
92     @Override
93     public void close() throws Exception {
94         // Noop
95     }
96 }