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.util.read.registry;
19 import static com.google.common.base.Preconditions.checkNotNull;
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;
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;
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.
45 * This could serve as a utility to hold & hide all available readers in upper layers.
47 public final class CompositeReaderRegistry implements ReaderRegistry {
49 private static final Logger LOG = LoggerFactory.getLogger(CompositeReaderRegistry.class);
51 private final Map<Class<? extends DataObject>, Reader<? extends DataObject, ? extends Builder<?>>> rootReaders;
54 * Create new {@link CompositeReaderRegistry}.
56 * @param rootReaders List of delegate readers
58 public CompositeReaderRegistry(@Nonnull final List<Reader<? extends DataObject, ? extends Builder<?>>> rootReaders) {
59 this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION);
64 public Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> readAll(
65 @Nonnull final ReadContext ctx) throws ReadFailedException {
67 LOG.debug("Reading from all delegates: {}", this);
68 LOG.trace("Reading from all delegates: {}", rootReaders.values());
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);
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);
81 final Optional<? extends DataObject> read = rootReader.read(rootReader.getManagedDataObjectType(), ctx);
82 if (read.isPresent()) {
83 objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get()));
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());
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);