2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.Maps;
13 import java.util.HashMap;
15 import javax.annotation.Nonnull;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
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.FrontendShardDataTreeSnapshotMetadata;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Frontend state as observed by a shard follower. This class is responsible for maintaining metadata state
28 * so that this can be used to seed {@link LeaderFrontendState} with proper state so that the frontend/backend
29 * conversation can continue where it left off.
31 * @author Robert Varga
34 final class FrontendMetadata extends ShardDataTreeMetadata<FrontendShardDataTreeSnapshotMetadata> {
35 private static final Logger LOG = LoggerFactory.getLogger(FrontendMetadata.class);
37 private final Map<FrontendIdentifier, FrontendClientMetadataBuilder> clients = new HashMap<>();
38 private final String shardName;
40 FrontendMetadata(final String shardName) {
41 this.shardName = Preconditions.checkNotNull(shardName);
45 Class<FrontendShardDataTreeSnapshotMetadata> getSupportedType() {
46 return FrontendShardDataTreeSnapshotMetadata.class;
55 void doApplySnapshot(final FrontendShardDataTreeSnapshotMetadata snapshot) {
58 for (FrontendClientMetadata m : snapshot.getClients()) {
59 clients.put(m.getIdentifier().getFrontendId(), new FrontendClientMetadataBuilder(m));
64 FrontendShardDataTreeSnapshotMetadata toSnapshot() {
65 return new FrontendShardDataTreeSnapshotMetadata(Collections2.transform(clients.values(),
66 FrontendClientMetadataBuilder::build));
69 private FrontendClientMetadataBuilder ensureClient(final ClientIdentifier id) {
70 final FrontendClientMetadataBuilder existing = clients.get(id.getFrontendId());
71 if (existing != null && id.equals(existing.getIdentifier())) {
75 final FrontendClientMetadataBuilder client = new FrontendClientMetadataBuilder(id);
76 final FrontendClientMetadataBuilder previous = clients.put(id.getFrontendId(), client);
77 if (previous != null) {
78 LOG.debug("{}: Replaced client {} with {}", shardName, previous, client);
80 LOG.debug("{}: Added client {}", shardName, client);
86 void onHistoryCreated(final LocalHistoryIdentifier historyId) {
87 ensureClient(historyId.getClientId()).onHistoryCreated(historyId);
91 void onHistoryClosed(final LocalHistoryIdentifier historyId) {
92 ensureClient(historyId.getClientId()).onHistoryClosed(historyId);
96 void onHistoryPurged(final LocalHistoryIdentifier historyId) {
97 ensureClient(historyId.getClientId()).onHistoryPurged(historyId);
101 void onTransactionAborted(final TransactionIdentifier txId) {
102 ensureClient(txId.getHistoryId().getClientId()).onTransactionAborted(txId);
106 void onTransactionCommitted(final TransactionIdentifier txId) {
107 ensureClient(txId.getHistoryId().getClientId()).onTransactionCommitted(txId);
111 void onTransactionPurged(final TransactionIdentifier txId) {
112 ensureClient(txId.getHistoryId().getClientId()).onTransactionPurged(txId);
116 * Transform frontend metadata into an active leader state map.
118 * @return Leader frontend state
120 @Nonnull Map<FrontendIdentifier, LeaderFrontendState> toLeaderState(@Nonnull final Shard shard) {
121 return new HashMap<>(Maps.transformValues(clients, meta -> meta.toLeaderState(shard)));