9093c080973374c7e9c1959f13ff3065b6d71af3
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractProxyHistory.java
1 /*
2  * Copyright (c) 2016 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.databroker.actors.dds;
9
10 import akka.actor.ActorRef;
11 import com.google.common.base.Preconditions;
12 import java.util.Optional;
13 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
17
18 /**
19  * Per-connection representation of a local history.
20  *
21  * @author Robert Varga
22  */
23 abstract class AbstractProxyHistory implements Identifiable<LocalHistoryIdentifier> {
24     // FIXME: this should really be ClientConnection
25     private final DistributedDataStoreClientBehavior client;
26     private final LocalHistoryIdentifier identifier;
27
28     AbstractProxyHistory(final DistributedDataStoreClientBehavior client, final LocalHistoryIdentifier identifier) {
29         this.client = Preconditions.checkNotNull(client);
30         this.identifier = Preconditions.checkNotNull(identifier);
31     }
32
33     static AbstractProxyHistory create(final DistributedDataStoreClientBehavior client,
34             final Optional<ShardBackendInfo> backendInfo, final LocalHistoryIdentifier identifier) {
35         final Optional<DataTree> dataTree = backendInfo.flatMap(ShardBackendInfo::getDataTree);
36         return dataTree.isPresent() ? new LocalProxyHistory(client, identifier, dataTree.get()) : new RemoteProxyHistory(client, identifier);
37     }
38
39     @Override
40     public LocalHistoryIdentifier getIdentifier() {
41         return identifier;
42     }
43
44     final ActorRef localActor() {
45         return client.self();
46     }
47
48     final AbstractProxyTransaction createTransactionProxy(final TransactionIdentifier txId) {
49         return doCreateTransactionProxy(client, new TransactionIdentifier(identifier, txId.getTransactionId()));
50     }
51
52     abstract AbstractProxyTransaction doCreateTransactionProxy(DistributedDataStoreClientBehavior client,
53             TransactionIdentifier txId);
54 }