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.collect.Collections2;
11 import java.util.HashMap;
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;
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.
28 * @author Robert Varga
31 final class FrontendMetadata extends ShardDataTreeMetadata<FrontendShardDataTreeSnapshotMetadata> {
32 private static final Logger LOG = LoggerFactory.getLogger(FrontendMetadata.class);
34 private final Map<FrontendIdentifier, FrontendClientMetadataBuilder> clients = new HashMap<>();
37 Class<FrontendShardDataTreeSnapshotMetadata> getSupportedType() {
38 return FrontendShardDataTreeSnapshotMetadata.class;
47 void doApplySnapshot(final FrontendShardDataTreeSnapshotMetadata snapshot) {
50 for (FrontendClientMetadata m : snapshot.getClients()) {
51 clients.put(m.getIdentifier().getFrontendId(), new FrontendClientMetadataBuilder(m));
56 FrontendShardDataTreeSnapshotMetadata toSnapshot() {
57 return new FrontendShardDataTreeSnapshotMetadata(Collections2.transform(clients.values(),
58 FrontendClientMetadataBuilder::build));
61 private FrontendClientMetadataBuilder ensureClient(final ClientIdentifier id) {
62 final FrontendClientMetadataBuilder existing = clients.get(id.getFrontendId());
63 if (existing != null && id.equals(existing.getIdentifier())) {
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);
72 LOG.debug("Added client {}", client);
78 void onHistoryCreated(final LocalHistoryIdentifier historyId) {
79 ensureClient(historyId.getClientId()).onHistoryCreated(historyId);
83 void onHistoryClosed(final LocalHistoryIdentifier historyId) {
84 ensureClient(historyId.getClientId()).onHistoryClosed(historyId);
88 void onHistoryPurged(final LocalHistoryIdentifier historyId) {
89 ensureClient(historyId.getClientId()).onHistoryPurged(historyId);
93 void onTransactionCommitted(final TransactionIdentifier txId) {
94 ensureClient(txId.getHistoryId().getClientId()).onTransactionCommitted(txId);