Search in sources :

Example 1 with ShardDataTreeSnapshotMetadata

use of org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshotMetadata in project controller by opendaylight.

the class ShardDataTree method applySnapshot.

private void applySnapshot(@Nonnull final ShardDataTreeSnapshot snapshot, final UnaryOperator<DataTreeModification> wrapper) throws DataValidationFailedException {
    final Stopwatch elapsed = Stopwatch.createStarted();
    if (anyPendingTransactions()) {
        LOG.warn("{}: applying state snapshot with pending transactions", logContext);
    }
    final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> snapshotMeta;
    if (snapshot instanceof MetadataShardDataTreeSnapshot) {
        snapshotMeta = ((MetadataShardDataTreeSnapshot) snapshot).getMetadata();
    } else {
        snapshotMeta = ImmutableMap.of();
    }
    for (ShardDataTreeMetadata<?> m : metadata) {
        final ShardDataTreeSnapshotMetadata<?> s = snapshotMeta.get(m.getSupportedType());
        if (s != null) {
            m.applySnapshot(s);
        } else {
            m.reset();
        }
    }
    final DataTreeModification mod = wrapper.apply(dataTree.takeSnapshot().newModification());
    // delete everything first
    mod.delete(YangInstanceIdentifier.EMPTY);
    final java.util.Optional<NormalizedNode<?, ?>> maybeNode = snapshot.getRootNode();
    if (maybeNode.isPresent()) {
        // Add everything from the remote node back
        mod.write(YangInstanceIdentifier.EMPTY, maybeNode.get());
    }
    mod.ready();
    final DataTreeModification unwrapped = unwrap(mod);
    dataTree.validate(unwrapped);
    DataTreeCandidateTip candidate = dataTree.prepare(unwrapped);
    dataTree.commit(candidate);
    notifyListeners(candidate);
    LOG.debug("{}: state snapshot applied in {}", logContext, elapsed);
}
Also used : DataTreeModification(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification) PruningDataTreeModification(org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification) MetadataShardDataTreeSnapshot(org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot) Stopwatch(com.google.common.base.Stopwatch) DataTreeCandidateTip(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ShardDataTreeSnapshotMetadata(org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshotMetadata)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 MetadataShardDataTreeSnapshot (org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot)1 ShardDataTreeSnapshotMetadata (org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshotMetadata)1 PruningDataTreeModification (org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification)1 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)1 DataTreeCandidateTip (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip)1 DataTreeModification (org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification)1