summaryrefslogtreecommitdiffstats
path: root/v3po/data-impl/src/main/java/io/fd/honeycomb/v3po/data/impl/ModifiableDataTreeDelegator.java
blob: 91de1885ebc3bbd8092f7194ec0ad38b8f5abe85 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * 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.v3po.data.impl;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.Futures.immediateCheckedFuture;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.CheckedFuture;
import io.fd.honeycomb.v3po.data.DataModification;
import io.fd.honeycomb.v3po.data.ReadableDataManager;
import io.fd.honeycomb.v3po.translate.TranslationException;
import io.fd.honeycomb.v3po.translate.util.write.TransactionMappingContext;
import io.fd.honeycomb.v3po.translate.util.write.TransactionWriteContext;
import io.fd.honeycomb.v3po.translate.write.WriteContext;
import io.fd.honeycomb.v3po.translate.write.WriterRegistry;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Extension of {@link ModifiableDataTreeManager} that propagates data changes to underlying writer layer before they
 * are fully committed in the backing data tree. Data changes are propagated in BA format.
 */
public final class ModifiableDataTreeDelegator extends ModifiableDataTreeManager {

    private static final Logger LOG = LoggerFactory.getLogger(ModifiableDataTreeDelegator.class);
    private static final ReadableDataManager EMPTY_OPERATIONAL = p -> immediateCheckedFuture(Optional.absent());

    private final WriterRegistry writerRegistry;
    private final org.opendaylight.controller.md.sal.binding.api.DataBroker contextBroker;
    // TODO what to use instead of deprecated BindingNormalizedNodeSerializer ?
    private final BindingNormalizedNodeSerializer serializer;

    /**
     * Creates configuration data tree instance.
     *  @param serializer     service for serialization between Java Binding Data representation and NormalizedNode
     *                       representation.
     * @param dataTree       data tree for configuration data representation
     * @param writerRegistry service for translation between Java Binding Data and data provider, capable of performing
     * @param contextBroker BA broker providing full access to mapping context data
     */
    public ModifiableDataTreeDelegator(@Nonnull final BindingNormalizedNodeSerializer serializer,
                                       @Nonnull final DataTree dataTree,
                                       @Nonnull final WriterRegistry writerRegistry,
                                       @Nonnull final org.opendaylight.controller.md.sal.binding.api.DataBroker contextBroker) {
        super(dataTree);
        this.contextBroker = checkNotNull(contextBroker, "contextBroker should not be null");
        this.serializer = checkNotNull(serializer, "serializer should not be null");
        this.writerRegistry = checkNotNull(writerRegistry, "writerRegistry should not be null");
    }

    @Override
    public DataModification newModification() {
        return new ConfigSnapshot(super.newModification());
    }

    private final class ConfigSnapshot extends ModifiableDataTreeManager.ConfigSnapshot {

        private final DataModification untouchedModification;

        /**
         * @param untouchedModification DataModification captured while this modification/snapshot was created.
         *                              To be used later while invoking writers to provide them with before state
         *                              (state without current modifications).
         *                              It must be captured as close as possible to when current modification started.
         */
        ConfigSnapshot(final DataModification untouchedModification) {
            this.untouchedModification = untouchedModification;
        }

        /**
         * Pass the changes to underlying writer layer.
         * Transform from BI to BA.
         * Revert(Write data before to subtrees that have been successfully modified before failure) in case of failure.
         */
        @Override
        protected void processCandidate(final DataTreeCandidate candidate)
            throws TranslationException {
            final DataTreeCandidateNode rootNode = candidate.getRootNode();
            final YangInstanceIdentifier rootPath = candidate.getRootPath();
            LOG.trace("ConfigDataTree.modify() rootPath={}, rootNode={}, dataBefore={}, dataAfter={}",
                rootPath, rootNode, rootNode.getDataBefore(), rootNode.getDataAfter());

            final ModificationDiff modificationDiff =
                    ModificationDiff.recursivelyFromCandidate(YangInstanceIdentifier.EMPTY, rootNode);
            LOG.debug("ConfigDataTree.modify() diff: {}", modificationDiff);

            final Map<InstanceIdentifier<?>, DataObject> nodesBefore = toBindingAware(modificationDiff.getModificationsBefore());
            LOG.debug("ConfigDataTree.modify() extracted nodesBefore={}", nodesBefore.keySet());
            final Map<InstanceIdentifier<?>, DataObject> nodesAfter = toBindingAware(modificationDiff.getModificationsAfter());
            LOG.debug("ConfigDataTree.modify() extracted nodesAfter={}", nodesAfter.keySet());

            try (final WriteContext ctx = getTransactionWriteContext()) {
                writerRegistry.update(nodesBefore, nodesAfter, ctx);

                final CheckedFuture<Void, TransactionCommitFailedException> contextUpdateResult =
                    ((TransactionMappingContext) ctx.getMappingContext()).submit();
                // Blocking on context data update
                contextUpdateResult.checkedGet();

            } catch (WriterRegistry.BulkUpdateException e) {
                LOG.warn("Failed to apply all changes", e);
                LOG.info("Trying to revert successful changes for current transaction");

                try {
                    e.revertChanges();
                    LOG.info("Changes successfully reverted");
                } catch (WriterRegistry.Reverter.RevertFailedException revertFailedException) {
                    // fail with failed revert
                    LOG.error("Failed to revert successful changes", revertFailedException);
                    throw revertFailedException;
                }

                throw e; // fail with success revert
            } catch (TransactionCommitFailedException e) {
                // FIXME revert should probably occur when context is not written successfully
                final String msg = "Error while updating mapping context data";
                LOG.error(msg, e);
                throw new TranslationException(msg, e);
            } catch (TranslationException e) {
                LOG.error("Error while processing data change (before={}, after={})", nodesBefore, nodesAfter, e);
                throw e;
            }
        }

        private TransactionWriteContext getTransactionWriteContext() {
            // Before Tx must use modification
            final DOMDataReadOnlyTransaction beforeTx = ReadOnlyTransaction.create(untouchedModification, EMPTY_OPERATIONAL);
            // After Tx must use current modification
            final DOMDataReadOnlyTransaction afterTx = ReadOnlyTransaction.create(this, EMPTY_OPERATIONAL);
            final TransactionMappingContext mappingContext = new TransactionMappingContext(
                contextBroker.newReadWriteTransaction());
            return new TransactionWriteContext(serializer, beforeTx, afterTx, mappingContext);
        }

        private Map<InstanceIdentifier<?>, DataObject> toBindingAware(final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> biNodes) {
            return ModifiableDataTreeDelegator.toBindingAware(biNodes, serializer);
        }
    }

    @VisibleForTesting
    static Map<InstanceIdentifier<?>, DataObject> toBindingAware(final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> biNodes,
                                                                 final BindingNormalizedNodeSerializer serializer) {
        final HashMap<InstanceIdentifier<?>, DataObject> transformed = new HashMap<>(biNodes.size());
        for (Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> biEntry : biNodes.entrySet()) {
            final Map.Entry<InstanceIdentifier<?>, DataObject> baEntry = serializer.fromNormalizedNode(biEntry.getKey(), biEntry.getValue());
            if (baEntry != null) {
                transformed.put(baEntry.getKey(), baEntry.getValue());
            }
        }
        return transformed;
    }

    @VisibleForTesting
    static final class ModificationDiff {

        private static final ModificationDiff EMPTY_DIFF = new ModificationDiff(Collections.emptyMap(), Collections.emptyMap());
        private static final EnumSet LEAF_MODIFICATIONS = EnumSet.of(ModificationType.WRITE, ModificationType.DELETE);

        private final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> modificationsBefore;
        private final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> modificationsAfter;

        private ModificationDiff(@Nonnull final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> modificationsBefore,
                                 @Nonnull final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> modificationsAfter) {
            this.modificationsBefore = modificationsBefore;
            this.modificationsAfter = modificationsAfter;
        }

        Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getModificationsBefore() {
            return modificationsBefore;
        }

        Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getModificationsAfter() {
            return modificationsAfter;
        }

        private ModificationDiff merge(final ModificationDiff other) {
            if (this == EMPTY_DIFF) {
                return other;
            }

            if (other == EMPTY_DIFF) {
                return this;
            }

            return new ModificationDiff(join(modificationsBefore, other.modificationsBefore),
                    join(modificationsAfter, other.modificationsAfter));
        }

        private static Map<YangInstanceIdentifier, NormalizedNode<?, ?>> join(
                final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> mapOne,
                final Map<YangInstanceIdentifier, NormalizedNode<?, ?>> mapTwo) {
            // Check unique modifications
            // TODO Probably not necessary to check
            final Sets.SetView<YangInstanceIdentifier> duplicates = Sets.intersection(mapOne.keySet(), mapTwo.keySet());
            checkArgument(duplicates.size() == 0, "Duplicates detected: %s. In maps: %s and %s", duplicates, mapOne, mapTwo);
            final HashMap<YangInstanceIdentifier, NormalizedNode<?, ?>> joined = new HashMap<>();
            joined.putAll(mapOne);
            joined.putAll(mapTwo);
            return joined;
        }

        private static ModificationDiff createFromBefore(YangInstanceIdentifier idBefore, DataTreeCandidateNode candidate) {
            return new ModificationDiff(
                    Collections.singletonMap(idBefore, candidate.getDataBefore().get()),
                    Collections.emptyMap());
        }

        private static ModificationDiff create(YangInstanceIdentifier id, DataTreeCandidateNode candidate) {
            return new ModificationDiff(
                    Collections.singletonMap(id, candidate.getDataBefore().get()),
                    Collections.singletonMap(id, candidate.getDataAfter().get()));
        }

        private static ModificationDiff createFromAfter(YangInstanceIdentifier idAfter, DataTreeCandidateNode candidate) {
            return new ModificationDiff(
                    Collections.emptyMap(),
                    Collections.singletonMap(idAfter, candidate.getDataAfter().get()));
        }

        /**
         * Produce a diff from a candidate node recursively.
         */
        @Nonnull
        static ModificationDiff recursivelyFromCandidate(@Nonnull final YangInstanceIdentifier yangIid,
                                                         @Nonnull final DataTreeCandidateNode currentCandidate) {
            switch (currentCandidate.getModificationType()) {
                case APPEARED:
                case DISAPPEARED:
                case UNMODIFIED: {
                    // (dis)appeared nodes are not important, no real data to process
                    return ModificationDiff.EMPTY_DIFF;
                }
                case WRITE: {
                    return currentCandidate.getDataBefore().isPresent()
                            ? ModificationDiff.create(yangIid, currentCandidate)
                            : ModificationDiff.createFromAfter(yangIid, currentCandidate);
                    // TODO HONEYCOMB-94 process children recursively to get modifications for child nodes
                }
                case DELETE:
                    return ModificationDiff.createFromBefore(yangIid, currentCandidate);
                case SUBTREE_MODIFIED: {
                    // Modifications here are presented also for leaves. However that kind of granularity is not required
                    // So if there's a modified leaf, mark current complex node also as modification
                    java.util.Optional<Boolean> leavesModified = currentCandidate.getChildNodes().stream()
                            .filter(ModificationDiff::isLeaf)
                            // For some reason, we get modifications on unmodified list keys TODO debug and report ODL bug
                            // and that messes up our modifications collection here, so we need to skip
                            .filter(ModificationDiff::isModification)
                            .map(child -> LEAF_MODIFICATIONS.contains(child.getModificationType()))
                            .reduce((boolOne, boolTwo) -> boolOne || boolTwo);

                    if (leavesModified.isPresent() && leavesModified.get()) {
                        return ModificationDiff.create(yangIid, currentCandidate);
                        // TODO HONEYCOMB-94 process children recursively to get modifications for child nodes even if current
                        // was modified
                    } else {
                        // SUBTREE MODIFIED, no modification on current, but process children recursively
                        return currentCandidate.getChildNodes().stream()
                                // not interested in modifications to leaves
                                .filter(child -> !isLeaf(child))
                                .map(candidate -> recursivelyFromCandidate(yangIid.node(candidate.getIdentifier()), candidate))
                                .reduce(ModificationDiff::merge)
                                .orElse(EMPTY_DIFF);
                    }
                }
                default:
                    throw new IllegalStateException("Unknown modification type: "
                            + currentCandidate.getModificationType() + ". Unsupported");
            }
        }

        /**
         * Check whether candidate.before and candidate.after is different. If not
         * return false.
         */
        private static boolean isModification(final DataTreeCandidateNode candidateNode) {
            if (candidateNode.getDataBefore().isPresent()) {
                if (candidateNode.getDataAfter().isPresent()) {
                    return !candidateNode.getDataAfter().get().equals(candidateNode.getDataBefore().get());
                } else {
                    return true;
                }
            }

            // considering not a modification if data after is also null
            return candidateNode.getDataAfter().isPresent();
        }

        /**
         * Check whether candidate node is for a leaf type node.
         */
        private static boolean isLeaf(final DataTreeCandidateNode candidateNode) {
            // orNull intentional, some candidate nodes have both data after and data before null
            return candidateNode.getDataAfter().orNull() instanceof LeafNode<?>
                    || candidateNode.getDataBefore().orNull() instanceof LeafNode<?>;
        }

        @Override
        public String toString() {
            return "ModificationDiff{"
                    + "modificationsBefore=" + modificationsBefore
                    + ", modificationsAfter=" + modificationsAfter
                    + '}';
        }
    }
}