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.impl.TraversalType;
22 import io.fd.honeycomb.v3po.translate.read.ReadContext;
23 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
24 import io.fd.honeycomb.v3po.translate.util.RWUtils;
25 import io.fd.honeycomb.v3po.translate.read.ChildReader;
26 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
27 import java.util.List;
28 import javax.annotation.Nonnull;
29 import javax.annotation.concurrent.ThreadSafe;
30 import org.opendaylight.yangtools.concepts.Builder;
31 import org.opendaylight.yangtools.yang.binding.Augmentation;
32 import org.opendaylight.yangtools.yang.binding.ChildOf;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 * Composite implementation of {@link ChildReader} able to place the read result into
38 * parent builder object.
42 public final class CompositeChildReader<C extends DataObject, B extends Builder<C>> extends AbstractCompositeReader<C, B>
43 implements ChildReader<C> {
45 private final ChildReaderCustomizer<C, B> customizer;
48 * Create new {@link CompositeChildReader}
50 * @param managedDataObjectType Class object for managed data type
51 * @param childReaders Child nodes(container, list) readers
52 * @param augReaders Child augmentations readers
53 * @param customizer Customizer instance to customize this generic reader
56 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
57 @Nonnull final List<ChildReader<? extends ChildOf<C>>> childReaders,
58 @Nonnull final List<ChildReader<? extends Augmentation<C>>> augReaders,
59 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
60 this(managedDataObjectType, childReaders, augReaders, customizer, TraversalType.PREORDER);
64 * Create new {@link CompositeChildReader}
66 * @param managedDataObjectType Class object for managed data type
67 * @param childReaders Child nodes(container, list) readers
68 * @param augReaders Child augmentations readers
69 * @param customizer Customizer instance to customize this generic reader
70 * @param traversalType Type of traversal to use in the tree of readers
73 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
74 @Nonnull final List<ChildReader<? extends ChildOf<C>>> childReaders,
75 @Nonnull final List<ChildReader<? extends Augmentation<C>>> augReaders,
76 @Nonnull final ChildReaderCustomizer<C, B> customizer,
77 @Nonnull final TraversalType traversalType) {
78 super(managedDataObjectType, childReaders, augReaders, traversalType);
79 this.customizer = customizer;
83 * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
85 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
86 @Nonnull final List<ChildReader<? extends ChildOf<C>>> childReaders,
87 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
88 this(managedDataObjectType, childReaders, RWUtils.<C>emptyAugReaderList(), customizer);
92 * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
94 public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
95 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
96 this(managedDataObjectType, RWUtils.emptyChildReaderList(), RWUtils.emptyAugReaderList(),
101 public final void read(@Nonnull final InstanceIdentifier<? extends DataObject> parentId,
102 @Nonnull final Builder<? extends DataObject> parentBuilder,
103 @Nonnull final ReadContext ctx) throws ReadFailedException {
104 final Optional<C> read = readCurrent(RWUtils.appendTypeToId(parentId, getManagedDataObjectType()), ctx);
106 if(read.isPresent()) {
107 customizer.merge(parentBuilder, read.get());
112 protected void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id, @Nonnull final B builder,
113 @Nonnull final ReadContext ctx)
114 throws ReadFailedException {
115 customizer.readCurrentAttributes(id, builder, ctx);
119 protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) {
120 return customizer.getBuilder(id);