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.impl.read;
19 import com.google.common.annotations.Beta;
20 import com.google.common.base.Optional;
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.util.RWUtils;
24 import io.fd.honeycomb.v3po.translate.read.ChildReader;
25 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
26 import java.util.List;
27 import javax.annotation.Nonnull;
28 import javax.annotation.concurrent.ThreadSafe;
29 import org.opendaylight.yangtools.concepts.Builder;
30 import org.opendaylight.yangtools.yang.binding.Augmentation;
31 import org.opendaylight.yangtools.yang.binding.ChildOf;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 * Composite implementation of {@link ChildReader} able to place the read result into
37 * parent builder object.
41 public final class CompositeChildReader<C extends DataObject, B extends Builder<C>> extends AbstractCompositeReader<C, B>
42 implements ChildReader<C> {
44 private final ChildReaderCustomizer<C, B> customizer;
47 * Create new {@link CompositeChildReader}
49 * @param managedDataObjectType Class object for managed data type
50 * @param childReaders Child nodes(container, list) readers
51 * @param augReaders Child augmentations readers
52 * @param customizer Customizer instance to customize this generic reader
55 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
56 @Nonnull final List<ChildReader<? extends ChildOf<C>>> childReaders,
57 @Nonnull final List<ChildReader<? extends Augmentation<C>>> augReaders,
58 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
59 super(managedDataObjectType, childReaders, augReaders);
60 this.customizer = customizer;
64 * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
66 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
67 @Nonnull final List<ChildReader<? extends ChildOf<C>>> childReaders,
68 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
69 this(managedDataObjectType, childReaders, RWUtils.<C>emptyAugReaderList(), customizer);
73 * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
75 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
76 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
77 this(managedDataObjectType, RWUtils.<C>emptyChildReaderList(), RWUtils.<C>emptyAugReaderList(),
82 public final void read(@Nonnull final InstanceIdentifier<? extends DataObject> parentId,
83 @Nonnull final Builder<? extends DataObject> parentBuilder,
84 @Nonnull final ReadContext ctx) throws ReadFailedException {
85 final Optional<C> read = readCurrent(RWUtils.appendTypeToId(parentId, getManagedDataObjectType()), ctx);
87 if(read.isPresent()) {
88 customizer.merge(parentBuilder, read.get());
93 protected void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, @Nonnull final B builder,
94 @Nonnull final ReadContext ctx)
95 throws ReadFailedException {
96 customizer.readCurrentAttributes(id, builder, ctx.getContext());
100 protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) {
101 return customizer.getBuilder(id);