164e7a9aa8e2add26727ef6ec84bdb2101d16231
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendMetadata.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.collect.Collections2;
11 import java.util.HashMap;
12 import java.util.Map;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
19 import org.opendaylight.controller.cluster.datastore.persisted.FrontendShardDataTreeSnapshotMetadata;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Frontend state as observed by a shard follower. This class is responsible for maintaining metadata state
25  * so that this can be used to seed {@link LeaderFrontendState} with proper state so that the frontend/backend
26  * conversation can continue where it left off.
27  *
28  * @author Robert Varga
29  */
30 @NotThreadSafe
31 final class FrontendMetadata extends ShardDataTreeMetadata<FrontendShardDataTreeSnapshotMetadata> {
32     private static final Logger LOG = LoggerFactory.getLogger(FrontendMetadata.class);
33
34     private final Map<FrontendIdentifier, FrontendClientMetadataBuilder> clients = new HashMap<>();
35
36     @Override
37     Class<FrontendShardDataTreeSnapshotMetadata> getSupportedType() {
38         return FrontendShardDataTreeSnapshotMetadata.class;
39     }
40
41     @Override
42     void reset() {
43         clients.clear();
44     }
45
46     @Override
47     void doApplySnapshot(final FrontendShardDataTreeSnapshotMetadata snapshot) {
48         clients.clear();
49
50         for (FrontendClientMetadata m : snapshot.getClients()) {
51             clients.put(m.getIdentifier().getFrontendId(), new FrontendClientMetadataBuilder(m));
52         }
53     }
54
55     @Override
56     FrontendShardDataTreeSnapshotMetadata toStapshot() {
57         return new FrontendShardDataTreeSnapshotMetadata(Collections2.transform(clients.values(),
58             FrontendClientMetadataBuilder::build));
59     }
60
61     private FrontendClientMetadataBuilder ensureClient(final ClientIdentifier id) {
62         final FrontendClientMetadataBuilder existing = clients.get(id.getFrontendId());
63         if (existing != null && id.equals(existing.getIdentifier())) {
64             return existing;
65         }
66
67         final FrontendClientMetadataBuilder client = new FrontendClientMetadataBuilder(id);
68         final FrontendClientMetadataBuilder previous = clients.put(id.getFrontendId(), client);
69         if (previous != null) {
70             LOG.debug("Replaced client {} with {}", previous, client);
71         } else {
72             LOG.debug("Added client {}", client);
73         }
74         return client;
75     }
76
77     @Override
78     void onHistoryClosed(final LocalHistoryIdentifier historyId) {
79         ensureClient(historyId.getClientId()).onHistoryClosed(historyId);
80     }
81
82     @Override
83     void onHistoryPurged(final LocalHistoryIdentifier historyId) {
84         ensureClient(historyId.getClientId()).onHistoryPurged(historyId);
85     }
86
87     @Override
88     void onTransactionCommitted(final TransactionIdentifier txId) {
89         ensureClient(txId.getHistoryId().getClientId()).onTransactionCommitted(txId);
90     }
91 }

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