summaryrefslogtreecommitdiffstats
path: root/infra/data-impl/src/main/java/io/fd/honeycomb/data/impl/ModifiableDataTreeDelegator.java
blob: b9246f3c443b45763e0717ae69afbc1c45cfe44a (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (c) 2016, 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.honeycomb.data.impl;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.Futures.immediateCheckedFuture;
import static io.fd.honeycomb.data.impl.ModifiableDataTreeDelegator.DataTreeWriteContextFactory.DataTreeWriteContext;
import static io.fd.honeycomb.data.impl.ModifiableDataTreeManager.DataTreeContextFactory.DataTreeContext;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.CheckedFuture;
import io.fd.honeycomb.data.DataModification;
import io.fd.honeycomb.data.ReadableDataManager;
import io.fd.honeycomb.translate.MappingContext;
import io.fd.honeycomb.translate.TranslationException;
import io.fd.honeycomb.translate.ValidationFailedException;
import io.fd.honeycomb.translate.util.RWUtils;
import io.fd.honeycomb.translate.util.TransactionMappingContext;
import io.fd.honeycomb.translate.util.write.TransactionWriteContext;
import io.fd.honeycomb.translate.write.DataObjectUpdate;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.registry.UpdateFailedException;
import io.fd.honeycomb.translate.write.registry.WriterRegistry;
import io.fd.honeycomb.translate.write.registry.WriterRegistry.DataObjectUpdates;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
import org.opendaylight.mdsal.binding.dom.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.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.model.api.SchemaContext;
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 DataBroker contextBroker;
    private final BindingNormalizedNodeSerializer serializer;
    private final SchemaContext schema;

    /**
     * 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 SchemaContext schema,
                                       @Nonnull final WriterRegistry writerRegistry,
                                       @Nonnull final DataBroker contextBroker) {
        super(dataTree, new DataTreeWriteContextFactory());
        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");
        this.schema = checkNotNull(schema, "schema should not be null");
    }

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

    private final class DelegatingConfigSnapshot 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.
         */
        DelegatingConfigSnapshot(final DataModification untouchedModification) {
            this.untouchedModification = untouchedModification;
        }

        @Override
        protected void validateCandidate(final DataTreeContext dataTreeContext) throws ValidationFailedException {
            final DataObjectUpdates baUpdates =
                getUpdates((DataTreeWriteContextFactory.DataTreeWriteContext) dataTreeContext);
            LOG.debug("ModifiableDataTreeDelegator.validateCandidate() extracted updates={}", baUpdates);

            try (final WriteContext ctx = getTransactionWriteContext()) {
                writerRegistry.validateModifications(baUpdates, ctx);
            }
        }

        private DataObjectUpdates getUpdates(final DataTreeWriteContext dataTreeContext) {
            DataObjectUpdates updates = dataTreeContext.getUpdates();
            if (updates != null) {
                return updates;
            } else {
                final DataTreeCandidate candidate = dataTreeContext.getCandidate();
                final DataTreeCandidateNode rootNode = candidate.getRootNode();
                final YangInstanceIdentifier rootPath = candidate.getRootPath();
                LOG.trace("ConfigDataTree.getUpdates() rootPath={}, rootNode={}, dataBefore={}, dataAfter={}",
                    rootPath, rootNode, rootNode.getDataBefore(), rootNode.getDataAfter());

                final ModificationDiff modificationDiff = new ModificationDiff.ModificationDiffBuilder()
                    .setCtx(schema).build(rootNode);
                LOG.debug("ConfigDataTree.getUpdates() diff: {}", modificationDiff);

                // Distinguish between updates (create + update) and deletes
                updates = toBindingAware(writerRegistry, modificationDiff.getUpdates());
                dataTreeContext.setUpdates(updates);
                return updates;
            }
        }

        /**
         * 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 DataTreeContext dataTreeContext) throws TranslationException {
            final DataTreeWriteContextFactory.DataTreeWriteContext dataTreeWriteContext =
                (DataTreeWriteContextFactory.DataTreeWriteContext) dataTreeContext;

            final DataObjectUpdates baUpdates = dataTreeWriteContext.getUpdates();
            LOG.debug("ModifiableDataTreeDelegator.processCandidate() extracted updates={}", baUpdates);

            final WriteContext ctx = getTransactionWriteContext();
            final MappingContext mappingContext = ctx.getMappingContext();
            try {
                writerRegistry.processModifications(baUpdates, ctx);

                final CheckedFuture<Void, TransactionCommitFailedException> contextUpdateResult =
                    ((TransactionMappingContext) mappingContext).submit();
                // Blocking on context data update
                contextUpdateResult.checkedGet();
            } catch (UpdateFailedException e) {
                // TODO - HONEYCOMB-411
                LOG.warn("Failed to apply all changes", e);
                final List<DataObjectUpdate> processed = e.getProcessed();
                if (processed.isEmpty()) {
                    // nothing was processed, which means either very first operation failed, or it was single operation
                    // update. In both cases, no revert is needed
                    LOG.info("Nothing to revert");
                    throw e;
                } else {
                    LOG.info("Trying to revert successful changes for current transaction");
                    // attempt revert with fresh context, to allow write logic used context-binded data
                    try (final TransactionWriteContext txContext = getRevertTransactionContext(mappingContext)) {
                        new Reverter(processed, writerRegistry).revert(txContext);
                        LOG.info("Changes successfully reverted");
                    } catch (Reverter.RevertFailedException revertFailedException) {
                        // fail with failed revert
                        LOG.error("Failed to revert successful(comitted) changes", revertFailedException);
                        throw revertFailedException;
                    }
                }
                // fail with success revert
                // not passing the cause,its logged above and it would be logged after transaction
                // ended again(prevent double logging of same error
                throw new Reverter.RevertSuccessException(getNonProcessedNodes(baUpdates, processed));
            } catch (TransactionCommitFailedException e) {
                // TODO HONEYCOMB-162 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);
            } finally {
                // Using finally instead of try-with-resources in order to leave ctx open for BulkUpdateException catch
                // block. The context is needed there, but try-with-resources closes the resource before handling ex.
                LOG.debug("Closing write context {}", ctx);
                ctx.close();
            }
        }

        /**
         * Creates inverted transaction context for reverting of proceeded changes.
         * Invert before/after transaction and reuse affected mapping context created by previous updates
         * to access all data created by previous updates
         * */
        // Sonar reports unclosed resources, but everything is closed by TransactionWriteContext.close()
        // This is known SonarJava issue
        // https://jira.sonarsource.com/browse/SONARJAVA-1670
        // Fixed in SonarJava 4.0 (requires SonarCube 5.6).
        // TODO(HONEYCOMB-419): remove after LF upgrades SonarCube
        @SuppressWarnings("squid:S2095")
        private TransactionWriteContext getRevertTransactionContext(final MappingContext affectedMappingContext) {
            // Before Tx == after partial update
            final DOMDataReadOnlyTransaction beforeTx = ReadOnlyTransaction.create(this, EMPTY_OPERATIONAL);
            // After Tx == before partial update
            final DOMDataReadOnlyTransaction afterTx = ReadOnlyTransaction.create(untouchedModification, EMPTY_OPERATIONAL);
            return new TransactionWriteContext(serializer, beforeTx, afterTx, affectedMappingContext);
        }

        // Sonar reports unclosed resources, but everything is closed by TransactionWriteContext.close()
        // TODO(HONEYCOMB-419): remove after LF upgrades SonarCube
        @SuppressWarnings("squid:S2095")
        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 WriterRegistry.DataObjectUpdates toBindingAware(final WriterRegistry registry,
                final Map<YangInstanceIdentifier, NormalizedNodeUpdate> biNodes) {
            return ModifiableDataTreeDelegator.toBindingAware(registry, biNodes, serializer);
        }
    }

    private static Set<InstanceIdentifier<?>> getNonProcessedNodes(final WriterRegistry.DataObjectUpdates allUpdates,
                                                                   final List<DataObjectUpdate> alreadyProcessed) {
        return allUpdates.getAllModifications().stream()
                .filter(update -> !alreadyProcessed.contains(update))
                .map(DataObjectUpdate::getId)
                .collect(Collectors.toSet());
    }

    @VisibleForTesting
    static WriterRegistry.DataObjectUpdates toBindingAware(
            final WriterRegistry registry,
            final Map<YangInstanceIdentifier, NormalizedNodeUpdate> biNodes,
            final BindingNormalizedNodeSerializer serializer) {

        final Multimap<InstanceIdentifier<?>, DataObjectUpdate> dataObjectUpdates = HashMultimap.create();
        final Multimap<InstanceIdentifier<?>, DataObjectUpdate.DataObjectDelete> dataObjectDeletes =
                HashMultimap.create();

        for (Map.Entry<YangInstanceIdentifier, NormalizedNodeUpdate> biEntry : biNodes.entrySet()) {
            final InstanceIdentifier<?> keyedId = serializer.fromYangInstanceIdentifier(biEntry.getKey());
            final InstanceIdentifier<?> unkeyedIid =
                    RWUtils.makeIidWildcarded(keyedId);

            NormalizedNodeUpdate normalizedNodeUpdate = biEntry.getValue();
            final DataObjectUpdate dataObjectUpdate = toDataObjectUpdate(normalizedNodeUpdate, serializer);
            if (dataObjectUpdate != null) {
                if (dataObjectUpdate instanceof DataObjectUpdate.DataObjectDelete) {
                    // is delete
                    dataObjectDeletes.put(unkeyedIid, (DataObjectUpdate.DataObjectDelete) dataObjectUpdate);
                } else if (dataObjectUpdate.getDataBefore() != null && !registry.writerSupportsUpdate(unkeyedIid)) {
                    // is update and direct update operation is not supported
                    // breaks update to delete + create pair

                    dataObjectDeletes.put(unkeyedIid,
                            (DataObjectUpdate.DataObjectDelete) DataObjectUpdate.DataObjectDelete
                                    .create(keyedId, dataObjectUpdate.getDataBefore(), null));
                    dataObjectUpdates
                            .put(unkeyedIid, DataObjectUpdate.create(keyedId, null, dataObjectUpdate.getDataAfter()));
                } else {
                    // is create
                    dataObjectUpdates.put(unkeyedIid, dataObjectUpdate);
                }
            }
        }
        return new WriterRegistry.DataObjectUpdates(dataObjectUpdates, dataObjectDeletes);
    }

    @Nullable
    private static DataObjectUpdate toDataObjectUpdate(
            final NormalizedNodeUpdate normalizedNodeUpdate,
            final BindingNormalizedNodeSerializer serializer) {

        InstanceIdentifier<?> baId = serializer.fromYangInstanceIdentifier(normalizedNodeUpdate.getId());
        checkNotNull(baId, "Unable to transform instance identifier: %s into BA", normalizedNodeUpdate.getId());

        DataObject dataObjectBefore = getDataObject(serializer,
                normalizedNodeUpdate.getDataBefore(), normalizedNodeUpdate.getId());
        DataObject dataObjectAfter =
                getDataObject(serializer, normalizedNodeUpdate.getDataAfter(), normalizedNodeUpdate.getId());

        return dataObjectBefore == null && dataObjectAfter == null
                ? null
                : DataObjectUpdate.create(baId, dataObjectBefore, dataObjectAfter);
    }

    @Nullable
    private static DataObject getDataObject(@Nonnull final BindingNormalizedNodeSerializer serializer,
                                            @Nullable final NormalizedNode<?, ?> data,
                                            @Nonnull final YangInstanceIdentifier id) {
        DataObject dataObject = null;
        if (data != null) {
            final Map.Entry<InstanceIdentifier<?>, DataObject> dataObjectEntry =
                    serializer.fromNormalizedNode(id, data);
            if (dataObjectEntry != null) {
                dataObject = dataObjectEntry.getValue();
            }
        }
        return dataObject;
    }

    static final class DataTreeWriteContextFactory implements ModifiableDataTreeManager.DataTreeContextFactory {
        @Nonnull
        @Override
        public DataTreeWriteContext create(@Nonnull final DataTreeCandidate candidate) {
            return new DataTreeWriteContext(candidate);
        }

        /**
         * Implementation of {@link DataTreeContext} that, in addition to {@link DataTreeCandidate}, stores also
         * {@DataObjectUpdates}. The {@link DataObjectUpdates} are shared by the validation and translation process.
         */
        static final class DataTreeWriteContext implements DataTreeContext {
            private final DataTreeCandidate candidate;
            private DataObjectUpdates updates;

            DataTreeWriteContext(@Nonnull final DataTreeCandidate candidate) {
                this.candidate = candidate;
            }

            @Nonnull
            @Override
            public DataTreeCandidate getCandidate() {
                return candidate;
            }

            public void setUpdates(@Nonnull final DataObjectUpdates updates) {
                this.updates = updates;
            }

            DataObjectUpdates getUpdates() {
                return updates;
            }
        }
    }
}