9bfbc2450138924d76e3fdef7bb50f57a1c8337e
[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 static com.google.common.base.Preconditions.checkArgument;
20
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;
33
34 @Beta
35 public abstract class AbstractGenericReader<D extends DataObject, B extends Builder<D>> implements Reader<D, B> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericReader.class);
38
39     private final InstanceIdentifier<D> instanceIdentifier;
40
41     protected AbstractGenericReader(final InstanceIdentifier<D> managedDataObjectType) {
42         this.instanceIdentifier = RWUtils.makeIidWildcarded(managedDataObjectType);
43     }
44
45     @Nonnull
46     @Override
47     public final InstanceIdentifier<D> getManagedDataObjectType() {
48         return instanceIdentifier;
49     }
50
51     /**
52      * @param id {@link InstanceIdentifier} pointing to current node. In case of keyed list, key must be present.
53      *
54      */
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();
61
62         LOG.trace("{}: Reading current attributes", this);
63         readCurrentAttributes(id, builder, ctx);
64
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)
68             ? Optional.absent()
69             : Optional.of(built);
70
71         LOG.debug("{}: Current node read successfully. Result: {}", this, read);
72         return read;
73     }
74
75     @Nonnull
76     @Override
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);
84     }
85
86     @Override
87     public String toString() {
88         return String.format("Reader[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
89     }
90 }