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 static com.google.common.base.Preconditions.checkArgument;
21 import com.google.common.annotations.Beta;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.v3po.translate.read.ReadContext;
24 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
25 import io.fd.honeycomb.v3po.translate.read.Reader;
26 import io.fd.honeycomb.v3po.translate.util.RWUtils;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yangtools.concepts.Builder;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 public abstract class AbstractGenericReader<D extends DataObject, B extends Builder<D>> implements Reader<D, B> {
37 private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericReader.class);
39 private final InstanceIdentifier<D> instanceIdentifier;
41 protected AbstractGenericReader(final InstanceIdentifier<D> managedDataObjectType) {
42 this.instanceIdentifier = RWUtils.makeIidWildcarded(managedDataObjectType);
47 public final InstanceIdentifier<D> getManagedDataObjectType() {
48 return instanceIdentifier;
52 * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present.
55 protected Optional<D> readCurrent(@Nonnull final InstanceIdentifier<D> id,
56 @Nonnull final ReadContext ctx) throws ReadFailedException {
57 LOG.debug("{}: Reading current: {}", this, id);
58 final B builder = getBuilder(id);
59 // Cache empty value to determine if anything has changed later TODO cache in a field
60 final D emptyValue = builder.build();
62 LOG.trace("{}: Reading current attributes", this);
63 readCurrentAttributes(id, builder, ctx);
65 // Need to check whether anything was filled in to determine if data is present or not.
66 final D built = builder.build();
67 final Optional<D> read = built.equals(emptyValue)
71 LOG.debug("{}: Current node read successfully. Result: {}", this, read);
77 @SuppressWarnings("unchecked")
78 public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
79 @Nonnull final ReadContext ctx)
80 throws ReadFailedException {
81 LOG.trace("{}: Reading : {}", this, id);
82 checkArgument(id.getTargetType().equals(getManagedDataObjectType().getTargetType()));
83 return readCurrent((InstanceIdentifier<D>) id, ctx);
87 public String toString() {
88 return String.format("Reader[%s]", getManagedDataObjectType().getTargetType().getSimpleName());