89f9f5675f816991949a80a99f0092aba8716861
[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.impl.read;
18
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;
35
36 /**
37  * Composite implementation of {@link ChildReader} able to place the read result into
38  * parent builder object.
39  */
40 @Beta
41 @ThreadSafe
42 public final class CompositeChildReader<C extends DataObject, B extends Builder<C>> extends AbstractCompositeReader<C, B>
43     implements ChildReader<C> {
44
45     private final ChildReaderCustomizer<C, B> customizer;
46
47     /**
48      * Create new {@link CompositeChildReader}
49      *
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
54      *
55      */
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);
61     }
62
63     /**
64      * Create new {@link CompositeChildReader}
65      *
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
71      *
72      */
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;
80     }
81
82     /**
83      * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
84      */
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);
89     }
90
91     /**
92      * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
93      */
94     public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
95                                 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
96         this(managedDataObjectType, RWUtils.emptyChildReaderList(), RWUtils.emptyAugReaderList(),
97             customizer);
98     }
99
100     @Override
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);
105
106         if(read.isPresent()) {
107             customizer.merge(parentBuilder, read.get());
108         }
109     }
110
111     @Override
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);
116     }
117
118     @Override
119     protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) {
120         return customizer.getBuilder(id);
121     }
122
123 }