Use YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardSnapshotCohort.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorRef;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
15 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
16 import org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20
21 /**
22  * Participates in raft snapshotting on behalf of a Shard actor.
23  *
24  * @author Thomas Pantelis
25  */
26 class ShardSnapshotCohort implements RaftActorSnapshotCohort {
27     private final ShardTransactionActorFactory transactionActorFactory;
28     private final ShardDataTree store;
29     private final String logId;
30     private final Logger log;
31
32     private int createSnapshotTransactionCounter;
33
34     ShardSnapshotCohort(ShardTransactionActorFactory transactionActorFactory, ShardDataTree store,
35             Logger log, String logId) {
36         this.transactionActorFactory = transactionActorFactory;
37         this.store = Preconditions.checkNotNull(store);
38         this.log = log;
39         this.logId = logId;
40     }
41
42     @Override
43     public void createSnapshot(ActorRef actorRef) {
44         // Create a transaction actor. We are really going to treat the transaction as a worker
45         // so that this actor does not get block building the snapshot. THe transaction actor will
46         // after processing the CreateSnapshot message.
47
48         ShardTransactionIdentifier transactionID = new ShardTransactionIdentifier(
49                 "createSnapshot" + ++createSnapshotTransactionCounter);
50
51         ActorRef createSnapshotTransaction = transactionActorFactory.newShardTransaction(
52                 TransactionType.READ_ONLY, transactionID, "");
53
54         createSnapshotTransaction.tell(CreateSnapshot.INSTANCE, actorRef);
55     }
56
57     @Override
58     public void applySnapshot(byte[] snapshotBytes) {
59         // Since this will be done only on Recovery or when this actor is a Follower
60         // we can safely commit everything in here. We not need to worry about event notifications
61         // as they would have already been disabled on the follower
62
63         log.info("{}: Applying snapshot", logId);
64
65         try {
66             ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction("snapshot-" + logId, null);
67
68             NormalizedNode<?, ?> node = SerializationUtils.deserializeNormalizedNode(snapshotBytes);
69
70             // delete everything first
71             transaction.getSnapshot().delete(YangInstanceIdentifier.EMPTY);
72
73             // Add everything from the remote node back
74             transaction.getSnapshot().write(YangInstanceIdentifier.EMPTY, node);
75             syncCommitTransaction(transaction);
76         } catch (InterruptedException | ExecutionException e) {
77             log.error("{}: An exception occurred when applying snapshot", logId, e);
78         } finally {
79             log.info("{}: Done applying snapshot", logId);
80         }
81
82     }
83
84     void syncCommitTransaction(final ReadWriteShardDataTreeTransaction transaction)
85             throws ExecutionException, InterruptedException {
86         ShardDataTreeCohort commitCohort = store.finishTransaction(transaction);
87         commitCohort.preCommit().get();
88         commitCohort.commit().get();
89     }
90 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.