387f3cc7c2b47cae3238d5df158feee35503adcc
[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;
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.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;
33 import java.util.Map;
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;
39
40 /**
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.
43  *
44  * This could serve as a utility to hold & hide all available readers in upper layers.
45  */
46 public final class DelegatingReaderRegistry implements ReaderRegistry {
47
48     private static final Logger LOG = LoggerFactory.getLogger(DelegatingReaderRegistry.class);
49
50     private final Map<Class<? extends DataObject>, Reader<? extends DataObject>> rootReaders;
51
52     /**
53      * Create new {@link DelegatingReaderRegistry}
54      *
55      * @param rootReaders List of delegate readers
56      */
57     public DelegatingReaderRegistry(@Nonnull final List<Reader<? extends DataObject>> rootReaders) {
58         this.rootReaders = RWUtils.uniqueLinkedIndex(checkNotNull(rootReaders), RWUtils.MANAGER_CLASS_FUNCTION);
59     }
60
61     @Override
62     @Nonnull
63     public Multimap<InstanceIdentifier<? extends DataObject>, ? extends DataObject> readAll(
64         @Nonnull final ReadContext ctx) throws ReadFailedException {
65
66         LOG.debug("Reading from all delegates: {}", this);
67         LOG.trace("Reading from all delegates: {}", rootReaders.values());
68
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);
72
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);
78                 }
79             } else {
80                 final Optional<? extends DataObject> read = rootReader.read(rootReader.getManagedDataObjectType(), ctx);
81                 if (read.isPresent()) {
82                     objects.putAll(rootReader.getManagedDataObjectType(), Collections.singletonList(read.get()));
83                 }
84             }
85         }
86
87         return objects;
88     }
89
90     @Nonnull
91     @Override
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());
98         checkNotNull(reader,
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);
102     }
103
104     /**
105      * @throws UnsupportedOperationException This getter is not supported for reader registry since it does not manage a
106      *                                       specific node type
107      */
108     @Nonnull
109     @Override
110     public InstanceIdentifier<DataObject> getManagedDataObjectType() {
111         throw new UnsupportedOperationException("Root registry has no type");
112     }
113 }