0958aade7156a304dbbb41fbac9755b2f8d79c34
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ShardBackendInfo.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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.primitives.UnsignedLong;
16 import java.util.Optional;
17 import org.opendaylight.controller.cluster.access.ABIVersion;
18 import org.opendaylight.controller.cluster.access.client.BackendInfo;
19 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ReadOnlyDataTree;
21
22 /**
23  * Combined backend tracking. Aside from usual {@link BackendInfo}, this object also tracks the cookie assigned
24  * to the shard. This assignment remains constant for as long as the client is not restarted. This class is thread-safe.
25  *
26  * @author Robert Varga
27  */
28 final class ShardBackendInfo extends BackendInfo {
29     private final Optional<ReadOnlyDataTree> dataTree;
30     private final UnsignedLong cookie;
31
32     ShardBackendInfo(final ActorRef actor, final long sessionId, final ABIVersion version, final String shardName,
33         final UnsignedLong cookie, final Optional<ReadOnlyDataTree> dataTree, final int maxMessages) {
34         super(actor, shardName, sessionId, version, maxMessages);
35         this.cookie = requireNonNull(cookie);
36         this.dataTree = requireNonNull(dataTree);
37     }
38
39     UnsignedLong getCookie() {
40         return cookie;
41     }
42
43     Optional<ReadOnlyDataTree> getDataTree() {
44         return dataTree;
45     }
46
47     LocalHistoryIdentifier brandHistory(final LocalHistoryIdentifier id) {
48         checkArgument(id.getCookie() == 0, "History %s is already branded", id);
49         return new LocalHistoryIdentifier(id.getClientId(), id.getHistoryId(), cookie.longValue());
50     }
51
52     @Override
53     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
54         return super.addToStringAttributes(toStringHelper).add("cookie", cookie).add("shard", getName())
55                 .add("dataTree", getDataTree().isPresent() ? "present" : "absent");
56     }
57 }