0c6d0a1b4e245bd840ccb5663240937e557c0388
[honeycomb.git] / infra / translate-impl / src / main / java / io / fd / honeycomb / translate / impl / write / registry / FlatWriterRegistryBuilder.java
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.translate.impl.write.registry;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.common.collect.ImmutableMap;
21 import io.fd.honeycomb.translate.util.AbstractSubtreeManagerRegistryBuilderBuilder;
22 import io.fd.honeycomb.translate.util.YangDAG;
23 import io.fd.honeycomb.translate.write.Writer;
24 import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder;
25 import io.fd.honeycomb.translate.write.registry.WriterRegistry;
26 import io.fd.honeycomb.translate.write.registry.WriterRegistryBuilder;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import javax.annotation.Nonnull;
30 import javax.annotation.concurrent.NotThreadSafe;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Builder for {@link FlatWriterRegistry} allowing users to specify inter-writer relationships.
38  */
39 @NotThreadSafe
40 public final class FlatWriterRegistryBuilder
41         extends AbstractSubtreeManagerRegistryBuilderBuilder<Writer<? extends DataObject>, WriterRegistry>
42         implements ModifiableWriterRegistryBuilder, WriterRegistryBuilder {
43
44     private static final Logger LOG = LoggerFactory.getLogger(FlatWriterRegistryBuilder.class);
45
46     public FlatWriterRegistryBuilder(@Nonnull final YangDAG yangDAG) {
47         super(yangDAG);
48     }
49
50     @Override
51     protected Writer<? extends DataObject> getSubtreeHandler(final @Nonnull Set<InstanceIdentifier<?>> handledChildren,
52                                                              final @Nonnull Writer<? extends DataObject> writer) {
53         return SubtreeWriter.createForWriter(handledChildren, writer);
54     }
55
56     /**
57      * Create FlatWriterRegistry with writers ordered according to submitted relationships.
58      */
59     @Override
60     public WriterRegistry build() {
61         final ImmutableMap<InstanceIdentifier<?>, Writer<?>> mappedWriters = getMappedHandlers();
62         LOG.debug("Building writer registry with writers: {}",
63                 mappedWriters.keySet().stream()
64                         .map(InstanceIdentifier::getTargetType)
65                         .map(Class::getSimpleName)
66                         .collect(Collectors.joining(", ")));
67         LOG.trace("Building writer registry with writers: {}", mappedWriters);
68         return new FlatWriterRegistry(mappedWriters);
69     }
70
71     @VisibleForTesting
72     @Override
73     protected ImmutableMap<InstanceIdentifier<?>, Writer<? extends DataObject>> getMappedHandlers() {
74         return super.getMappedHandlers();
75     }
76 }