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