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