0a948c7a824d029025edfab35ca6c949e6f34b94
[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.registry;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.base.Optional;
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.LinkedListMultimap;
24 import com.google.common.collect.Multimap;
25 import io.fd.honeycomb.v3po.translate.read.ListReader;
26 import io.fd.honeycomb.v3po.translate.read.ReadContext;
27 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
28 import io.fd.honeycomb.v3po.translate.read.Reader;
29 import io.fd.honeycomb.v3po.translate.read.registry.ReaderRegistry;
30 import io.fd.honeycomb.v3po.translate.util.RWUtils;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yangtools.concepts.Builder;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Simple reader registry able to perform and aggregated read (ROOT read) on top of all provided readers. Also able to
43  * delegate a specific read to one of the delegate readers.
44  * <p/>
45  * This could serve as a utility to hold & hide all available readers in upper layers.
46  */
47 public final class CompositeReaderRegistry implements ReaderRegistry {
48
49     private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistry.class);
50
51     private final Map<Class<? extends DataObject>, Reader<? extends DataObject, ? extends Builder<?>>> rootReaders;
52
53     /**
54      * Create new {@link CompositeReaderRegistry}.
55      *
56      * @param rootReaders List of delegate readers
57      */
58     public CompositeReaderRegistry(@Nonnull final List<Reader<? extends DataObject, ? extends Builder<?>>> rootReaders) {
59         this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION);
60     }
61
62     @Override
63     @Nonnull
64     public Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> readAll(
65         @Nonnull final ReadContext ctx) throws ReadFailedException {
66
67         LOG.debug("Reading from all delegates: {}", this);
68         LOG.trace("Reading from all delegates: {}", rootReaders.values());
69
70         final Multimap<InstanceIdentifier<? extends DataObject>, DataObject> objects = LinkedListMultimap.create();
71         for (Reader<? extends DataObject, ? extends Builder<?>> rootReader : rootReaders.values()) {
72             LOG.debug("Reading from delegate: {}", rootReader);
73
74             if (rootReader instanceof ListReader) {
75                 final List<? extends DataObject> listEntries =
76                         ((ListReader) rootReader).readList(rootReader.getManagedDataObjectType(), ctx);
77                 if (!listEntries.isEmpty()) {
78                     objects.putAll(rootReader.getManagedDataObjectType(), listEntries);
79                 }
80             } else {
81                 final Optional<? extends DataObject> read = rootReader.read(rootReader.getManagedDataObjectType(), ctx);
82                 if (read.isPresent()) {
83                     objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get()));
84                 }
85             }
86         }
87
88         return objects;
89     }
90
91     @Nonnull
92     @Override
93     public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
94                                                @Nonnull final ReadContext ctx)
95             throws ReadFailedException {
96         final InstanceIdentifier.PathArgument first = checkNotNull(
97                 Iterables.getFirst(id.getPathArguments(), null), "Empty id");
98         final Reader<? extends DataObject, ? extends Builder<?>> reader = rootReaders.get(first.getType());
99         checkNotNull(reader,
100                 "Unable to read %s. Missing reader. Current readers for: %s", id, rootReaders.keySet());
101         LOG.debug("Reading from delegate: {}", reader);
102         return reader.read(id, ctx);
103     }
104 }