/* * Copyright (c) 2017 Cisco and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.fd.hc2vpp.docs.core; import com.google.common.collect.ImmutableSet; import io.fd.honeycomb.translate.ModifiableSubtreeManagerRegistryBuilder; import io.fd.honeycomb.translate.write.Writer; import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** * Implementations of builder that collects handlers as they are bind */ public class CollectingWriterBuilder implements ModifiableWriterRegistryBuilder { private final List writeHandlers; public CollectingWriterBuilder() { writeHandlers = new LinkedList<>(); } private void addHandler(final Writer handler) { writeHandlers.add(new WriteHandler(handler)); } private void addHandler(final Writer handler, final Set> handledChildren) { writeHandlers.add(new WriteHandler(handler, handledChildren)); } @Override public ModifiableSubtreeManagerRegistryBuilder> add( @Nonnull Writer handler) { addHandler(handler); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> subtreeAdd( @Nonnull Set> handledChildren, @Nonnull Writer handler) { addHandler(handler, handledChildren); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addBefore( @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { addHandler(handler, Collections.singleton(relatedType)); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addBefore( @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { addHandler(handler); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> subtreeAddBefore( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { addHandler(handler, handledChildren); return null; } @Override public ModifiableSubtreeManagerRegistryBuilder> subtreeAddBefore( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { addHandler(handler, handledChildren); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addAfter( @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { addHandler(handler); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addAfter( @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { addHandler(handler); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> subtreeAddAfter( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { addHandler(handler, handledChildren); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> subtreeAddAfter( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { addHandler(handler, handledChildren); return this; } public List getWriteHandlers() { return writeHandlers; } public static class WriteHandler { private final Writer writer; private final Set handledNodes; public WriteHandler(Writer writer, Set> handledChildren) { this.writer = writer; this.handledNodes = ImmutableSet.builder() // add node managed by writer .add(writer.getManagedDataObjectType().getTargetType().getName()) // and set of handled children (may be empty in case of non subtree writers) .addAll(handledChildren.stream() .map(InstanceIdentifier::getTargetType) .map(Class::getName) .collect(Collectors.toSet())) .build(); } public WriteHandler(final Writer writer) { this(writer, Collections.emptySet()); } public Writer getWriter() { return writer; } public Set getHandledNodes() { return handledNodes; } } }