BUG-5280: add FrontendMetadata
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendClientMetadataBuilder.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 com.google.common.collect.Collections2;
12 import com.google.common.collect.Range;
13 import com.google.common.collect.RangeSet;
14 import com.google.common.collect.TreeRangeSet;
15 import com.google.common.primitives.UnsignedLong;
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
22 import org.opendaylight.controller.cluster.datastore.persisted.FrontendHistoryMetadata;
23 import org.opendaylight.yangtools.concepts.Builder;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25
26 final class FrontendClientMetadataBuilder implements Builder<FrontendClientMetadata>, Identifiable<ClientIdentifier> {
27     private final Map<LocalHistoryIdentifier, FrontendHistoryMetadataBuilder> currentHistories = new HashMap<>();
28     private final RangeSet<UnsignedLong> purgedHistories;
29     private final ClientIdentifier identifier;
30
31     FrontendClientMetadataBuilder(final ClientIdentifier identifier) {
32         this.identifier = Preconditions.checkNotNull(identifier);
33         purgedHistories = TreeRangeSet.create();
34     }
35
36     FrontendClientMetadataBuilder(final FrontendClientMetadata meta) {
37         this.identifier = Preconditions.checkNotNull(meta.getIdentifier());
38         purgedHistories = TreeRangeSet.create(meta.getPurgedHistories());
39
40         for (FrontendHistoryMetadata h : meta.getCurrentHistories()) {
41             final FrontendHistoryMetadataBuilder b = new FrontendHistoryMetadataBuilder(identifier, h);
42             currentHistories.put(b.getIdentifier(), b);
43         }
44     }
45
46     @Override
47     public FrontendClientMetadata build() {
48         return new FrontendClientMetadata(identifier, purgedHistories,
49             Collections2.transform(currentHistories.values(), FrontendHistoryMetadataBuilder::build));
50     }
51
52     @Override
53     public ClientIdentifier getIdentifier() {
54         return identifier;
55     }
56
57     void onHistoryClosed(final LocalHistoryIdentifier historyId) {
58         ensureHistory(historyId).onHistoryClosed();
59     }
60
61     void onHistoryPurged(final LocalHistoryIdentifier historyId) {
62         currentHistories.remove(historyId);
63         // XXX: do we need to account for cookies?
64         purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(historyId.getHistoryId())));
65     }
66
67     void onTransactionCommitted(final TransactionIdentifier txId) {
68         ensureHistory(txId.getHistoryId()).onTransactionCommitted(txId);
69     }
70
71     private FrontendHistoryMetadataBuilder ensureHistory(final LocalHistoryIdentifier historyId) {
72         final FrontendHistoryMetadataBuilder existing = currentHistories.get(historyId);
73         if (existing != null) {
74             return existing;
75         }
76
77         final FrontendHistoryMetadataBuilder ret = new FrontendHistoryMetadataBuilder(historyId);
78         currentHistories.put(historyId, ret);
79         return ret;
80     }
81 }

©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.