d8d7bdda217b9dd2c1144b4acfd901b2435acd79
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendHistoryMetadataBuilder.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.datastore;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
12 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
13 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
15 import org.opendaylight.yangtools.concepts.Builder;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17
18 final class FrontendHistoryMetadataBuilder implements Builder<FrontendHistoryMetadata>, Identifiable<LocalHistoryIdentifier> {
19     private final LocalHistoryIdentifier identifier;
20
21     private long nextTransaction;
22     private boolean closed;
23
24     FrontendHistoryMetadataBuilder(final LocalHistoryIdentifier identifier) {
25         this.identifier = Preconditions.checkNotNull(identifier);
26     }
27
28     FrontendHistoryMetadataBuilder(final ClientIdentifier clientId, final FrontendHistoryMetadata meta) {
29         identifier = new LocalHistoryIdentifier(clientId, meta.getHistoryId(), meta.getCookie());
30         nextTransaction = meta.getNextTransaction();
31         closed = meta.isClosed();
32     }
33
34     @Override
35     public LocalHistoryIdentifier getIdentifier() {
36         return identifier;
37     }
38
39     @Override
40     public FrontendHistoryMetadata build() {
41         return new FrontendHistoryMetadata(identifier.getHistoryId(), identifier.getCookie(), nextTransaction, closed);
42     }
43
44     void onHistoryClosed() {
45         closed = true;
46     }
47
48     void onTransactionCommitted(final TransactionIdentifier txId) {
49         nextTransaction = txId.getTransactionId() + 1;
50     }
51 }