2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.client;
10 import akka.actor.ActorRef;
11 import akka.dispatch.OnComplete;
12 import akka.pattern.Patterns;
13 import akka.persistence.SelectedSnapshot;
14 import akka.persistence.SnapshotMetadata;
15 import akka.persistence.SnapshotSelectionCriteria;
16 import akka.persistence.snapshot.japi.SnapshotStore;
17 import com.google.common.base.Preconditions;
18 import java.util.Optional;
19 import scala.concurrent.Future;
20 import scala.concurrent.Promise;
23 * Instantiated by akka. MockedSnapshotStore forwards method calls as
24 * {@link MockedSnapshotStoreMessage} messages to delegate actor. Delegate reference
25 * must be sent as a message to this snapshot store.
27 class MockedSnapshotStore extends SnapshotStore {
29 private static final long TIMEOUT = 1000;
31 private ActorRef delegate;
34 * Marker interface for messages produced by MockedSnapshotStore.
36 interface MockedSnapshotStoreMessage {
40 public Future<Optional<SelectedSnapshot>> doLoadAsync(final String persistenceId,
41 final SnapshotSelectionCriteria criteria) {
42 return askDelegate(new LoadRequest(persistenceId, criteria));
46 public Future<Void> doSaveAsync(final SnapshotMetadata metadata, final Object snapshot) {
47 return askDelegate(new SaveRequest(metadata, snapshot));
51 public Future<Void> doDeleteAsync(final SnapshotMetadata metadata) {
52 return askDelegate(new DeleteByMetadataRequest(metadata));
56 public Future<Void> doDeleteAsync(final String persistenceId, final SnapshotSelectionCriteria criteria) {
57 return askDelegate(new DeleteByCriteriaRequest(persistenceId, criteria));
61 public void unhandled(final Object message) {
62 if (message instanceof ActorRef) {
63 delegate = (ActorRef) message;
66 super.unhandled(message);
69 private <T> Future<T> askDelegate(final MockedSnapshotStoreMessage message) {
70 Preconditions.checkNotNull(delegate, "Delegate ref wasn't sent");
71 final Future<Object> ask = Patterns.ask(delegate, message, TIMEOUT);
72 return transform(ask);
75 private <T> Future<T> transform(final Future<Object> future) {
76 final Promise<T> promise = new scala.concurrent.impl.Promise.DefaultPromise<>();
77 future.onComplete(new OnComplete<Object>() {
79 public void onComplete(final Throwable failure, final Object success) {
80 if (success instanceof Throwable) {
81 promise.failure((Throwable) success);
84 if (success == Void.TYPE) {
85 promise.success(null);
88 promise.success((T) success);
90 }, context().dispatcher());
91 return promise.future();
94 class LoadRequest implements MockedSnapshotStoreMessage {
95 private final String persistenceId;
96 private final SnapshotSelectionCriteria criteria;
98 LoadRequest(final String persistenceId, final SnapshotSelectionCriteria criteria) {
99 this.persistenceId = persistenceId;
100 this.criteria = criteria;
103 public String getPersistenceId() {
104 return persistenceId;
107 public SnapshotSelectionCriteria getCriteria() {
112 class DeleteByCriteriaRequest implements MockedSnapshotStoreMessage {
113 private final String persistenceId;
114 private final SnapshotSelectionCriteria criteria;
116 DeleteByCriteriaRequest(final String persistenceId, final SnapshotSelectionCriteria criteria) {
117 this.persistenceId = persistenceId;
118 this.criteria = criteria;
121 public String getPersistenceId() {
122 return persistenceId;
125 public SnapshotSelectionCriteria getCriteria() {
130 class DeleteByMetadataRequest implements MockedSnapshotStoreMessage {
131 private final SnapshotMetadata metadata;
133 DeleteByMetadataRequest(final SnapshotMetadata metadata) {
134 this.metadata = metadata;
137 public SnapshotMetadata getMetadata() {
142 class SaveRequest implements MockedSnapshotStoreMessage {
143 private final SnapshotMetadata metadata;
144 private final Object snapshot;
146 SaveRequest(final SnapshotMetadata metadata, final Object snapshot) {
147 this.metadata = metadata;
148 this.snapshot = snapshot;
151 public SnapshotMetadata getMetadata() {
155 public Object getSnapshot() {