Fix warnings and clean up javadocs in sal-akka-raft
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / SnapshotTracker.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.raft.behaviors;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.protobuf.ByteString;
14 import java.util.Arrays;
15 import org.slf4j.Logger;
16
17 /**
18  * Helper class that maintains state for a snapshot that is being installed in chunks on a Follower.
19  */
20 public class SnapshotTracker {
21     private final Logger log;
22     private final int totalChunks;
23     private final String leaderId;
24     private ByteString collectedChunks = ByteString.EMPTY;
25     private int lastChunkIndex = LeaderInstallSnapshotState.FIRST_CHUNK_INDEX - 1;
26     private boolean sealed = false;
27     private int lastChunkHashCode = LeaderInstallSnapshotState.INITIAL_LAST_CHUNK_HASH_CODE;
28
29     SnapshotTracker(Logger log, int totalChunks, String leaderId) {
30         this.log = log;
31         this.totalChunks = totalChunks;
32         this.leaderId = Preconditions.checkNotNull(leaderId);
33     }
34
35     /**
36      * Adds a chunk to the tracker.
37      *
38      * @param chunkIndex the index of the chunk
39      * @param chunk the chunk data
40      * @param lastChunkHashCode the optional hash code for the chunk
41      * @return true if this is the last chunk is received
42      * @throws InvalidChunkException if the chunk index is invalid or out of order
43      */
44     boolean addChunk(int chunkIndex, byte[] chunk, Optional<Integer> maybeLastChunkHashCode)
45             throws InvalidChunkException {
46         log.debug("addChunk: chunkIndex={}, lastChunkIndex={}, collectedChunks.size={}, lastChunkHashCode={}",
47                 chunkIndex, lastChunkIndex, collectedChunks.size(), this.lastChunkHashCode);
48
49         if (sealed) {
50             throw new InvalidChunkException("Invalid chunk received with chunkIndex " + chunkIndex
51                     + " all chunks already received");
52         }
53
54         if (lastChunkIndex + 1 != chunkIndex) {
55             throw new InvalidChunkException("Expected chunkIndex " + (lastChunkIndex + 1) + " got " + chunkIndex);
56         }
57
58         if (maybeLastChunkHashCode.isPresent() && maybeLastChunkHashCode.get() != this.lastChunkHashCode) {
59             throw new InvalidChunkException("The hash code of the recorded last chunk does not match "
60                     + "the senders hash code, expected " + this.lastChunkHashCode + " was "
61                     + maybeLastChunkHashCode.get());
62         }
63
64         sealed = chunkIndex == totalChunks;
65         lastChunkIndex = chunkIndex;
66         collectedChunks = collectedChunks.concat(ByteString.copyFrom(chunk));
67         this.lastChunkHashCode = Arrays.hashCode(chunk);
68         return sealed;
69     }
70
71     byte[] getSnapshot() {
72         if (!sealed) {
73             throw new IllegalStateException("lastChunk not received yet");
74         }
75
76         return collectedChunks.toByteArray();
77     }
78
79     ByteString getCollectedChunks() {
80         return collectedChunks;
81     }
82
83     String getLeaderId() {
84         return leaderId;
85     }
86
87     public static class InvalidChunkException extends Exception {
88         private static final long serialVersionUID = 1L;
89
90         InvalidChunkException(String message) {
91             super(message);
92         }
93     }
94 }