46a59bb901b053d6a98ea6f269c0b7cf7c493dfa
[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.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;
34
35 /**
36  * Composite implementation of {@link ChildReader} able to place the read result into
37  * parent builder object.
38  */
39 @Beta
40 @ThreadSafe
41 public final class CompositeChildReader<C extends DataObject, B extends Builder<C>> extends AbstractCompositeReader<C, B>
42     implements ChildReader<C> {
43
44     private final ChildReaderCustomizer<C, B> customizer;
45
46     /**
47      * Create new {@link CompositeChildReader}
48      *
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
53      *
54      */
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;
61     }
62
63     /**
64      * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
65      */
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);
70     }
71
72     /**
73      * @see {@link CompositeChildReader#CompositeChildReader(Class, List, List, ChildReaderCustomizer)}
74      */
75     public CompositeChildReader(@Nonnull final Class<C> managedDataObjectType,
76                                 @Nonnull final ChildReaderCustomizer<C, B> customizer) {
77         this(managedDataObjectType, RWUtils.<C>emptyChildReaderList(), RWUtils.<C>emptyAugReaderList(),
78             customizer);
79     }
80
81     @Override
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);
86
87         if(read.isPresent()) {
88             customizer.merge(parentBuilder, read.get());
89         }
90     }
91
92     @Override
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());
97     }
98
99     @Override
100     protected B getBuilder(@Nonnull final InstanceIdentifier<C> id) {
101         return customizer.getBuilder(id);
102     }
103
104 }