summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/AbstractSubtreeManagerRegistryBuilderBuilder.java
blob: 6a19ed22052c539cd66a6f9761ad4ee7d39caf9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2016 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.honeycomb.translate.util;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import io.fd.honeycomb.translate.ModifiableSubtreeManagerRegistryBuilder;
import io.fd.honeycomb.translate.SubtreeManager;
import io.fd.honeycomb.translate.SubtreeManagerRegistryBuilder;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public abstract class AbstractSubtreeManagerRegistryBuilderBuilder<S extends SubtreeManager<? extends DataObject>, R>
        implements ModifiableSubtreeManagerRegistryBuilder<S>, SubtreeManagerRegistryBuilder<R>, AutoCloseable {

    // Using directed acyclic graph to represent the ordering relationships between writers
    private final DirectedAcyclicGraph<InstanceIdentifier<?>, Order>
            handlersRelations = new DirectedAcyclicGraph<>((sourceVertex, targetVertex) -> new Order());
    private final Map<InstanceIdentifier<?>, S> handlersMap = new HashMap<>();

    /**
     * Add handler without any special relationship to any other type.
     */
    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> add(@Nonnull final S handler) {
        // Make IID wildcarded just in case
        // + the way InstanceIdentifier.create + equals work for Identifiable items is unexpected, meaning updates would
        // not be matched to writers in registry
        final InstanceIdentifier<?> targetType = RWUtils.makeIidWildcarded(handler.getManagedDataObjectType());
        checkWriterNotPresentYet(targetType);
        handlersRelations.addVertex(targetType);
        handlersMap.put(targetType, handler);
        return this;
    }

    /**
     * Add handler without any special relationship to any other type.
     */
    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> subtreeAdd(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
                                                                         @Nonnull final S handler) {
        add(getSubtreeHandler(handledChildren, handler));
        return this;
    }

    private void checkWriterNotPresentYet(final InstanceIdentifier<?> targetType) {
        Preconditions.checkArgument(!handlersMap.containsKey(targetType),
                "Writer for type: %s already present: %s", targetType, handlersMap.get(targetType));
    }

    /**
     * Add handler with relationship: to be executed before handler handling relatedType.
     */
    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> addBefore(@Nonnull final S handler,
                                                                        @Nonnull final InstanceIdentifier<?> relatedType) {
        final InstanceIdentifier<?> targetType = RWUtils.makeIidWildcarded(handler.getManagedDataObjectType());
        final InstanceIdentifier<?> wildcardedRelatedType = RWUtils.makeIidWildcarded(relatedType);
        checkWriterNotPresentYet(targetType);
        handlersRelations.addVertex(targetType);
        handlersRelations.addVertex(wildcardedRelatedType);
        addEdge(targetType, wildcardedRelatedType);
        handlersMap.put(targetType, handler);
        return this;
    }

    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> addBefore(@Nonnull final S handler,
                                                                        @Nonnull final Collection<InstanceIdentifier<?>> relatedTypes) {
        final InstanceIdentifier<?> targetType = RWUtils.makeIidWildcarded(handler.getManagedDataObjectType());
        checkWriterNotPresentYet(targetType);
        handlersRelations.addVertex(targetType);
        relatedTypes.stream()
                .map(RWUtils::makeIidWildcarded)
                .forEach(handlersRelations::addVertex);
        relatedTypes.stream()
                .map(RWUtils::makeIidWildcarded)
                .forEach(type -> addEdge(targetType, type));
        handlersMap.put(targetType, handler);
        return this;
    }

    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> subtreeAddBefore(
            @Nonnull final Set<InstanceIdentifier<?>> handledChildren,
            @Nonnull final S handler,
            @Nonnull final InstanceIdentifier<?> relatedType) {
        return addBefore(getSubtreeHandler(handledChildren, handler), relatedType);
    }

    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> subtreeAddBefore(
            @Nonnull final Set<InstanceIdentifier<?>> handledChildren,
            @Nonnull final S handler,
            @Nonnull final Collection<InstanceIdentifier<?>> relatedTypes) {
        return addBefore(getSubtreeHandler(handledChildren, handler), relatedTypes);
    }

    protected abstract S getSubtreeHandler(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
                                           @Nonnull final S handler);

    /**
     * Add handler with relationship: to be executed after handler handling relatedType.
     */
    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> addAfter(@Nonnull final S handler,
                                                                       @Nonnull final InstanceIdentifier<?> relatedType) {
        final InstanceIdentifier<?> targetType = RWUtils.makeIidWildcarded(handler.getManagedDataObjectType());
        final InstanceIdentifier<?> wildcardedRelatedType = RWUtils.makeIidWildcarded(relatedType);
        checkWriterNotPresentYet(targetType);
        handlersRelations.addVertex(targetType);
        handlersRelations.addVertex(wildcardedRelatedType);
        // set edge to indicate before relationship, just reversed
        addEdge(wildcardedRelatedType, targetType);
        handlersMap.put(targetType, handler);
        return this;
    }

    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> addAfter(@Nonnull final S handler,
                                                                       @Nonnull final Collection<InstanceIdentifier<?>> relatedTypes) {
        final InstanceIdentifier<?> targetType = RWUtils.makeIidWildcarded(handler.getManagedDataObjectType());
        checkWriterNotPresentYet(targetType);
        handlersRelations.addVertex(targetType);
        relatedTypes.stream()
                .map(RWUtils::makeIidWildcarded)
                .forEach(handlersRelations::addVertex);
        // set edge to indicate before relationship, just reversed
        relatedTypes.stream()
                .map(RWUtils::makeIidWildcarded)
                .forEach(type -> addEdge(type, targetType));
        handlersMap.put(targetType, handler);
        return this;
    }

    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> subtreeAddAfter(
            @Nonnull final Set<InstanceIdentifier<?>> handledChildren,
            @Nonnull final S handler,
            @Nonnull final InstanceIdentifier<?> relatedType) {
        return addAfter(getSubtreeHandler(handledChildren, handler), relatedType);
    }

    @Override
    public AbstractSubtreeManagerRegistryBuilderBuilder<S, R> subtreeAddAfter(
            @Nonnull final Set<InstanceIdentifier<?>> handledChildren,
            @Nonnull final S handler,
            @Nonnull final Collection<InstanceIdentifier<?>> relatedTypes) {
        return addAfter(getSubtreeHandler(handledChildren, handler), relatedTypes);
    }


    private void addEdge(final InstanceIdentifier<?> targetType,
                         final InstanceIdentifier<?> relatedType) {
        try {
            handlersRelations.addDagEdge(targetType, relatedType);
        } catch (DirectedAcyclicGraph.CycleFoundException e) {
            throw new IllegalArgumentException(String.format(
                    "Unable to add writer with relation: %s -> %s. Loop detected", targetType, relatedType), e);
        }
    }

    protected ImmutableMap<InstanceIdentifier<?>, S> getMappedHandlers() {
        final ImmutableMap.Builder<InstanceIdentifier<?>, S> builder = ImmutableMap.builder();
        // Iterate writer types according to their relationships from graph
        handlersRelations.iterator()
                .forEachRemaining(handlerType -> {
                    // There might be types stored just for relationship sake, no real writer, ignoring those
                    if (handlersMap.containsKey(handlerType)) {
                        builder.put(handlerType, handlersMap.get(handlerType));
                    }
                });

        // TODO HONEYCOMB-171 we could optimize subtree handlers, if there is a dedicated handler for a node managed by a subtree
        // handler, recreate the subtree handler with a subset of handled child nodes
        // This way it is not necessary to change the configuration of subtree writer, just to add a dedicated child
        // writer

        return builder.build();
    }

    @Override
    public void close() throws Exception {
        handlersMap.clear();
        // Wrap sets into another set to avoid concurrent modification ex in graph
        handlersRelations.removeAllEdges(Sets.newHashSet(handlersRelations.edgeSet()));
        handlersRelations.removeAllVertices(Sets.newHashSet(handlersRelations.vertexSet()));
    }

    // Represents edges in graph
    private class Order {}
}