6b901b4fa85988cc14ce4557d865a09d4a28d034
[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>,
19         Identifiable<LocalHistoryIdentifier> {
20     private final LocalHistoryIdentifier identifier;
21
22     private long nextTransaction;
23     private boolean closed;
24
25     FrontendHistoryMetadataBuilder(final LocalHistoryIdentifier identifier) {
26         this.identifier = Preconditions.checkNotNull(identifier);
27     }
28
29     FrontendHistoryMetadataBuilder(final ClientIdentifier clientId, final FrontendHistoryMetadata meta) {
30         identifier = new LocalHistoryIdentifier(clientId, meta.getHistoryId(), meta.getCookie());
31         nextTransaction = meta.getNextTransaction();
32         closed = meta.isClosed();
33     }
34
35     @Override
36     public LocalHistoryIdentifier getIdentifier() {
37         return identifier;
38     }
39
40     @Override
41     public FrontendHistoryMetadata build() {
42         return new FrontendHistoryMetadata(identifier.getHistoryId(), identifier.getCookie(), nextTransaction, closed);
43     }
44
45     void onHistoryClosed() {
46         closed = true;
47     }
48
49     void onTransactionCommitted(final TransactionIdentifier txId) {
50         nextTransaction = txId.getTransactionId() + 1;
51     }
52 }