Add optional lz4 compression for snapshots
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / actors / ShardSnapshotActorTest.java
1 /*
2  * Copyright (c) 2016, 2017 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 package org.opendaylight.controller.cluster.datastore.actors;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import akka.actor.ActorRef;
15 import akka.testkit.javadsl.TestKit;
16 import com.google.common.io.ByteSource;
17 import java.io.ByteArrayOutputStream;
18 import java.io.ObjectInputStream;
19 import java.time.Duration;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
23 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
24 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
25 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
26 import org.opendaylight.controller.cluster.io.InputOutputStreamFactory;
27 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
28 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31
32 public class ShardSnapshotActorTest extends AbstractActorTest {
33     private static final InputOutputStreamFactory STREAM_FACTORY = InputOutputStreamFactory.simple();
34
35     private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
36
37     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
38             final boolean withInstallSnapshot) throws Exception {
39         final TestKit kit = new TestKit(getSystem());
40         final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(STREAM_FACTORY), testName);
41         kit.watch(snapshotActor);
42
43         final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
44
45         ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
46         ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
47             Optional.ofNullable(installSnapshotStream), kit.getRef());
48
49         final CaptureSnapshotReply reply = kit.expectMsgClass(Duration.ofSeconds(3), CaptureSnapshotReply.class);
50         assertNotNull("getSnapshotState is null", reply.getSnapshotState());
51         assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
52         assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
53
54         if (installSnapshotStream != null) {
55             final ShardDataTreeSnapshot deserialized;
56             try (ObjectInputStream in = new ObjectInputStream(STREAM_FACTORY.createInputStream(
57                     ByteSource.wrap(installSnapshotStream.toByteArray())))) {
58                 deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
59             }
60
61             assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
62
63             final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
64             assertTrue("isPresent", maybeNode.isPresent());
65             assertEquals("Root node", expectedRoot, maybeNode.get());
66         }
67     }
68
69     @Test
70     public void testSerializeBoronSnapshot() throws Exception {
71         testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
72                 new MetadataShardDataTreeSnapshot(DATA), true);
73         testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
74                 new MetadataShardDataTreeSnapshot(DATA), false);
75     }
76 }