Bump yangtools to 7.0.2
[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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.Range;
14 import com.google.common.collect.RangeSet;
15 import com.google.common.collect.TreeRangeSet;
16 import com.google.common.primitives.UnsignedLong;
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
23 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
24 import org.opendaylight.yangtools.concepts.Builder;
25 import org.opendaylight.yangtools.concepts.Identifiable;
26
27 final class FrontendHistoryMetadataBuilder implements Builder<FrontendHistoryMetadata>,
28         Identifiable<LocalHistoryIdentifier> {
29
30     private final Map<UnsignedLong, Boolean> closedTransactions;
31     private final RangeSet<UnsignedLong> purgedTransactions;
32     private final LocalHistoryIdentifier identifier;
33
34     private boolean closed;
35
36     FrontendHistoryMetadataBuilder(final LocalHistoryIdentifier identifier) {
37         this.identifier = requireNonNull(identifier);
38         this.purgedTransactions = TreeRangeSet.create();
39         this.closedTransactions = new HashMap<>(2);
40     }
41
42     FrontendHistoryMetadataBuilder(final ClientIdentifier clientId, final FrontendHistoryMetadata meta) {
43         identifier = new LocalHistoryIdentifier(clientId, meta.getHistoryId(), meta.getCookie());
44         closedTransactions = new HashMap<>(meta.getClosedTransactions());
45         purgedTransactions = TreeRangeSet.create(meta.getPurgedTransactions());
46         closed = meta.isClosed();
47     }
48
49     @Override
50     public LocalHistoryIdentifier getIdentifier() {
51         return identifier;
52     }
53
54     @Override
55     public FrontendHistoryMetadata build() {
56         return new FrontendHistoryMetadata(identifier.getHistoryId(), identifier.getCookie(), closed,
57             closedTransactions, purgedTransactions);
58     }
59
60     void onHistoryClosed() {
61         checkState(identifier.getHistoryId() != 0);
62         closed = true;
63     }
64
65     void onTransactionAborted(final TransactionIdentifier txId) {
66         closedTransactions.put(UnsignedLong.fromLongBits(txId.getTransactionId()), Boolean.FALSE);
67     }
68
69     void onTransactionCommitted(final TransactionIdentifier txId) {
70         closedTransactions.put(UnsignedLong.fromLongBits(txId.getTransactionId()), Boolean.TRUE);
71     }
72
73     void onTransactionPurged(final TransactionIdentifier txId) {
74         final UnsignedLong id = UnsignedLong.fromLongBits(txId.getTransactionId());
75         closedTransactions.remove(id);
76         purgedTransactions.add(Range.closedOpen(id, UnsignedLong.ONE.plus(id)));
77     }
78
79     /**
80      * Transform frontend metadata for a particular client history into its {@link LocalFrontendHistory} counterpart.
81      *
82      * @param shard parent shard
83      * @return Leader history state
84      */
85     @NonNull AbstractFrontendHistory toLeaderState(final @NonNull Shard shard) {
86         if (identifier.getHistoryId() == 0) {
87             return StandaloneFrontendHistory.recreate(shard.persistenceId(), identifier.getClientId(),
88                 shard.getDataStore(), closedTransactions, purgedTransactions);
89         }
90
91         return LocalFrontendHistory.recreate(shard.persistenceId(), shard.getDataStore(),
92             shard.getDataStore().recreateTransactionChain(identifier, closed), closedTransactions, purgedTransactions);
93     }
94 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.