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;
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.ReaderRegistry;
29 import io.fd.honeycomb.v3po.translate.util.RWUtils;
30 import io.fd.honeycomb.v3po.translate.read.Reader;
31 import java.util.Collections;
32 import java.util.List;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Simple reader registry able to perform and aggregated read (ROOT read) on top of all provided readers. Also able to
42 * delegate a specific read to one of the delegate readers.
44 * This could serve as a utility to hold & hide all available readers in upper layers.
46 public final class DelegatingReaderRegistry implements ReaderRegistry {
48 private static final Logger LOG = LoggerFactory.getLogger(DelegatingReaderRegistry.class);
50 private final Map<Class<? extends DataObject>, Reader<? extends DataObject>> rootReaders;
53 * Create new {@link DelegatingReaderRegistry}
55 * @param rootReaders List of delegate readers
57 public DelegatingReaderRegistry(@Nonnull final List<Reader<? extends DataObject>> rootReaders) {
58 this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION);
63 public Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> readAll(
64 @Nonnull final ReadContext ctx) throws ReadFailedException {
66 LOG.debug("Reading from all delegates: {}", this);
67 LOG.trace("Reading from all delegates: {}", rootReaders.values());
69 final Multimap<InstanceIdentifier<? extends DataObject>, DataObject> objects = LinkedListMultimap.create();
70 for (Reader<? extends DataObject> rootReader : rootReaders.values()) {
71 LOG.debug("Reading from delegate: {}", rootReader);
73 if (rootReader instanceof ListReader) {
74 final List<? extends DataObject> listEntries =
75 ((ListReader) rootReader).readList(rootReader.getManagedDataObjectType(), ctx);
76 if (!listEntries.isEmpty()) {
77 objects.putAll(rootReader.getManagedDataObjectType(), listEntries);
80 final Optional<? extends DataObject> read = rootReader.read(rootReader.getManagedDataObjectType(), ctx);
81 if (read.isPresent()) {
82 objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get()));
92 public Optional<? extends DataObject> read(@Nonnull final InstanceIdentifier<? extends DataObject> id,
93 @Nonnull final ReadContext ctx)
94 throws ReadFailedException {
95 final InstanceIdentifier.PathArgument first = checkNotNull(
96 Iterables.getFirst(id.getPathArguments(), null), "Empty id");
97 final Reader<? extends DataObject> reader = rootReaders.get(first.getType());
99 "Unable to read %s. Missing reader. Current readers for: %s", id, rootReaders.keySet());
100 LOG.debug("Reading from delegate: {}", reader);
101 return reader.read(id, ctx);
105 * @throws UnsupportedOperationException This getter is not supported for reader registry since it does not manage a
110 public InstanceIdentifier<DataObject> getManagedDataObjectType() {
111 throw new UnsupportedOperationException("Root registry has no type");